-
Notifications
You must be signed in to change notification settings - Fork 0
/
GramaticaIvan.py
347 lines (278 loc) · 7.97 KB
/
GramaticaIvan.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
import sys
sys.path.append('../..')
from sly import Lexer, Parser
#usar $ tanto para & como para *
tabla = {} #Dictionary
labels = 0
class Tabla():
contador = 0
global tabla
def insertVar(self, var, value):
if var in tabla:
raise NameError('Variable ' + "\"" + var + "\"" + ' declarada anteriormente')
else:
self.contador -= 4
tabla[var] = [self.contador, value,]
def insertValue(self, var, value):
if var in tabla:
tabla[var][1] = value #Modificamos el campo valor de la variable.
else:
raise NameError('Variable ' + "\"" + var + "\"" + 'no declarada')
def compVar(self, var):
if var in tabla:
return 't'
else:
return 'f'
def cadena(self, var):
if var in tabla:
res = str(tabla[var][0]) + "(%ebp)"
else:
res = "$" + str(var)
return res
manejador = Tabla()
def incrementLabel():
global labels
labels = labels + 1
print('L' + str(labels) + ':')
class NodoVariable():
def __init__(self):
print("subl $4, %esp")
class NodoAsign():
def __init__(self, variable, valor):
if valor == None:
print("movl %eax, " + str(tabla[variable][0]) + "(%ebp)")
else:
print("movl $" + str(valor) + ", " + str(tabla[variable][0]) + "(%ebp)")
class NodoSuma():
def __init__(self, valor1, valor2):
#Vamos a la tabla para la comprobacion de tipos.
str1 = manejador.cadena(valor1)#[0]
str2 = manejador.cadena(valor2)#[0]
print("movl " + str1 + ", %eax")
print("addl " + str2 + ", %eax")
#print("movl %eax, " + str1)
class NodoProducto():
def __init__(self, valor1, valor2):
str1 = manejador.cadena(valor1)
str2 = manejador.cadena(valor2)
print("movl " + str1 + ", %eax")
print("addl " + str2 + ", %eax")
print("movl %eax, " + str1)
class NodoEqual():
def __init__(self, valor1, valor2):
print("operador ==")
class NodoNequal():
def __init__(self, valor1, valor2):
print("operador !=")
class NodoGreater():
def __init__(self, valor1, valor2):
print("operador >")
class CalcLexer(Lexer):
tokens = {ID, TIPO, NUM, PLUS, MINUS, TIMES, DIVIDE, ASSIGN, LPAREN, RPAREN, EQUAL, NEQUAL ,GREATER,
IF, ELSE, WHILE, LKEY, RKEY, COMA, END, LESS}
ignore = ' \t'
# Tokens
ID = r'[a-zA-Z_][a-zA-Z0-9_]*'
ID['int'] = TIPO
ID['if'] = IF
ID['else'] = ELSE
ID['while'] = WHILE
NUM = r'\d+'
#Aritmetic
PLUS = r'\+'
MINUS = r'-'
TIMES = r'\*'
DIVIDE = r'/'
GREATER = r'>'
LESS = r'<'
EQUAL = r'=='
NEQUAL = r'!='
ASSIGN = r'='
LPAREN = r'\('
RPAREN = r'\)'
LKEY = r'{'
RKEY = r'}'
COMA = r','
END = r';'
# Ignored pattern
ignore_newline = r'\n+'
# Extra action for newlines
def ignore_newline(self, t):
self.lineno += t.value.count('\n')
def error(self, t):
print("Illegal character '%s'" % t.value[0])
self.index += 1
class CalcParser(Parser):
tokens = CalcLexer.tokens
#manejador = Tabla()
#precedence = (
# ('left', PLUS, MINUS),
# ('left', TIMES, DIVIDE),
# ('right', UMINUS)
# )
def __init__(self):
self.names = { }
@_('f_linea instruction')
def entrada(self,p):
pass
@_('statement')
def instruction(self, p):
pass
@_('endwhile')#('WHILE LPAREN logic RPAREN LKEY statement RKEY')
def instruction(self, p):
print('Pasando por instruction')
pass
@_('middle statement RKEY')
def endwhile(self, p):
print('pasando por endwhile')
pass
@_('middle_logic RPAREN LKEY')
def middle(self, p):
print('pasando por middle')
pass
@_('begin_while LPAREN logic')
def middle_logic(self, p):
print('pasando por middle_logic')
pass
@_('WHILE LPAREN')
def begin_while(self, p):
#print('pasando por begin_while')
incrementLabel()
pass
#Comienzo del bucle while
@_('endif endelse')
def instruction(self,p):
print("Pasando por endif endelse")
@_('middle_if statement RKEY')
def endif(self,p):
print("Pasando por middleif statement RKEY")
@_('middle_if_logic LKEY')
def middle_if(self,p):
print("Pasando por middle_if_logic LKEY")
@_('begin_if logic RPAREN')
def middle_if_logic(self,p):
print("Pasando por begin_if logic RPAREN")
@_('IF LPAREN')
def begin_if(self,p):
print("Pasando por IF LPAREN")
@_('middle_else RKEY')
def endelse(self,p):
print("Pasa por middleelse RKEY")
@_('')
def endelse(self,p):
print("No hay else")
@_('begin_else statement')
def middle_else(self,p):
print("Pasa por begin_else statement")
@_('ELSE LKEY')
def begin_else(self,p):
print("Paso por ELSE LKEY")
@_('TIPO linea END f_linea')
def f_linea(self, p):
#print('Instruccion correcta')
pass
@_('')
def f_linea(self, p):
pass
@_('assig rest')
def linea(self, p):
pass
#manejador.insertVar(p.ID) #Aquí almacenamos la variable.
@_('ID rest')
def linea(self, p):
manejador.insertVar(p.ID, 0)
NodoVariable()
@_('ID ASSIGN logic')
def assig(self,p):
manejador.insertVar(p.ID, p.logic)
NodoVariable()
NodoAsign(p.ID, p.logic)
#print(tabla[p.ID])
@_(' ')
def assig(self, p):
pass
@_('COMA linea')
def rest(self, p):
pass
@_(' ')
def rest(self,p):
pass
@_('ID ASSIGN logic END statement')
def statement(self, p): #Falta buscar el valor en el sistema
manejador.insertValue(p.ID, p.logic)
NodoAsign(p.ID, p.logic)
#print('pasando por statement')
#tabla[p.ID][1] = p.logic
#print(tabla[p.ID])
@_('logic')
def statement(self,p):
return p.logic
@_(' ')
def statement(self,p):
pass
@_('logic EQUAL logicpr')
def logic(self, p):
NodoEqual(p.logic, p.logicpr)
@_('logic NEQUAL logicpr')
def logic(self, p):
NodoNequal(p.logic, p.logicpr)
#print('pasando por !=')
@_('logicpr')
def logic(self,p):
return p.logicpr
@_('logicpr GREATER expr')
def logicpr(self, p):
NodoGreater(p.logicpr, p.expr)
@_('logicpr LESS expr')
def logicpr(self, p):
NodoLess(p.logicpr, p.expr)
@_('expr')
def logicpr(self, p):
return p.expr
@_('expr PLUS fact')
def expr(self, p):
NodoSuma(p.expr,p.fact)
@_('expr MINUS fact')
def expr(self, p):
a = NodoResta(p.expr, p.fact)
return a.result #p.expr0 - p.expr1
@_('fact')
def expr(self, p):
return p.fact
@_('fact TIMES basico')
def fact(self, p):
a = NodoProducto(p.fact, p.basico)
#return a.result #p.expr0 * p.expr1
@_('fact DIVIDE basico')
def fact(self, p):
a = fact(p.fact, p.basico)
return a.result #p.expr0 / p.expr1
@_('basico')
def fact(self, p):
return p.basico
@_('MINUS basico')
def basico(self, p):
return -p.basico
@_('ID') #Uso de la variable
def basico(self, p): #Si el id no esta en la tabla, debe mostrar error.
return p.ID
@_('NUM')
def basico(self, p):
#print("NUM = ", p.NUM)
return int(p.NUM)
@_('LPAREN expr RPAREN')
def basico(self, p):
return p.expr
if __name__ == '__main__':
lexer = CalcLexer()
parser = CalcParser()
while True:
try:
text = input('calc > ')
except EOFError:
break
if text:
parser.parse(lexer.tokenize(text))
#entrada -> int a = 3, b = 4; while( a != 3) { a = a + 1; }
#entrada -> int a = 3, b =4; a = a +b;
#Problemas: La insercion de variables en la pila se hace al reves.