-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser_yacc.py
405 lines (322 loc) · 9.75 KB
/
parser_yacc.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#from ply import yacc, lex
from ply.ply import yacc, lex
import lexer_lex
from lexer_lex import tokens
from constructs.ast import *
from constructs.symbol_table import SymbolTable
from constructs.ast_vis import draw_AST
import icg
import sys
import ico
symtab = SymbolTable()
start_ast = Node("start", children=[])
def cprint(ptype: str, start: int, end: int ):
print("<",ptype,", ", start, ", ", end,">", sep="")
def p_start(p):
"""
start : start command
| empty
"""
if (len(p) != 2):
start_ast.children.append(p[2])
def p_command(p):
"""
command : use
| print
| var_decl
| arr_decl
| var_op
| until
| foreach
"""
p[0] = p[1]
def p_use(p):
"""
use : USE ID SEMI
"""
p[0] = Use(p[2])
cprint("use statement", p.lineno(1), p.lineno(3))
def p_var_decl(p):
"""
var_decl : MY VARNAME SEMI
| MY VARNAME EQ identifier_types SEMI
| MY VARNAME EQ expr SEMI
"""
if (len(p) == 4):
print('var_decl', p[2], "belieb")
symtab.add_if_not_exists(p[2][0])
p[0] = BinOP("=", Literal("variable", p[2][0]), Literal("string", ""))
cprint("variable declaration", p.lineno(1), p.lineno(3))
elif (len(p) == 6):
print('var_decl', p[2], p[4])
symtab.add_if_not_exists(p[2][0])
symtab.get_symbol(p[2][0]).value = p[4]
if isinstance(p[4], Node):
val = p[4]
else:
val = p[4][0]
if ( val.isnumeric()):
print("NUMBER", p[4])
val = Literal('number', p[4][0])
else:
val = Literal('string', p[4][0])
p[0] = BinOP('=', Literal("variable", p[2][0]), val)
cprint("variable declaration", p.lineno(1), p.lineno(5))
symtab.get_symbol(p[2][0]).lineno = p.lineno(2)
def p_arr_decl(p):
"""
arr_decl : MY ARRNAME SEMI
| MY ARRNAME EQ OP handle_types CL SEMI
"""
if (len(p) == 4):
symtab.add_if_not_exists(p[2])
p[0] = Array(p[2])
cprint("array declaration", p.lineno(1), p.lineno(3))
elif (len(p) == 8):
symtab.add_if_not_exists(p[2])
symtab.get_symbol(p[2]).value = p[5]
p[0] = Array(p[2], data=p[5])
cprint("array declaration", p.lineno(1), p.lineno(7))
symtab.get_symbol(p[2]).lineno = p.lineno(2)
def p_identifier_types(p):
"""
identifier_types : VARNAME INDEXOP NUMBER INDEXCL
| VARNAME INDEXOP VARNAME INDEXCL
| VARNAME
| NUMBER
| STRING
"""
if (len(p) == 2):
cprint("Identifier", p.lineno(1), p.lineno(1))
if (p[1][1] == "VARNAME"):
symbol = symtab.get_symbol(p[1][0])
if (symbol is None):
print("ERROR AT", p.lineno(1))
print("Variable accessed before declaration")
else:
print(symbol)
p[0] = Literal("variable", p[1][0])
elif(p[1][1] == "NUMBER"):
cprint("Number", p.lineno(1), p.lineno(1))
p[0] = Literal("number", p[1][0])
else:
cprint("String", p.lineno(1), p.lineno(1))
p[0] = Literal("string", p[1][0])
else:
symbol = symtab.get_symbol(p[1][0])
if (symbol is None):
print("ERROR AT", p.lineno(1))
print("Array used before declaration.")
else:
print(symbol)
p[0] = Array(p[1][0], index=p[3][0])
cprint("Array", p.lineno(1), p.lineno(1))
def p_var_op(p):
"""
var_op : identifier_types EQ identifier_types SEMI
| identifier_types EQ expr SEMI
| identifier_types INCREMENT SEMI
| identifier_types DECREMENT SEMI
| INCREMENT identifier_types
| DECREMENT identifier_types
"""
if('=' in p):
print("var_op: ", p[1], p[3])
t = symtab.get_symbol(p[1].value)
if t is None:
print(symtab)
t = symtab.get_symbol(p[1])
t.value = p[3]
t = symtab.get_symbol(p[1].value)
print(t)
p[0] = BinOP('=', p[1], p[3])
cprint("Variable reassignment", p.lineno(1), p.lineno(4))
elif('++' in p):
cprint("Variable increment", p.lineno(1), p.lineno(3))
if ( '+' in p[1] ):
p[0] = BinOP('++', right=p[2])
else:
p[0] = BinOP('++', left=p[2])
elif('--' in p):
cprint("Variable decrement", p.lineno(1), p.lineno(3))
if ( '-' in p[1] ):
p[0] = BinOP('--', right=p[2])
else:
p[0] = BinOP('--', left=p[2])
def p_until(p):
"""
until : UNTIL OP logical_expr_main CL block_op block block_cl
| UNTIL OP relational_expr CL block_op block block_cl
"""
p[0] = Until(p[3], p[6])
cprint("until block", p.lineno(1), p.lineno(7))
def p_foreach(p):
"""
foreach : FOREACH OP ARRNAME CL block_op block block_cl
| FOREACH OP handle_types CL block_op block block_cl
"""
if ('@' in p[3]):
symb = symtab.get_symbol(p[3])
if (symb is None):
print("ERROR array accessed before declaration.")
p[0] = Foreach(p[3], p[6])
cprint("foreach block", p.lineno(1), p.lineno(7))
def p_block_op(p):
"""
block_op : BLOCKOP
"""
symtab.enter_scope()
def p_block_cl(p):
"""
block_cl : BLOCKCL
"""
symtab.leave_scope()
def p_block(p):
"""
block : block command
| empty
"""
if (len(p) == 2):
p[0] = List([])
else:
p[1].append(p[2])
p[0] = p[1]
def p_print(p):
"""
print : PRINT handle_types SEMI
"""
p[0] = Print(p[2])
cprint("print statement", p.lineno(1), p.lineno(3))
def p_handle_types(p):
"""
handle_types : identifier_types COMMA handle_types
| identifier_types
"""
if (len(p) == 2):
p[0] = List([p[1]])
else:
p[3].append(p[1])
p[0] = p[3]
def p_relational_expr(p):
"""
relational_expr : identifier_types GT identifier_types
| identifier_types LT identifier_types
| identifier_types EQ EQ identifier_types
| identifier_types GT EQ identifier_types
| identifier_types LT EQ identifier_types
"""
if (len(p) == 2):
cprint("relational expr", p.lineno(1), p.lineno(1))
elif(len(p) == 4):
p[0] = RelationalExpr(p[2], left = p[1], right = p[3], type=bool)
cprint("relational expr", p.lineno(1), p.lineno(3))
elif (len(p) == 5):
temp_op = str(p[2]+p[3])
p[0] = RelationalExpr(temp_op, left = p[1], right = p[4], type=bool)
cprint("relational expr", p.lineno(1), p.lineno(4))
else:
print("relational expr")
def p_logical_expr(p):
"""
logical_expr : logical_expr_bin AND identifier_types
| logical_expr_bin OR identifier_types
| logical_expr_bin AND relational_expr
| logical_expr_bin OR relational_expr
| logical_expr_bin AND logical_expr_bin
| logical_expr_bin OR logical_expr_bin
| logical_expr_bin
| empty
"""
if (len(p) == 2):
p[0] = p[1]
elif (len(p) == 4):
p[0] = LogicalExprBin(p[2], left = p[1], right = p[3], type=bool)
def p_logical_expr_bin(p):
"""
logical_expr_bin : identifier_types AND identifier_types
| identifier_types OR identifier_types
| relational_expr AND relational_expr
| relational_expr OR relational_expr
"""
if (len(p) == 2):
p[0] = p[1]
elif (len(p) == 4):
p[0] = LogicalExprBin(p[2], left = p[1], right = p[3], type=bool)
def p_logical_expr_main(p):
"""
logical_expr_main : NOT logical_expr
| logical_expr
"""
if (len(p) == 2):
p[0] = p[1]
else:
p[0] = LogicalExprBin(p[1], right=p[2], type=bool)
def p_expr(p):
"""
expr : expr_bin PLS identifier_types
| expr_bin MIN identifier_types
| expr_bin DIV identifier_types
| expr_bin MUL identifier_types
| expr_bin PLS expr_bin
| expr_bin MIN expr_bin
| expr_bin DIV expr_bin
| expr_bin MUL expr_bin
| expr_bin
| empty
"""
if (len(p) == 2):
p[0] = p[1]
elif (len(p) == 4):
p[0] = BinOP(p[2], left = p[1], right = p[3])
def p_expr_bin_op(p):
"""
expr_bin : identifier_types PLS identifier_types
| identifier_types MIN identifier_types
| identifier_types DIV identifier_types
| identifier_types MUL identifier_types
"""
cprint("binary operation", p.lineno(1), p.lineno(1))
p[0] = BinOP(p[2], left = p[3], right=p[1])
def p_empty(p):
"""
empty :
"""
pass
def p_error(p):
print("syntax error", p.lineno)
if __name__ == "__main__":
lexer = lex.lex(module=lexer_lex)
parser = yacc.yacc(debug=True)
with open(sys.argv[1], "rt") as f:
data = f.read()
lexer.input(data)
result = parser.parse(data)
print(result)
print()
print()
print()
print("Printing SYMBOL_TABLE")
print(symtab)
print()
print()
print()
print("Generating AST")
draw_AST(start_ast)
print("Intermediate Code Generation")
res = icg.intermediate_codegen(start_ast, symtab)
res.print_three_address_code()
print()
print()
print()
print("Printing SYMBOL_TABLE")
print(symtab)
print()
print()
print()
print("PRINTING QUAD TABLE")
print(res)
print()
print()
print()
res = ico.parse_ico(res, symtab)
res.print_three_address_code()