-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmaeStarYacc.py
543 lines (416 loc) · 16.4 KB
/
maeStarYacc.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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import ply.yacc as yacc
from maeStarLex import *
from quadruple import avail,avail_dict
# Jump stack
jump_stack = list()
for_stack = list()
# will hold a jump stack specifically for "dimensions"
dimensions_direction = list()
# will hold the line number of the dimensions
dimensions_set = set()
# With the line number as key, it will hold the value/dimensions of the specific variable
dimensions_dict = dict()
# ---------------------------- GRAMMAR FUNCTIONS -------------------------------
def p_program(p):
""" program : variables methods MAIN LBRACE severalstatutes RBRACE
"""
# Quitar variables????
def p_statutes(p):
""" statutes : conditional
| editvariables
| cyclical
| calling
| readwrite
"""
def p_severalstatutes(p):
""" severalstatutes : statutes severalstatutes
| statutes
| empty
"""
def p_editvariables(p):
""" editvariables : singledimension
| multidimension
| swapdimension
| multi_to_singledimension
| incrementdecrement
"""
def p_singledimension(p):
""" singledimension : ID ASSIGN arithexp SEMICOLON
"""
if p[1]:
re = operand_stack.pop(0)
id1 = operand_stack.pop(0)
square_obj.square('=', id1, '_', re) # Save square
def p_multi_to_singledimension(p):
""" multi_to_singledimension : ID ASSIGN ID dimensions SEMICOLON
"""
line_number = str(p.slice[1].__dict__['lineno'])
if line_number in dimensions_dict:
aux = 0
dimension_size = dimensions_dict[line_number].count('[')
if dimension_size == 1:
if len(operand_stack) > 3: # PLY carry on the first next ID on the operand stack, but we dont need this right now
aux = operand_stack.pop() # So we are going to ignore it for a bit
elif dimension_size == 2:
if len(operand_stack) > 4: # PLY carry on the first next ID on the operand stack, but we dont need this right now
aux = operand_stack.pop() # So we are going to ignore it for a bit
id1 = operand_stack.pop(0)
re = operand_stack.pop(0)
square_obj.square('=', re, '_', id1)
to_add_value = dimensions_dict[line_number]
num = square_obj.get_num() - 1
prev_value = square_obj.quadruple_dict[num][1]
new_value = prev_value + to_add_value
square_obj.quadruple_dict[num][1] = new_value # We add the new dimension to the variable
for _ in range(new_value.count('[')): # We need to remove the extra operands
operand_stack.pop()
if aux:
operand_stack.append(aux)
def p_multidimension(p):
""" multidimension : ID dimensions ASSIGN arithexp SEMICOLON
"""
# adding dimension to array variables in quadruples
line_number = str(p.slice[1].__dict__['lineno'])
if line_number in dimensions_dict:
aux = 0
dimension_size = dimensions_dict[line_number].count('[')
if dimension_size == 1:
if len(operand_stack) > 3: # PLY carry on the first next ID on the operand stack, but we dont need this right now
aux = operand_stack.pop() # So we are going to ignore it for a bit
elif dimension_size == 2:
if len(operand_stack) > 4: # PLY carry on the first next ID on the operand stack, but we dont need this right now
aux = operand_stack.pop() # So we are going to ignore it for a bit
re = operand_stack.pop(0)
id1 = operand_stack.pop()
square_obj.square('=', id1, '_', re)
to_add_value = dimensions_dict[line_number]
num = square_obj.get_num() - 1
prev_value = square_obj.quadruple_dict[num].pop()
new_value = prev_value + to_add_value
square_obj.quadruple_dict[num].append(new_value) # We add the new dimension to the variable
for _ in range(new_value.count('[')): # We need to remove the extra operands
operand_stack.pop()
if aux:
operand_stack.append(aux)
def p_dimensionoperation(p):
""" dimensionoperation : ID dimensions PLUS ID dimensions
| ID dimensions MINUS ID dimensions
| ID dimensions TIMES ID dimensions
| ID dimensions DIVISION ID dimensions
| ID dimensions LESSTHAN ID dimensions
| ID dimensions GREATERTHAN ID dimensions
| ID dimensions EQUALS ID dimensions
| ID dimensions NOTEQUAL ID dimensions
| ID dimensions logicexp ID dimensions
"""
# adding dimension to array variables in quadruples
line_number = str(p.slice[1].__dict__['lineno'])
operation = p.slice[3].__dict__['value']
if line_number in dimensions_dict:
if len(operand_stack) > 4:
dim2_oper2 = operand_stack.pop()
dim1_oper2 = operand_stack.pop()
oper2 = operand_stack.pop()
oper2 = oper2+'['+dim1_oper2+']'+'['+dim2_oper2+']'
dim2_oper1 = operand_stack.pop()
dim1_oper1 = operand_stack.pop()
oper1 = operand_stack.pop()
oper1 = oper1+'['+dim1_oper1+']'+'['+dim2_oper1+']'
else:
dim1_oper2 = operand_stack.pop()
oper2 = operand_stack.pop()
oper2 = oper2+'['+dim1_oper2+']'
dim1_oper1 = operand_stack.pop()
oper1 = operand_stack.pop()
oper1 = oper1+'['+dim1_oper1+']'
temporal = avail.pop(0)
operand_stack.append(temporal)
avail_dict[temporal] = ""
square_obj.square(operation, oper1, oper2, temporal)
def p_swapdimension(p):
""" swapdimension : ID dimensions ASSIGN ID dimensions SEMICOLON
"""
line_number = str(p.slice[1].__dict__['lineno'])
operation = p.slice[3].__dict__['value']
if line_number in dimensions_dict:
oper1 = operand_stack.pop(0)
dim1_oper1 = operand_stack.pop(0)
oper1 = oper1+'['+dim1_oper1+']'
oper2 = operand_stack.pop(0)
dim1_oper2 = operand_stack.pop(0)
oper2 = oper2+'['+dim1_oper2+']'
square_obj.square('=', oper2, '_', oper1)
def p_incrementdecrement(p):
""" incrementdecrement : selectid PLUSPLUS SEMICOLON
| selectid MINUSMINUS SEMICOLON
"""
if p[2] == '++':
quadruple.generate_quadruple_inc_dec(p, square_obj, operand_stack)
elif p[2] == '--':
quadruple.generate_quadruple_inc_dec(p, square_obj, operand_stack)
def p_selectid(p):
""" selectid : ID
| ID dimensions
"""
def p_conditional(p):
""" conditional : IF LPAREN logicexp RPAREN ckp_if1 LBRACE severalstatutes RBRACE ckp_if3
| IF LPAREN logicexp RPAREN ckp_if1 LBRACE severalstatutes RBRACE elsecase
"""
def p_elsecase(p):
""" elsecase : ELSE ckp_if2 LBRACE severalstatutes RBRACE ckp_if3
"""
def p_ckp_if1(p):
""" ckp_if1 : empty
"""
re = operand_stack.pop()
square_obj.square('gotoF', re, '_', '_')
jump_stack.append(square_obj.get_num() - 1)
def p_ckp_if2(p):
""" ckp_if2 : empty
"""
dir1 = jump_stack.pop()
square_obj.square('goto', '_', '_', '_')
num = square_obj.get_num()
square_obj.quadruple_dict[dir1].pop()
square_obj.quadruple_dict[dir1].append(num)
jump_stack.append(num - 1)
def p_ckp_if3(p):
""" ckp_if3 : empty
"""
dir1 = jump_stack.pop()
num = square_obj.get_num()
square_obj.quadruple_dict[dir1].pop()
square_obj.quadruple_dict[dir1].append(num)
def p_cyclical(p):
""" cyclical : DO ckp_dowhile1 LBRACE severalstatutes RBRACE WHILE LPAREN logicexp RPAREN ckp_dowhile2
| WHILE ckp_while1 LPAREN logicexp RPAREN ckp_while2 LBRACE severalstatutes RBRACE ckp_while3
| FOR LPAREN for1section SEMICOLON for2section SEMICOLON for3section RPAREN LBRACE severalstatutes RBRACE for4section
"""
def p_for1section(p):
""" for1section : ID ASSIGN CONST
| ID ASSIGN ID
"""
id_name = p.slice[1].__dict__['value']
operation = p.slice[2].__dict__['value']
value = p.slice[3].__dict__['value']
square_obj.square(operation, value, '_', id_name)
operand_stack.pop()
operand_stack.pop() # We're creating or own quadruple here, so we must remove the operands from the stack
def p_for2section(p):
""" for2section : forlogicfunction
"""
def p_for3section(p):
""" for3section : ID PLUSPLUS
| ID MINUSMINUS
| for3_1section
"""
if str(p.slice[1]) != "for3_1section":
if str(p.slice[2].__dict__['value']) == '++' or str(p.slice[2].__dict__['value']) == '--':
id_name = p[1]
operation = p[2]
# for_avail_num = len(for_stack)
temporal = avail.pop(0) # Tr = Temporal avail
avail_dict[temporal] = ""
for_stack.append(('=', temporal, '_', id_name))
for_stack.append((operation, id_name, '1', temporal))
# for_stack.append(('=', 'TF'+str(for_avail_num), '_', id_name))
# for_stack.append((operation, id_name, '1', 'TF'+str(for_avail_num)))
operand_stack.pop() # We're creating or own quadruple here, so we must remove the operands from the stack
def p_for3_1section(p):
""" for3_1section : ID PLUS CONST
| ID MINUS CONST
| ID TIMES CONST
| ID DIVISION CONST
"""
id_name = p.slice[1].__dict__['value']
operation = p.slice[2].__dict__['value']
value = p.slice[3].__dict__['value']
temporal = avail.pop(0) # Tr = Temporal avail
avail_dict[temporal] = ""
for_stack.append(('=', temporal, '_', id_name))
for_stack.append((operation, value, id_name, temporal))
# for_avail_num = len(for_stack)
# for_stack.append(('=', 'TF'+str(for_avail_num), '_', id_name))
# for_stack.append((operation, value, id_name, 'TF'+str(for_avail_num)))
operand_stack.pop()
operand_stack.pop() # We're creating or own quadruple here, so we must remove the operands from the stack
def p_for4section(p):
""" for4section : empty
"""
dir1 = jump_stack.pop()
num = square_obj.get_num() + 3 # Plus 3 because of the next two quadruples we're going to add and the extra 1
# because of the instruction
op_code, operand1, operand2, result = for_stack.pop()
square_obj.square(op_code, operand1, operand2, result)
op_code, operand1, operand2, result = for_stack.pop()
square_obj.square(op_code, operand1, operand2, result)
square_obj.quadruple_dict[dir1 + 1].pop()
square_obj.quadruple_dict[dir1 + 1].append(num)
square_obj.square("gotoFor", '_', '_', dir1)
def p_ckp_while1(p):
""" ckp_while1 : empty
"""
jump_stack.append(square_obj.get_num())
def p_ckp_while2(p):
""" ckp_while2 : empty
"""
re = operand_stack.pop()
square_obj.square('gotoF', re, '_', '_')
jump_stack.append(square_obj.get_num() - 1)
def p_ckp_while3(p):
""" ckp_while3 : empty
"""
dir1 = jump_stack.pop()
dir2 = jump_stack.pop()
square_obj.square('goto', '_', '_', str(dir2))
num = square_obj.get_num()
square_obj.quadruple_dict[dir1].pop()
square_obj.quadruple_dict[dir1].append(num)
def p_ckp_dowhile1(p):
""" ckp_dowhile1 : empty
"""
jump_stack.append(square_obj.get_num())
def p_ckp_dowhile2(p):
""" ckp_dowhile2 : empty
"""
re = operand_stack.pop()
dir1 = jump_stack.pop()
square_obj.square('gotoT', re, '_', dir1)
def p_variables(p):
""" variables : type ID SEMICOLON variables
| type ID dimensions SEMICOLON variables
| empty
"""
if str(p.slice[1]) == 'type':
operand_stack.pop() # These are not operands, we need to remove them at the beginning
def p_dimensions(p):
""" dimensions : LBRACKET CONST RBRACKET dimensions
| LBRACKET CONST RBRACKET
| LBRACKET ID RBRACKET dimensions
| LBRACKET ID RBRACKET
"""
line_number = str(p.slice[1].__dict__['lineno'])
value = str(p.slice[2].__dict__['value'])
if line_number in dimensions_dict:
prev_value = dimensions_dict[line_number]
dimensions_dict[line_number] = '[' + value + ']' + prev_value
else:
dimensions_dict[line_number] = '[' + value + ']'
def p_type(p):
""" type : INT
| DOUBLE
"""
p_dict = p.slice[1].__dict__
var_type = p_dict['value']
if var_type == 'int':
int_set.add(p_dict['lineno'])
elif var_type == 'double':
double_set.add(p_dict['lineno'])
def p_methods(p):
""" methods : METHOD ID LPAREN RPAREN LBRACE severalstatutes RBRACE return_method methods
| empty
"""
def p_return_method(p):
""" return_method : empty
"""
square_obj.square('Return', '.', '.', '.')
def p_calling(p):
""" calling : CALL ID LPAREN RPAREN SEMICOLON
"""
method_name = p.slice[2].__dict__['value']
square_obj.square('call', method_name, '_', procedure_directory[method_name])
operand_stack.pop(0)
def p_readwrite(p):
""" readwrite : READ LPAREN idreadingloop RPAREN SEMICOLON
| WRITE LPAREN writecontent RPAREN SEMICOLON
| WRITE LPAREN STRING RPAREN SEMICOLON
"""
# while(operand_stack):
id_name = operand_stack.pop(0)
operation = p[1]
square_obj.square(operation, '_', '_', id_name)
# X : A X
# X : A
def p_idreadingloop(p):
""" idreadingloop : ID COMA idreadingloop
| ID
"""
# to print messages is missing
def p_writecontent(p):
""" writecontent : arithfunction
| logicfunction
"""
def p_arithexp(p):
""" arithexp : arithterm
| arithterm PLUS arithexp
| arithterm MINUS arithexp
"""
if len(p) > 2:
if str(p[2]) in ('plus', '-'):
quadruple.generate_quadruple(p, square_obj, operand_stack)
def p_arithterm(p):
""" arithterm : arithfunction
| arithfunction TIMES arithterm
| arithfunction DIVISION arithterm
| arithfunction MOD arithterm
"""
if len(p) > 2:
if str(p[2]) in ('*', '/', '%'):
quadruple.generate_quadruple(p, square_obj, operand_stack)
def p_arithfunction(p):
""" arithfunction : idconst
| dimensionoperation
| LPAREN arithexp RPAREN
"""
def p_logicexp(p):
""" logicexp : logicterm
| logicterm OR logicexp
"""
if len(p) > 2:
if str(p[2]) == 'or': # ONLY FOR /
quadruple.generate_quadruple(p, square_obj, operand_stack)
def p_logicterm(p):
""" logicterm : logicfunction
| logicfunction AND logicterm
"""
if len(p) > 2:
if str(p[2]) == 'and': # ONLY FOR /
quadruple.generate_quadruple(p, square_obj, operand_stack)
def p_logicfunction(p):
""" logicfunction : idconst LESSTHAN idconst
| idconst GREATERTHAN idconst
| idconst EQUALS idconst
| idconst NOTEQUAL idconst
| LPAREN logicexp RPAREN
| dimensionoperation
"""
if len(p) > 2:
if str(p[2]) in ('<', '>', '==', '!='):
quadruple.generate_quadruple(p, square_obj, operand_stack)
def p_forlogicfunction(p):
""" forlogicfunction : idconst LESSTHAN idconst
| idconst GREATERTHAN idconst
| idconst EQUALS idconst
| idconst NOTEQUAL idconst
| LPAREN logicexp RPAREN
"""
num = square_obj.get_num()
jump_stack.append(num)
quadruple.generate_quadruple_for(p, square_obj, operand_stack)
def p_idconst(p):
""" idconst : ID
| CONST
"""
def p_empty (p):
""" empty :
"""
# Error rule for syntax errors
def p_error(p):
print("Syntax error in input!")
#raise RuntimeError("Syntax error in input!")
def get_parser():
"""
:return: Parser object
"""
parser = yacc.yacc()
return parser