-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathanalizador_sintactico.py
181 lines (156 loc) · 4.87 KB
/
analizador_sintactico.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
import ply.yacc as yacc
from analizador_lexico import tokens
from analizador_lexico import analizador
# resultado del analisis
resultado_gramatica = []
precedence = (
('right','ASIGNAR'),
('left', 'SUMA', 'RESTA'),
('left', 'MULT', 'DIV'),
('right', 'UMINUS'),
)
nombres = {}
def p_declaracion_asignar(t):
'declaracion : IDENTIFICADOR ASIGNAR expresion PUNTOCOMA'
nombres[t[1]] = t[3]
def p_declaracion_expr(t):
'declaracion : expresion'
# print("Resultado: " + str(t[1]))
t[0] = t[1]
def p_expresion_operaciones(t):
'''
expresion : expresion SUMA expresion
| expresion RESTA expresion
| expresion MULT expresion
| expresion DIV expresion
| expresion POTENCIA expresion
| expresion MODULO expresion
'''
if t[2] == '+':
t[0] = t[1] + t[3]
elif t[2] == '-':
t[0] = t[1] - t[3]
elif t[2] == '*':
t[0] = t[1] * t[3]
elif t[2] == '/':
t[0] = t[1] / t[3]
elif t[2] == '%':
t[0] = t[1] % t[3]
elif t[2] == '**':
i = t[3]
t[0] = t[1]
while i > 1:
t[0] *= t[1]
i -= 1
def p_expresion_uminus(t):
'expresion : RESTA expresion %prec UMINUS'
t[0] = -t[2]
def p_expresion_grupo(t):
'''
expresion : PARIZQ expresion PARDER
| LLAIZQ expresion LLADER
| CORIZQ expresion CORDER
'''
t[0] = t[2]
# sintactico de expresiones logicas
def p_expresion_logicas(t):
'''
expresion : expresion MENORQUE expresion
| expresion MAYORQUE expresion
| expresion MENORIGUAL expresion
| expresion MAYORIGUAL expresion
| expresion IGUAL expresion
| expresion DISTINTO expresion
| PARIZQ expresion PARDER MENORQUE PARIZQ expresion PARDER
| PARIZQ expresion PARDER MAYORQUE PARIZQ expresion PARDER
| PARIZQ expresion PARDER MENORIGUAL PARIZQ expresion PARDER
| PARIZQ expresion PARDER MAYORIGUAL PARIZQ expresion PARDER
| PARIZQ expresion PARDER IGUAL PARIZQ expresion PARDER
| PARIZQ expresion PARDER DISTINTO PARIZQ expresion PARDER
'''
if t[2] == "<": t[0] = t[1] < t[3]
elif t[2] == ">": t[0] = t[1] > t[3]
elif t[2] == "<=": t[0] = t[1] <= t[3]
elif t[2] == ">=": t[0] = t[1] >= t[3]
elif t[2] == "==": t[0] = t[1] is t[3]
elif t[2] == "!=": t[0] = t[1] != t[3]
elif t[3] == "<":
t[0] = t[2] < t[4]
elif t[2] == ">":
t[0] = t[2] > t[4]
elif t[3] == "<=":
t[0] = t[2] <= t[4]
elif t[3] == ">=":
t[0] = t[2] >= t[4]
elif t[3] == "==":
t[0] = t[2] is t[4]
elif t[3] == "!=":
t[0] = t[2] != t[4]
# print('logica ',[x for x in t])
# gramatica de expresiones booleanadas
def p_expresion_booleana(t):
'''
expresion : expresion AND expresion
| expresion OR expresion
| expresion NOT expresion
| PARIZQ expresion AND expresion PARDER
| PARIZQ expresion OR expresion PARDER
| PARIZQ expresion NOT expresion PARDER
'''
if t[2] == "&&":
t[0] = t[1] and t[3]
elif t[2] == "||":
t[0] = t[1] or t[3]
elif t[2] == "!":
t[0] = t[1] is not t[3]
elif t[3] == "&&":
t[0] = t[2] and t[4]
elif t[3] == "||":
t[0] = t[2] or t[4]
elif t[3] == "!":
t[0] = t[2] is not t[4]
def p_expresion_numero(t):
'expresion : ENTERO'
t[0] = t[1]
def p_expresion_cadena(t):
'expresion : COMDOB expresion COMDOB'
t[0] = t[2]
def p_expresion_nombre(t):
'expresion : IDENTIFICADOR'
try:
t[0] = nombres[t[1]]
except LookupError:
print("Nombre desconocido ", t[1])
t[0] = 0
def p_error(t):
global resultado_gramatica
if t:
resultado = "Error sintactico de tipo {} en el valor {}".format( str(t.type),str(t.value))
print(resultado)
else:
resultado = "Error sintactico {}".format(t)
print(resultado)
resultado_gramatica.append(resultado)
# instanciamos el analizador sistactico
parser = yacc.yacc()
def prueba_sintactica(data):
global resultado_gramatica
resultado_gramatica.clear()
for item in data.splitlines():
if item:
gram = parser.parse(item)
if gram:
resultado_gramatica.append(str(gram))
else: print("data vacia")
print("result: ", resultado_gramatica)
return resultado_gramatica
if __name__ == '__main__':
while True:
try:
s = input(' ingresa dato >>> ')
except EOFError:
continue
if not s: continue
# gram = parser.parse(s)
# print("Resultado ", gram)
prueba_sintactica(s)