-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcompile_run.py
More file actions
1162 lines (1006 loc) · 22.1 KB
/
Copy pathcompile_run.py
File metadata and controls
1162 lines (1006 loc) · 22.1 KB
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
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import ply.lex as lex
import ply.yacc as yacc
import sys
import os
import numpy as np
from SymbolTable import SymbolTable
from execute_f import execute
#from LexDefinition import *
"""
Paul Vazquez A00819877
"""
fileName = 'variables_simples.cpp'
## ---------------- SymbolTable ---------------
SymbolTable = SymbolTable()
lista_var = []
value= None
## ---------------- Cuadruple ---------------
pila_operandos = []
pila_operadores = []
pila_saltos = []
contador_T = 0
contador_cuadruplos = 0
cuadruplos = []
func_id = ""
line_number = -1
def debug(ele):
output=False
if(output==True):
print(ele)
def get_value(value):
if(is_number(value)):
return value
element = SymbolTable.lookup(value)
if element is None:
raise Exception('variable {} not previously declared '.format(value))
return element.value
def is_number(s):
try:
float(s)
return True
except ValueError:
return False
except TypeError:
return False
def print_cuadruplos():
f = open("cuadruplos.txt", "w")
f.write("")
f.close()
f = open("cuadruplos.txt", "a")
for index,element in enumerate(cuadruplos):
debug(str(index)+" --> "+str(element))
f.write(str(index)+" --> "+str(element)+"\n")
f.close()
def fill(dir1,cont):
debug("Relleno el espacio en la direccion: " + str(dir1)+" con "+str(cont))
global cuadruplos
position_fill = cuadruplos[dir1]
# Last position
cuadruplos[dir1][-1] = str(cont)
## ---------------- MATRIX ---------------
def initialize_list(n):
for i in range(0,n):
return [0]*n
def recursive_generation(depth,lista,stack_dimensions):
if(depth>0):
number_elements= stack_dimensions.pop(0)
new_lista = initialize_list(number_elements)
for index,ele in enumerate(lista):
lista[index] = new_lista
lista = lista[0]
depth= depth-1
recursive_generation(depth,lista,stack_dimensions)
return lista
def print_lista_arrays():
for index,ele in enumerate(list_arrays):
debug("Lista en la posicion " + str(index))
debug(ele)
def is_array(id):
ele = SymbolTable.lookup(id)
if(ele is None):
raise Exception('variable {} not previously declared '.format(id))
if(ele.tipo[0] =="["):
return True
return False
list_arrays = []
stack_dimension = []
global_dimension = 0
tamano = 1
list_indexes = []
stack_list_indexes = []
## ---------------- LEXER ---------------
tokens = (
#SPECIAL CHARACTERS
"OPEN_BRACES", #{
"CLOSING_BRACES",#}
"COMA",#,
"SEMICOLON", #;
"OPEN_PARENTH", #(
"CLOSING_PARENTH", #)
"OPEN_BRACKET",#[
"CLOSING_BRACKET",#]
"COMMENT", #//
#COMPARATION OPERATORS
"EQUAL_EQUAL", #==
"NOT_EQUAL",#!=
"BIGGER_THAN",#>
"LOWER_THAN",#<
##ARITHMETIC OPERATORS
"EQUAL",#=
"PLUS",#+
"MINUS",#-
"MULTIPLY",#*
"DIVISION",#/
"PLUSPLUS", #++
"MINUSMINUS",#--
##BOOLEAN OPERATORS
"OR",#||
"AND",#&&
"NOT",#!
##RESERVED WORDS
"ID",#id
"PRINT",#print
"READ",#read
"IF",#if
"ELSE",#else
"WHILE",#while
"DO",#do
"FOR",#for
"FUNCTION",#function
"MAIN", #main
##NUMERIC TYPES
"INT", #int
"DOUBLE", #double
"CONSTANT", #digito+
#string
"STRING",#"(.)*"
)
precedence = (
('left', 'PLUS', 'MINUS'),
)
#ignorar espacios
t_ignore = '\t | \n '
def t_COMMENT(t):
r'(/\*(.|\n)*?\*/)|(//.*)'
pass
# No return value. Token discarded
def t_OPEN_BRACES(t):
r'{'
return t
def t_CLOSING_BRACES(t):
r'}'
return t
def t_COMA(t):
r'\,'
return t
def t_SEMICOLON(t):
r'\;'
return t
def t_OPEN_PARENTH(t):
r'\('
return t
def t_CLOSING_PARENTH(t):
r'\)'
return t
def t_OPEN_BRACKET(t):
r'\['
return t
def t_CLOSING_BRACKET(t):
r'\]'
return t
#COMPARATION
def t_EQUAL_EQUAL(t):
r'=='
return t
def t_NOT_EQUAL(t):
r'!='
return t
def t_BIGGER_THAN(t):
r'>'
return t
def t_LOWER_THAN(t):
r'<'
return t
#ARITMETHIC
def t_EQUAL(t):
r'\='
return t
def t_PLUSPLUS(t):
r'\+\+'
return t
def t_MINUSMINUS(t):
r'\-\-'
return t
def t_PLUS(t):
r'\+'
return t
def t_MINUS(t):
r'\-'
return t
def t_MULTIPLY(t):
r'\*'
return t
def t_DIVISION(t):
r'\/'
return t
#BOOLEAN OPERATOR
def t_OR(t):
r'\|\|'
return t
def t_AND(t):
r'&&'
return t
def t_NOT(t):
r'!'
return t
#RESERVED WORDS
def t_PRINT(t):
r'print'
return t
def t_READ(t):
r'read'
return t
def t_IF(t):
r'if'
return t
def t_ELSE(t):
r'else'
return t
def t_WHILE(t):
r'while'
return t
def t_DO(t):
r'\bdo\b'
return t
def t_FOR(t):
r'for'
return t
def t_FUNCTION(t):
r'function'
return t
def t_MAIN(t):
r'main'
return t
##NUMERIC TYPES
def t_INT(t):
r'int'
return t
def t_DOUBLE(t):
r'double'
return t
def t_CONSTANT(t):
r'[-+]?[0-9]+[.]?[0-9]*'
return t
def t_ID(t):
r'(([a-z]+[A-Z]*[0-9]*)+)'
return t
def t_STRING(t):
r'"((?!").)*"'
return t
#en caso de error
def t_error(t):
debug('Error de lexico: '+str(t))
t.lexer.skip(1)
##----------------PARSER-----------------------
def p_PROGRAMA(p):
'''
PROGRAMA : VAR FUNC M
'''
# Generar cuadruplo de fin de programa
global cuadruplos
# Generar cuadruplo RETURN
local_cuad = []
local_cuad.append("END")
cuadruplos.append(local_cuad)
#
debug("END ")
def p_VAR(p):
'''
VAR : TIPO DECLARE SEMICOLON
| TIPO MAT SEMICOLON
| VAR TIPO DECLARE SEMICOLON
| VAR TIPO MAT SEMICOLON
|
'''
tipo=""
size = len(p)
global lista_var
global stack_dimension
global tamano
global stack_list_indexes
dimension = 0
list_indexes=[]
if(len(stack_dimension)>0):
dimension = stack_dimension.pop()
list_indexes = stack_list_indexes.pop()
if(dimension>0):
##declaration of n-dimensional array
debug("n-dimentional_array")
debug("--------------------------")
debug("tamano " + str(tamano))
debug('dimension' + str(dimension))
debug("indexes" + str(list_indexes))
debug("id:" + str(p[3]))
debug("--------------------------")
if(size==5):
id = p[3]
tipo = p[2]
else:
id = p[2]
tipo = p[1]
##declare in SymbolTable
position_in_lista_arrays = len(list_arrays)
SymbolTable.insert(id=id,tipo='['+p[2],attributes=position_in_lista_arrays)
##initialize in lista_arrays
lista_local=[None]
list_indexes = list(map(int, list_indexes))
list_arrays.append(recursive_generation(int(dimension),lista_local,list_indexes))
else:
if(size == 4):
tipo = p[1]
#debug("tipo " + p[1])
elif(size ==5):
tipo = p[2]
for element in lista_var:
SymbolTable.insert(id=element, tipo=tipo, attributes=0.0)
lista_var.clear()
tamano = 1
list_indexes = []
def p_DECLARE(p):
'''
DECLARE : ID
| DECLARE COMA ID
| ASSIGN
| DECLARE COMA ASSIGN
'''
size = len(p)
global value
global lista_var
#debug("size en declare" + str(size))
if(size == 2):
#debug('En declare' + str(p[1]))
element = p[1]
lista_var.append(element)
elif(size == 4):
element = p[3]
lista_var.append(element)
def p_ASSIGN(p):
'''
ASSIGN : ID EQUAL EA
| MAT EQUAL EA
'''
global lista_var
global cuadruplos
global stack_dimension
global stack_list_indexes
dimension = 0
if(len(stack_dimension)>0):
dimension = stack_dimension.pop()
#Cuadruplo de asignacion
ea = pila_operandos.pop()
local_cuad = []
local_cuad.append('=')
local_cuad.append(str(ea))
local_cuad.append('$')
debug("ASSIGN" + p[1]+" " + str(dimension))
## And is of type mat
is_arr = is_array(p[1])
if(dimension>0 and is_arr):
debug("ASSIGN TO MAT")
debug(stack_list_indexes)
list_indexes = stack_list_indexes.pop()
indexes = '-'.join([str(elem) for elem in list_indexes])
local_cuad.append('['+p[1]+'-'+indexes)
dimension=0
else:
lista_var.append(p[1])
local_cuad.append(p[1])
debug('= ' + str(ea) +' $ ' + str(p[1]))
cuadruplos.append(local_cuad)
p[0] = p[1]
def p_FUNC(p):
'''
FUNC : FUNCTION AUX_FUNC SET_ID OPEN_PARENTH CLOSING_PARENTH DECLARE_FUNC OPEN_BRACES S CLOSING_BRACES RETURN IF_AUX3
| FUNC AUX_FUNC FUNCTION SET_ID OPEN_PARENTH CLOSING_PARENTH OPEN_BRACES DECLARE_FUNC S CLOSING_BRACES RETURN IF_AUX3
| empty
'''
def p_SET_ID(p):
'''
SET_ID : ID
'''
global func_id
debug("set_function_id")
func_id = p[1]
def p_DECLARE_FUNC(p):
'''
DECLARE_FUNC : empty
'''
global func_id
global line_number
debug("REgister new function")
debug('FUnc id' + func_id)
debug("line_number" + str(line_number))
SymbolTable.insert(id=func_id, tipo="void", attributes=line_number)
def p_RETURN(p):
'''
RETURN : empty
'''
global cuadruplos
# Generar cuadruplo RETURN
local_cuad = []
local_cuad.append("RETURN")
cuadruplos.append(local_cuad)
#
debug("RETURN " + "____")
def p_AUX_FUNC(p):
'''
AUX_FUNC : empty
'''
global pila_operandos
global cuadruplos
global pila_saltos
global line_number
# Generar cuadruplo
local_cuad = []
local_cuad.append("GOTO")
local_cuad.append("___")
cuadruplos.append(local_cuad)
#
contador_cuadruplos = len(cuadruplos)
pila_saltos.append(contador_cuadruplos-1)
debug("GOTO " + "____")
line_number = contador_cuadruplos
p[0]= contador_cuadruplos
def p_M(p):
'''
M : MAIN OPEN_PARENTH CLOSING_PARENTH OPEN_BRACES S CLOSING_BRACES
'''
def p_TIPO(p):
'''
TIPO : INT
| DOUBLE
'''
if(p[1]=="int"):
p[0]="int"
else:
p[0]="double"
##----------------------STATEMENTS----------------------
def p_S(p):
'''
S : STATEMENTS
| S STATEMENTS
|
'''
def p_STATEMENTS(p):
'''
STATEMENTS : VAR
| IDSTAT
| PRINTSTAT
| READSTAT
| IFSTAT
| WHILESTAT
| DOSTAT
| FORSTAT
| FUNCSTAT
| INC_STAT
|
'''
def p_IDSTAT(p):
'''
IDSTAT : ASSIGN SEMICOLON
'''
def p_PRINTSTAT(p):
'''
PRINTSTAT : PRINT OPEN_PARENTH EA CLOSING_PARENTH SEMICOLON
| PRINT OPEN_PARENTH STRING CLOSING_PARENTH SEMICOLON
'''
global cuadruplos
local_cuad = []
local_cuad.append("PRINT")
debug("EEEEEEEEEEEEEEEEEEEEEE")
debug(p[3])
debug("AAAAAAAAAA")
local_cuad.append(p[3])
cuadruplos.append(local_cuad)
def p_READSTAT(p):
'''
READSTAT : READ OPEN_PARENTH EA CLOSING_PARENTH SEMICOLON
'''
global cuadruplos
local_cuad = []
local_cuad.append("READ")
local_cuad.append(p[3])
cuadruplos.append(local_cuad)
def p_IFSTAT(p):
'''
IFSTAT : IF OPEN_PARENTH EL CLOSING_PARENTH IF_AUX1 IN_S IF_AUX3
| IF OPEN_PARENTH EL CLOSING_PARENTH IF_AUX1 IN_S ELSE IF_AUX2 IN_S IF_AUX3
'''
def p_IF_AUX1(p):
'''
IF_AUX1 : empty
'''
global pila_operandos
global cuadruplos
global pila_saltos
Result_Logic = pila_operandos.pop()
# Generar cuadruplo
local_cuad = []
local_cuad.append("GOTOF")
local_cuad.append(str(Result_Logic))
local_cuad.append("___")
cuadruplos.append(local_cuad)
#
contador_cuadruplos = len(cuadruplos)
pila_saltos.append(contador_cuadruplos-1)
debug("GOTO " + str(Result_Logic) + "____")
def p_IF_AUX2(p):
'''
IF_AUX2 : empty
'''
global pila_operandos
global cuadruplos
global pila_saltos
dir_1 = pila_saltos.pop()
cuadruplos.append(["GOTO","___"])
debug("GOTO " + "____")
contador_cuadruplos = len(cuadruplos)
pila_saltos.append(contador_cuadruplos-1)
fill(dir_1,contador_cuadruplos)
def p_IF_AUX3(p):
'''
IF_AUX3 : empty
'''
global pila_operandos
global cuadruplos
global pila_saltos
dir_1 = pila_saltos.pop()
contador_cuadruplos = len(cuadruplos)
fill(dir_1,contador_cuadruplos)
def p_empty(p):
'empty :'
pass
def p_WHILESTAT(p):
'''
WHILESTAT : WHILE WHILE_AUX_1 OPEN_PARENTH EL CLOSING_PARENTH WHILE_AUX_2 IN_S
'''
global pila_saltos
global cuadruplos
dir1 = pila_saltos.pop()
dir2= pila_saltos.pop()
global cuadruplos
# Generar cuadruplo
local_cuad = []
local_cuad.append("GOTO")
local_cuad.append(str(dir2))
cuadruplos.append(local_cuad)
#
fill(dir1,len(cuadruplos))
def p_WHILE_AUX_1(p):
'''
WHILE_AUX_1 : empty
'''
global pila_saltos
global cuadruplos
pila_saltos.append(len(cuadruplos))
def p_WHILE_AUX_2(p):
'''
WHILE_AUX_2 : empty
'''
global pila_operandos
global cuadruplos
Result_Logic = pila_operandos.pop()
# Generar cuadruplo
local_cuad = []
local_cuad.append("GOTOF")
local_cuad.append(str(Result_Logic))
local_cuad.append("___")
cuadruplos.append(local_cuad)
#
contador_cuadruplos = len(cuadruplos)
pila_saltos.append(contador_cuadruplos-1)
debug("GOTOF " + str(Result_Logic) + "____")
def p_DOSTAT(p):
'''
DOSTAT : DO WHILE_AUX_1 IN_S WHILE OPEN_PARENTH EL CLOSING_PARENTH SEMICOLON
'''
global pila_operandos
global cuadruplos
Result_Logic = pila_operandos.pop()
# Generar cuadruplo
local_cuad = []
local_cuad.append("GOTOT")
local_cuad.append(str(Result_Logic))
local_cuad.append("___")
cuadruplos.append(local_cuad)
#
debug("GOTOT " + str(Result_Logic) + "____")
contador_cuadruplos = len(cuadruplos)
fill(contador_cuadruplos-1,pila_saltos.pop())
def p_FORSTAT(p):
'''
FORSTAT : FOR OPEN_PARENTH ASSIGN SEMICOLON WHILE_AUX_1 EL SEMICOLON WHILE_AUX_2 ASSIGN FOR_INCREMENT CLOSING_PARENTH IN_S
'''
global pila_saltos
global cuadruplos
# Meter la asignacion
debug("METO EN FOR ANTES DE LOS SALTOS")
for each in p[10][::-1]:
debug(each)
cuadruplos.append(each)
dir1 = pila_saltos.pop()
dir2= pila_saltos.pop()
# Generar cuadruplo
local_cuad = []
local_cuad.append("GOTO")
local_cuad.append(str(dir2))
cuadruplos.append(local_cuad)
#
fill(dir1,len(cuadruplos))
def p_FOR_INCREMENT(p):
'''
FOR_INCREMENT : empty
'''
global cuadruplos
local_cuad = []
while(cuadruplos[-1][0]!="GOTOF"):
local_cuad.append(cuadruplos.pop())
p[0] = local_cuad
def p_FUNCSTAT(p):
'''
FUNCSTAT : ID OPEN_PARENTH CLOSING_PARENTH SEMICOLON
'''
global pila_saltos
global cuadruplos
id = p[1]
# Generar cuadruplo
local_cuad = []
local_cuad.append("CALL")
# Sacar line_number de la funcion de la SymbolTable
result = SymbolTable.lookup(id=id)
if(result==None):
raise Exception('function "{}" not previously declared '.format(id))
debug(result)
local_cuad.append(result.attributes)
cuadruplos.append(local_cuad)
#
def p_INC_STAT(p):
'''
INC_STAT : ID PLUSPLUS SEMICOLON
| ID MINUSMINUS SEMICOLON
'''
def p_IN_S(p):
'''
IN_S : OPEN_BRACES S CLOSING_BRACES
'''
##----------------------EXPRESIONES----------------------
def p_EA(p):
'''
EA : TA
| EA PLUS TA
| EA MINUS TA
'''
size = len(p)
global contador_T
global pila_operandos
global cuadruplos
local_cuad = []
debug("--------------------EVALUO EA----------------")
#debug(pila_operandos)
if(size==4):
op_1 = pila_operandos.pop()
op_2 = pila_operandos.pop()
# Check if operands in SymbolTable
res = 'T_' + str(contador_T)
if(p[2]=='+'):
local_cuad.append('+')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))
local_cuad.append(str(res))
contador_T=contador_T+1
pila_operandos.append(res)
elif(p[2]=='-'):
local_cuad.append('-')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))
local_cuad.append(str(res))
debug('- ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
else:
debug("Unknown operator")
cuadruplos.append(local_cuad)
else:
debug("REgerso el resultado")
debug(p[1])
p[0]=p[1]
def p_FA(p):
'''
FA : CONSTANT
| ID
| MAT
| OPEN_PARENTH EA CLOSING_PARENTH
'''
#CUADRUPLOS
global pila_operandos
dimension = 0
global stack_dimension
global stack_list_indexes
size = len(p)
if(size == 2):
element = SymbolTable.lookup(p[1])
if(element is not None):
#debug("Variable " +p[1])
if(len(stack_dimension)>0) and is_array(p[1]):
dimension = stack_dimension.pop()
if (dimension>1):
#Is matrix
list_indexes = stack_list_indexes.pop()
debug("indexes" + str(list_indexes))
debug(p[1])
#special format
indexes = '-'.join([str(elem) for elem in list_indexes])
pila_operandos.append('['+element.id+'-'+indexes)
dimension = 0
else:
#normal variable
pila_operandos.append(element.id)
elif(is_number(p[1])):
pila_operandos.append(float(p[1]))
else:
#No existe
#debug("Constant " + p[1])
raise Exception("variable {} no previously declared".format(p[1]))
p[0]=p[1]
def p_TA(p):
'''
TA : FA
| TA MULTIPLY FA
| TA DIVISION FA
'''
# CUADRUPLOS
global contador_T
global pila_operandos
global cuadruplos
local_cuad = []
size = len(p)
if(size==4):
op_1 = pila_operandos.pop()
op_2 = pila_operandos.pop()
res = 'T_' + str(contador_T)
if(p[2]=='*'):
local_cuad.append('*')
local_cuad.append(op_1)
local_cuad.append(op_2)
local_cuad.append(res)
#debug('* ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
elif(p[2]=='/'):
local_cuad.append('/')
local_cuad.append(op_1)
local_cuad.append(op_2)
local_cuad.append(res)
#debug('/ ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
else:
debug("Unknown operator")
cuadruplos.append(local_cuad)
else:
p[0] = p[1]
def p_EL(p):
'''
EL : TL
| EL OR TL
'''
size= len(p)
global contador_T
global pila_operandos
global cuadruplos
local_cuad = []
#debug(pila_operandos)
if(size==4):
op_1 = pila_operandos.pop()
op_2 = pila_operandos.pop()
res = 'T_' + str(contador_T)
if(p[2]=='||'):
local_cuad.append('||')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))
local_cuad.append(str(res))
#debug('|| ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
cuadruplos.append(local_cuad)
else:
debug("Unknown operator")
def p_TL(p):
'''
TL : FL
| TL AND FL
'''
size= len(p)
global contador_T
global pila_operandos
global cuadruplos
local_cuad = []
if(size==4):
op_1 = pila_operandos.pop()
op_2 = pila_operandos.pop()
res = 'T_' + str(contador_T)
if(p[2]=='&&'):
local_cuad.append('&&')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))
local_cuad.append(str(res))
#debug('&& ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
cuadruplos.append(local_cuad)
else:
debug("Unknown operator")
def p_FL(p):
'''
FL : NL OPERATORS NL
| EA OPERATORS EA
| NL
| OPEN_PARENTH EL CLOSING_PARENTH
'''
#Puedo comparar con una expresion aritmetica?
size= len(p)
global contador_T
global pila_operandos
global cuadruplos
local_cuad = []
if(size==4 and p[1]!='('):
op_1 = pila_operandos.pop()
op_2 = pila_operandos.pop()
res = 'T_' + str(contador_T)
if(p[2]=='!='):
local_cuad.append('!=')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))
local_cuad.append(str(res))
#debug('!= ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
elif(p[2]=='=='):
local_cuad.append('==')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))
local_cuad.append(str(res))
#debug('== ' + str(op_1)+ ' ' + str(op_2)+ ' ' + str(res))
pila_operandos.append(res)
contador_T=contador_T+1
elif(p[2]=='>'):
local_cuad.append('>')
local_cuad.append(str(op_1))
local_cuad.append(str(op_2))