summaryrefslogtreecommitdiffstats
path: root/third_party/python/ply/example/yply/yparse.py
blob: 1f2e8d0922c816c39ea11e28fc07387336f0a35e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# parser for Unix yacc-based grammars
#
# Author: David Beazley (dave@dabeaz.com)
# Date  : October 2, 2006

import ylex
tokens = ylex.tokens

from ply import *

tokenlist = []
preclist = []

emit_code = 1


def p_yacc(p):
    '''yacc : defsection rulesection'''


def p_defsection(p):
    '''defsection : definitions SECTION
                  | SECTION'''
    p.lexer.lastsection = 1
    print("tokens = ", repr(tokenlist))
    print()
    print("precedence = ", repr(preclist))
    print()
    print("# -------------- RULES ----------------")
    print()


def p_rulesection(p):
    '''rulesection : rules SECTION'''

    print("# -------------- RULES END ----------------")
    print_code(p[2], 0)


def p_definitions(p):
    '''definitions : definitions definition
                   | definition'''


def p_definition_literal(p):
    '''definition : LITERAL'''
    print_code(p[1], 0)


def p_definition_start(p):
    '''definition : START ID'''
    print("start = '%s'" % p[2])


def p_definition_token(p):
    '''definition : toktype opttype idlist optsemi '''
    for i in p[3]:
        if i[0] not in "'\"":
            tokenlist.append(i)
    if p[1] == '%left':
        preclist.append(('left',) + tuple(p[3]))
    elif p[1] == '%right':
        preclist.append(('right',) + tuple(p[3]))
    elif p[1] == '%nonassoc':
        preclist.append(('nonassoc',) + tuple(p[3]))


def p_toktype(p):
    '''toktype : TOKEN
               | LEFT
               | RIGHT
               | NONASSOC'''
    p[0] = p[1]


def p_opttype(p):
    '''opttype : '<' ID '>'
               | empty'''


def p_idlist(p):
    '''idlist  : idlist optcomma tokenid
               | tokenid'''
    if len(p) == 2:
        p[0] = [p[1]]
    else:
        p[0] = p[1]
        p[1].append(p[3])


def p_tokenid(p):
    '''tokenid : ID 
               | ID NUMBER
               | QLITERAL
               | QLITERAL NUMBER'''
    p[0] = p[1]


def p_optsemi(p):
    '''optsemi : ';'
               | empty'''


def p_optcomma(p):
    '''optcomma : ','
                | empty'''


def p_definition_type(p):
    '''definition : TYPE '<' ID '>' namelist optsemi'''
    # type declarations are ignored


def p_namelist(p):
    '''namelist : namelist optcomma ID
                | ID'''


def p_definition_union(p):
    '''definition : UNION CODE optsemi'''
    # Union declarations are ignored


def p_rules(p):
    '''rules   : rules rule
               | rule'''
    if len(p) == 2:
        rule = p[1]
    else:
        rule = p[2]

    # Print out a Python equivalent of this rule

    embedded = []      # Embedded actions (a mess)
    embed_count = 0

    rulename = rule[0]
    rulecount = 1
    for r in rule[1]:
        # r contains one of the rule possibilities
        print("def p_%s_%d(p):" % (rulename, rulecount))
        prod = []
        prodcode = ""
        for i in range(len(r)):
            item = r[i]
            if item[0] == '{':    # A code block
                if i == len(r) - 1:
                    prodcode = item
                    break
                else:
                    # an embedded action
                    embed_name = "_embed%d_%s" % (embed_count, rulename)
                    prod.append(embed_name)
                    embedded.append((embed_name, item))
                    embed_count += 1
            else:
                prod.append(item)
        print("    '''%s : %s'''" % (rulename, " ".join(prod)))
        # Emit code
        print_code(prodcode, 4)
        print()
        rulecount += 1

    for e, code in embedded:
        print("def p_%s(p):" % e)
        print("    '''%s : '''" % e)
        print_code(code, 4)
        print()


def p_rule(p):
    '''rule : ID ':' rulelist ';' '''
    p[0] = (p[1], [p[3]])


def p_rule2(p):
    '''rule : ID ':' rulelist morerules ';' '''
    p[4].insert(0, p[3])
    p[0] = (p[1], p[4])


def p_rule_empty(p):
    '''rule : ID ':' ';' '''
    p[0] = (p[1], [[]])


def p_rule_empty2(p):
    '''rule : ID ':' morerules ';' '''

    p[3].insert(0, [])
    p[0] = (p[1], p[3])


def p_morerules(p):
    '''morerules : morerules '|' rulelist
                 | '|' rulelist
                 | '|'  '''

    if len(p) == 2:
        p[0] = [[]]
    elif len(p) == 3:
        p[0] = [p[2]]
    else:
        p[0] = p[1]
        p[0].append(p[3])

#   print("morerules", len(p), p[0])


def p_rulelist(p):
    '''rulelist : rulelist ruleitem
                | ruleitem'''

    if len(p) == 2:
        p[0] = [p[1]]
    else:
        p[0] = p[1]
        p[1].append(p[2])


def p_ruleitem(p):
    '''ruleitem : ID
                | QLITERAL
                | CODE
                | PREC'''
    p[0] = p[1]


def p_empty(p):
    '''empty : '''


def p_error(p):
    pass

yacc.yacc(debug=0)


def print_code(code, indent):
    if not emit_code:
        return
    codelines = code.splitlines()
    for c in codelines:
        print("%s# %s" % (" " * indent, c))