-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
528 lines (433 loc) · 12.3 KB
/
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
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
from lexer import tokens
from functionDirectory import FunctionDirectory
from quadManager import QuadManager
funcDir = FunctionDirectory()
quads = QuadManager(funcDir)
# Parsing productions
# PROGRAM
def p_program(p):
"program : PROGRAM ID found_program_name ';' vars functions main body"
# Finish parsing
quads.addEndQuad()
p[0] = quads
# Build .o
quads.build()
# Make a GOTO quad to main()
def p_found_program_name(p):
'found_program_name : empty'
if quads.debug:
print("\n|==|==|==|==|==|==|==|==| START DEBUG LOG |==|==|==|==|==|==|==|==|\n")
funcDir.addFunction('main')
funcDir.setGlobalFunction()
quads.addMainQuad()
# VARS
# Variable declaration
def p_vars(p):
"""vars : VAR found_var var_declare
| empty"""
pass
def p_found_var(p):
"found_var : empty"
funcDir.createVarTable()
def p_var_declare(p):
"""var_declare : type var_name ';' var_declare
| type var_name ';'"""
pass
def p_var_name(p):
"""var_name : variable_declare var_dims ',' var_name
| variable_declare var_dims"""
pass
# Create var upon finding its ID
def p_variable_declare(p):
"variable_declare : ID"
var = p[1]
if funcDir.varAvailable(var):
funcDir.addVar(var, quads.vDir.generateVirtualAddress(funcDir.currentFunc, funcDir.currentType))
funcDir.setVarHelper(var)
else:
s_error(f'Variable "{var}" already exists!"')
def p_var_no_dims(p):
"var_dims : empty"
pass
# Make space for variable array/matrix
def p_var_dims(p):
"""var_dims : dim dim
| dim"""
if len(p) == 3:
funcDir.setVarDims([p[1], p[2]])
p[0] = p[1] * p[2]
else:
funcDir.setVarDims([p[1]])
p[0] = p[1]
quads.vDir.makeSpaceForArray(funcDir.currentFunc, funcDir.currentType, p[0])
# TYPE
def p_type(p):
"""type : INT
| FLOAT
| CHAR
| BOOL"""
funcDir.setCurrentType(p[1])
p[0] = p[1]
# FUNCTIONS
def p_functions(p):
"""functions : FUNCTION func_type ID found_func_name func_dims '(' func_params ')' vars found_func_start body found_func_end functions
| empty"""
pass
# Set function type
def p_func_type(p):
"""func_type : type
| VOID"""
if p[1] == 'void':
funcDir.setCurrentType('void')
# Create function
def p_found_func_name(p):
"found_func_name : empty"
func = p[-1]
if funcDir.functionExists(func):
s_error(f'Function "{func}" already exists!"')
else:
funcDir.addFunction(func)
funcDir.createVarTable()
def p_func_no_dims(p):
"func_dims : empty"
pass
# Set return dimensions
def p_func_dims(p):
"""func_dims : dim dim
| dim"""
if len(p) == 3:
funcDir.setReturnDims([p[1], p[2]])
else:
funcDir.setReturnDims([p[1]])
def p_dim(p):
"dim : '[' CTE_INT ']'"
if p[2] <= 0:
s_error(f'Zero or negative indexes are not allowed! [{p[0]}]')
p[0] = p[2]
def p_func_params(p):
"""func_params : func_param
| empty"""
pass
def p_func_param(p):
"""func_param : type ID found_func_param param_dims ',' func_param
| type ID found_func_param param_dims"""
pass
# Add function parameter
def p_found_func_param(p):
"found_func_param : empty"
param = p[-1]
if funcDir.varAvailable(param):
funcDir.setVarHelper(param)
vAddr = quads.vDir.generateVirtualAddress(funcDir.currentFunc, funcDir.currentType)
funcDir.addVar(param, vAddr)
funcDir.addFuncParam(vAddr)
else:
s_error(f'Multiple declaration of "{param}"!')
def p_param_no_dims(p):
"param_dims : empty"
pass
# Add dimensions to function parameter
def p_param_dims(p):
"""param_dims : dim dim
| dim"""
space = None
if len(p) == 3:
p[0] = [p[1], p[2]]
space = p[1] * p[2]
else:
p[0] = [p[1]]
space = p[1]
funcDir.addFuncParamDims(p[0])
funcDir.setVarDims(p[0])
quads.vDir.makeSpaceForArray(funcDir.currentFunc, funcDir.currentType, space)
# Set quad start for start of function
def p_found_func_start(p):
"found_func_start : empty"
funcDir.setQuadStart(quads.getQuadCount())
# Prepare end of function
def p_found_func_end(p):
"found_func_end : empty"
quads.addEndFuncQuad()
funcDir.deleteVarTable()
# MAIN
def p_main(p):
"main : MAIN '(' ')'"
quads.completeMainQuad()
# BODY
def p_body(p):
"body : '{' statements '}'"
pass
def p_statements(p):
"""statements : statement statements
| empty"""
pass
def p_statement(p):
"""statement : assignment
| call_func
| return
| read
| print
| if
| from
| while
| break
| empty"""
pass
## ASSIGNMENT
def p_assignment(p):
"assignment : expr_var '=' found_expr_duo_op expr found_assignment_end ';'"
pass
# Add assignment quadruple
def p_found_assignment_end(p):
"found_assignment_end : empty"
quads.addAssignQuad()
## RETURN
def p_return(p):
"return : RETURN '(' expr ')' ';'"
quads.addReturnQuad()
## READ
def p_read(p):
"read : READ '(' read_params ')' ';'"
pass
def p_read_params(p):
"""read_params : expr_var found_read_param ',' read_params
| expr_var found_read_param"""
pass
# Add read quadruple
def p_found_read_param(p):
"found_read_param : empty"
quads.addReadQuad()
## PRINT
def p_print(p):
"print : PRINT '(' print_params ')' ';'"
pass
def p_print_params(p):
"""print_params : print_param ',' print_params
| print_param"""
pass
# Add print quadruple
def p_print_param(p):
"print_param : expr"
quads.addPrintQuad(False)
# Add print quadruple
def p_print_string(p):
"print_param : STRING"
quads.addPrintQuad(p[1])
## IF
def p_if(p):
"""if : IF '(' expr ')' found_if_expr THEN body
| IF '(' expr ')' found_if_expr THEN body else"""
quads.completeIfQuad()
def p_found_if_expr(p):
"found_if_expr : empty"
quads.addGoToFQuad()
## ELSE
def p_else(p):
"else : ELSE found_else body"
pass
def p_found_else(p):
"found_else : empty"
quads.addElseQuad()
## FROM
def p_from(p):
"from : FROM '(' ID found_from_iterator '=' expr found_from_start TO expr ')' found_from_cond DO body"
quads.addFromEndQuads()
def p_found_from_iterator(p):
"found_from_iterator : empty"
quads.addFromIteratorQuads(p[-1])
def p_found_from_start(p):
"found_from_start : empty"
quads.addFromStartQuad()
def p_found_from_cond(p):
"found_from_cond : empty"
quads.addFromCondQuads()
## WHILE
def p_while(p):
"while : WHILE found_while '(' expr ')' found_while_expr DO body"
quads.completeLoopQuad()
def p_found_while(p):
"found_while : empty"
quads.prepareLoop()
def p_found_while_expr(p):
"found_while_expr : empty"
quads.addGoToFQuad()
## BREAK
def p_break(p):
"break : BREAK ';'"
quads.addBreakQuad()
# EXPRESSION -> Order of operator precedence:
# - Matrix operations ($, !, ?)
# - Factors (*, /, %, .)
# - Arithmetic (+, -)
# - Comparison (==, !=, <, <=, >, >=)
# - Logic (&, |)
def p_expr(p):
"expr : expr_logic"
pass
def p_expr_logic(p):
"expr_logic : expr_compare found_expr_logic expr_logic2"
pass
def p_found_expr_logic(p):
"found_expr_logic : empty"
quads.addDualOpQuad(['&', '|'])
def p_expr_logic2(p):
"""expr_logic2 : '&' found_expr_duo_op expr_logic
| '|' found_expr_duo_op expr_logic
| empty"""
pass
def p_expr_compare(p):
"expr_compare : expr_arith found_expr_compare expr_compare2"
pass
def p_found_expr_compare(p):
"found_expr_compare : empty"
quads.addDualOpQuad(['==', '!=', '<', '<=', '>', '>='])
def p_expr_compare2(p):
"""expr_compare2 : IS_EQUAL found_expr_duo_op expr_compare
| IS_NOT_EQUAL found_expr_duo_op expr_compare
| '<' found_expr_duo_op expr_compare
| LESS_THAN_OR_EQUAL found_expr_duo_op expr_compare
| '>' found_expr_duo_op expr_compare
| MORE_THAN_OR_EQUAL found_expr_duo_op expr_compare
| empty"""
pass
def p_expr_arith(p):
"expr_arith : expr_factor found_expr_arith expr_arith2"
pass
def p_found_expr_arith(p):
"found_expr_arith : empty"
quads.addDualOpQuad(['+', '-'])
def p_expr_arith2(p):
"""expr_arith2 : '+' found_expr_duo_op expr_arith
| '-' found_expr_duo_op expr_arith
| empty"""
pass
def p_expr_factor(p):
"expr_factor : expr_mono found_expr_factor expr_factor2"
pass
def p_found_expr_factor(p):
"found_expr_factor : empty"
quads.addDualOpQuad(['*', '/', '%', '.'])
def p_expr_factor2(p):
"""expr_factor2 : '*' found_expr_duo_op expr_factor
| '/' found_expr_duo_op expr_factor
| '%' found_expr_duo_op expr_factor
| '.' found_expr_duo_op expr_factor
| empty"""
pass
def p_found_expr_duo_op(p):
"found_expr_duo_op : empty"
quads.pushOperator(p[-1])
def p_expr_mono(p):
"expr_mono : expr_atom expr_mono_op"
pass
def p_expr_mono_op(p):
"""expr_mono_op : '!' found_expr_mono_op expr_mono_op
| '$' found_expr_mono_op expr_mono_op
| '?' found_expr_mono_op expr_mono_op
| empty"""
pass
def p_found_expr_mono_op(p):
"found_expr_mono_op : empty"
quads.addMonoOpQuad(p[-1])
def p_expr_atom(p):
"""expr_atom : expr_group
| expr_call_func
| expr_var"""
pass
def p_expr_group(p):
"expr_group : '(' found_expr_duo_op expr ')'"
quads.popOperator()
def p_expr_var(p):
"expr_var : expr_var_name expr_var_dims"
pass
def p_expr_var_name(p):
"expr_var_name : ID"
var = funcDir.getVar(p[1])
funcDir.setVarHelper(var.name)
quads.pushVar(var)
def p_expr_var_no_dims(p):
"expr_var_dims : empty"
pass
def p_expr_var_dims(p):
"""expr_var_dims : found_expr_var_dims expr_var_dim expr_var_dim
| found_expr_var_dims expr_var_dim"""
quads.addBaseAddressQuad()
def p_found_expr_var_dims(p):
"found_expr_var_dims : empty"
quads.sDims.append((funcDir.varHelper, 0))
def p_expr_var_dim(p):
"expr_var_dim : '[' expr_var_open_dim expr ']'"
quads.addArrQuads()
def p_expr_var_open_dim(p):
"expr_var_open_dim : empty"
aux = quads.sDims.pop()
if aux[1] + 1 > len(funcDir.getDimensionsOfVar(aux[0])):
raise Exception(f'{aux[0]} has {len(funcDir.getDimensionsOfVar(aux[0]))} dimension(s)! -> {aux[1]}')
quads.sDims.append((aux[0], aux[1] + 1))
quads.sOperators.append('(')
def p_cte(p):
"""expr_var : CTE_INT
| CTE_FLOAT
| CTE_BOOL
| CTE_CHAR"""
t = None
if (type(p[1]) is int):
t = 'int'
elif (type(p[1]) is float):
t = 'float'
elif (type(p[1]) is str):
t = 'char'
elif (type(p[1] is bool)):
t = 'bool'
cte = quads.upsertCte(p[1], t)
quads.pushCte(cte)
def p_expr_call_func(p):
"expr_call_func : ID found_call_func_name '(' call_func_params ')' found_call_func_end"
quads.addAssignFuncQuad()
## CALL_FUNCTION
def p_call_func(p):
"call_func : ID found_call_func_name '(' call_func_params ')' found_call_func_end ';'"
func = quads.popFunction()
if quads.funcDir.getReturnTypeOfFunc(func) != 'void':
raise Exception(f"This function is non-void, therefore it can't be used as an expression! -> {func}")
def p_found_call_func_name(p):
"found_call_func_name : empty"
func = p[-1]
if funcDir.functionExists(func):
quads.addEraQuad(func)
quads.pushFunction(func)
quads.sOperators.append('(')
else:
raise Exception(f'Function {func}() does not exist!')
def p_call_func_params(p):
"""call_func_params : call_func_param
| empty"""
pass
def p_call_func_param(p):
"""call_func_param : expr func_single_step ',' call_func_param
| expr func_single_step"""
pass
def p_func_single_step(p):
"func_single_step : empty"
target_param = funcDir.getParamOfFunc(quads.getTopFunction())
quads.addParamQuad(target_param, funcDir.paramCount)
funcDir.incrementParamCount()
pass
def p_found_call_func_end(p):
"found_call_func_end : empty"
func = quads.getTopFunction()
if funcDir.verifyParamCount(func):
funcDir.resetParamCount()
quads.addGoSubQuad(func, funcDir.getQuadStartOfFunc(func))
quads.popOperator()
else:
raise Exception(f'Wrong number of parameters in {func}!')
def p_empty(p):
"empty :"
pass
# Error handling
def p_error(p):
raise Exception(f'({p.lineno}) Syntax error at "{p.value}"')
# Manual error (line number & position are kinda broken...)
def s_error(msg):
raise Exception(msg)