-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths1_parser.py
310 lines (272 loc) · 7.31 KB
/
s1_parser.py
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
class AST:
repr_start_start = "\n========== START OF "
repr_end_start = "\n========== END OF "
repr_end = " =========="
def parser_format(self, first, second):
return f"{AST.repr_start_start}{first}{AST.repr_end}\n{str(second)}{AST.repr_end_start}{first}{AST.repr_end}"
class Conditional(AST):
def __init__(self, left, right):
self.cond = left
self.stat = right
def __repr__(self):
return f"Condition of this {self.__class__.name}:\n" + self.parser_format("CONDITION", self.cond) + \
f"\nStatements of this {self.__class__.name}:\n" + self.parser_format("LIST OF STATEMENTS", self.stat)
class IfE(Conditional):
name = "IfE"
class IfNe(Conditional):
name = "IfNe"
class WhileE(Conditional):
name = "WhileE"
class WhileNe(Conditional):
name = "WhileNe"
class Return(AST):
def __init__(self, expr):
self.expr = expr
def __repr__(self):
return "Expression of this return:\n" + self.parser_format("EXPRESSION", self.expr)
class Func(AST):
def __init__(self, left, middle, right):
self.name = left
self.pars = middle
self.code = right
def __repr__(self):
repr = "Name of this function:\n" + self.name + "\nFormal parameters of this function:\n"
for parameter in self.pars:
repr += str(parameter) + "\n"
return repr + "Code of this function:\n" + self.parser_format("LIST OF STATEMENTS", self.code)
class Program(AST):
def __init__(self):
self.blocks = []
def __repr__(self):
repr = "Blocks of this program:"
for block in self.blocks:
repr += "\n" + self.parser_format("BLOCK", block)
return repr
class Assign(AST):
def __init__(self, left, right):
self.var = left
self.value = right
class FuncCall(AST):
def __init__(self, left):
self.name = left
self.pars = []
class BinOp(AST):
def __init__(self, left, op, right):
self.left = left
self.op = op
self.right = right
class NoOp(AST):
def __init__(self):
pass
class Var(AST):
def __init__(self, token):
self.name = token.value
def __repr__(self):
return "Variable " + self.name
class Set(AST):
def __init__(self):
self.pars = []
class Parser:
def __init__(self, lexer):
self.lexer = lexer
self.next()
def next(self):
self.current_token = self.lexer.get_next_token()
def parse(self):
node = self.program()
assert self.current_token.type == 'EOF'
return node
def program(self):
node = Program()
while (self.current_token.type != 'EOF'):
node.blocks.append(self.block())
return node
def block(self):
if self.current_token.type in ['IFE', 'IFNE', 'WHIE', 'WHINE']:
token = self.current_token.type
return self.conditional(token, {"IFE": IfE, "IFNE": IfNe, "WHIE": WhileE, "WHINE": WhileNe}[token])
else:
assert self.current_token.type == 'ID'
pos = self.lexer.pos
token = self.current_token
try:
return self.func()
except:
self.lexer.pos = pos
self.current_token = token
return self.statement_list()
def conditional(self, token, class_name):
assert self.current_token.type == token
self.next()
assert self.current_token.type == 'LBR'
self.next()
expr = self.expr()
assert self.current_token.type == 'RBR'
self.next()
assert self.current_token.type == 'LCBR'
self.next()
stats_list = self.statement_list()
assert self.current_token.type == 'RCBR'
self.next()
return class_name(expr, stats_list)
def func(self):
name = self.func_name()
assert self.current_token.type == 'LBR'
self.next()
pars = self.formparams()
assert self.current_token.type == 'RBR'
self.next()
assert self.current_token.type == 'LCBR'
self.next()
stats_list = self.statement_list(True)
assert self.current_token.type == 'RCBR'
self.next()
return Func(name, pars, stats_list)
def formparams(self):
node = []
while self.current_token.type == 'ID':
node.append(self.var())
if self.current_token.type != 'COMMA':
pos = self.lexer.pos
token = self.current_token
self.next()
assert self.current_token.type != 'ID'
self.lexer.pos = pos
self.current_token = token
else:
self.next()
return node
def var(self):
assert self.current_token.type == 'ID'
token = self.current_token
self.next()
return Var(token)
def func_name(self):
assert self.current_token.type == 'ID'
token = self.current_token
self.next()
return token.value
def statement_list(self, is_func = False):
stats_list = []
while True:
if self.current_token.type in ['ID', 'LCOMM', 'IFE', 'IFNE', 'WHIE', 'WHINE']:
stats_list.append(self.statement())
elif is_func and (self.current_token.type == 'RET'):
stats_list.append(self.func_statement())
else:
break
return stats_list
def func_statement(self):
assert self.current_token.type == 'RET'
self.next()
node = Return(self.expr())
assert self.current_token.type == 'SEMICOL'
self.next()
return node
def statement(self):
if self.current_token.type == 'EMP':
self.next()
return NoOp()
elif self.current_token.type == 'LCOMM':
self.next()
return NoOp()
elif self.current_token.type in ['IFE', 'IFNE', 'WHIE', 'WHINE']:
return self.block()
else:
assert self.current_token.type == 'ID'
pos = self.lexer.pos
token = self.current_token
try:
node = self.assign()
assert self.current_token.type == 'SEMICOL'
self.next()
return node
except:
try:
self.lexer.pos = pos
self.current_token = token
node = self.funcall()
assert self.current_token.type == 'SEMICOL'
self.next()
return node
except:
self.lexer.pos = pos
self.current_token = token
return self.func()
def params(self):
node = []
signal = False
signal2 = True
while True:
pos = self.lexer.pos
token = self.current_token
try:
node.append(self.expr())
if signal:
signal2 = False
if self.current_token.type != 'COMMA':
signal = True
else:
self.next()
except:
self.lexer.pos = pos
self.current_token = token
assert signal2
break
return node
def assign(self):
assert self.current_token.type == 'ID'
var = self.var()
assert self.current_token.type == 'ASSIGN'
self.next()
expr = self.expr()
return Assign(var, expr)
def funcall(self):
assert self.current_token.type == 'ID'
node = FuncCall(self.func_name())
assert self.current_token.type == 'LBR'
self.next()
pars = self.params()
node.pars = pars
assert self.current_token.type == 'RBR'
self.next()
return node
def expr(self):
assert self.current_token.type in ['ID', 'LBR', 'LSBR']
left = self.term()
token = self.current_token
if self.current_token.type in ['UNION', 'INTER', 'DIFF', 'SYMDIFF']:
self.next()
right = self.expr()
return BinOp(left, token, right)
return left
def term(self):
if self.current_token.type == 'LSBR':
return self.set1()
elif self.current_token.type == 'LBR':
self.next()
node = self.expr()
assert self.current_token.type == 'RBR'
self.next()
return node
else:
assert self.current_token.type == 'ID'
pos = self.lexer.pos
token = self.current_token
try:
node = self.funcall()
return node
except:
self.lexer.pos = pos
self.current_token = token
node = self.var()
return node
def set1(self):
assert self.current_token.type == 'LSBR'
self.next()
pars = self.params()
node = Set()
node.pars = pars
assert self.current_token.type == 'RSBR'
self.next()
return node