-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexLpp.py
206 lines (175 loc) · 5.9 KB
/
lexLpp.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
import ply.lex as lex
class LexicalAnalyzer():
operadores = {
'*' : 'SIGNO_MULT',
'+' : 'SIGNO_SUM',
'-' : 'SIGNO_RES',
'/' : 'SIGNO_DIV',
'%' : 'SIGNO_MOD',
'>=': 'MAYOR_IGUAL',
'<=': 'MENOR_IGUAL',
'?' : 'DIFERENTE',
'=' : 'IGUAL',
'>' : 'MAYOR',
'<' : 'MENOR',
'|' : 'SIGNO_O',
'&' : 'SIGNO_Y',
'{' : 'SIGNO_LLAVE_IZQ',
'}' : 'SIGNO_LLAVE_DER',
'(' : 'SIGNO_PAR_IZQ',
')' : 'SIGNO_PAR_DER',
';' : 'TERMINACION',
':' : 'DOS_PUNTOS',
'.' : 'PUNTO',
',' : 'COMA'
}
categorias = {
'SIGNO_MULT' : 'OPERADORES_ARITMETICOS',
'SIGNO_SUM' : 'OPERADORES_ARITMETICOS',
'SIGNO_RES' : 'OPERADORES_ARITMETICOS',
'SIGNO_DIV' : 'OPERADORES_ARITMETICOS',
'SIGNO_MOD' : 'OPERADORES_ARITMETICOS',
'MAYOR_IGUAL' : 'OPERADORES_RELACIONALES',
'MENOR_IGUAL' : 'OPERADORES_RELACIONALES',
'DIFERENTE' : 'OPERADORES_RELACIONALES',
'IGUAL' : 'OPERADORES_RELACIONALES',
'MAYOR' : 'OPERADORES_RELACIONALES',
'MENOR' : 'OPERADORES_RELACIONALES',
'SIGNO_O' : 'OPERADORES_LOGICOS',
'SIGNO_Y' : 'OPERADORES_LOGICOS',
'SIGNO_LLAVE_IZQ' : 'AGRUPACION',
'SIGNO_LLAVE_DER' : 'AGRUPACION',
'SIGNO_PAR_IZQ' : 'AGRUPACION',
'SIGNO_PAR_DER' : 'AGRUPACION',
'TERMINACION' : 'PUNTUACION',
'DOS_PUNTOS' : 'PUNTUACION',
'PUNTO' : 'PUNTUACION',
'COMA' : 'PUNTUACION',
'TIPO_BOOLEANO' : 'TIPO',
'TIPO_DECIMAL' : 'TIPO',
'TIPO_CADENA' : 'TIPO',
'TIPO_ENTERO' : 'TIPO',
'TIPO_CONSTANTE' : 'TIPO',
'NULO' : 'NULO',
'PROCEDIMIENTO' : 'RESERVADA',
'ENTONCES' : 'RESERVADA',
'MIENTRAS' : 'RESERVADA',
'PEROSI' : 'RESERVADA',
'REGRESA' : 'RESERVADA',
'SINO' : 'RESERVADA',
'POR' : 'RESERVADA',
'SI' : 'RESERVADA',
'EN' : 'RESERVADA',
'VERDADERO' : 'BOOLEANO',
'FALSO' : 'BOOLEANO'
}
tok_reservadas = ['SI','ENTONCES', 'PEROSI', 'SINO', 'MIENTRAS', 'POR', 'EN', 'PROCEDIMIENTO', 'REGRESA', 'FIN']
tok_tipos = ['ENTERO', 'DECIMAL', 'CADENA', 'BOOLEANO','CONSTANTE']
tokens = ['ASIGNACION', 'COMENTARIO', 'BOOLEANO', 'IDENTIFICADOR', 'DECIMAL', 'ENTERO', 'CADENA', 'OPERADOR_RELACIONAL', 'OPERADOR_ARITMETICO', 'OPERADOR_LOGICO', 'SIGNOS_PUNTUACION', 'SIGNOS_AGRUPACION', 'TIPO'] + list(categorias.keys())
def __init__(self):
self.build()
#Caracteres a ignorar (Solo el espacio, por ahora)
t_ignore = r' '
def t_error(self, t):
print(f'ERROR: Caracter Ilegal "{t.value[0]}".')
t.lexer.skip(1)
def t_NEWLINE(self, t):
r'[\r\n]+'
t.lexer.lineno += len(t.value)
def t_ASIGNACION(self, t):
r'->'
t.value = {
'category' : 'ASIGNACION',
'value' : t.value
}
return t
def t_COMENTARIO(self, t):
r'@([^@]|\n)*@'
pass
def t_BOOLEANO(self, t):
r'(VERDADERO|FALSO)'
t.value = {
'category' : self.categorias.get(t.value,t.type),
'value' : t.value
}
return t
def t_IDENTIFICADOR(self, t):
r'[a-zA-Z_][a-zA-Z_0-9]*'
if t.value in self.tok_tipos:
t.type = "TIPO_" + t.value
if t.value in self.tok_reservadas:
t.type = t.value
t.value = {
'category' : self.categorias.get(t.value, t.type),
'value' : t.value
}
return t
def t_DECIMAL(self, t):
r'-?\d+\.\d+'
t.value = {
'category' : 'DECIMAL',
'value' : float(t.value)
}
return t
def t_ENTERO(self, t):
r'-?\d+'
t.value = {
'category' : 'ENTERO',
'value' : int(t.value)
}
return t
def t_CADENA(self, t):
r'".*"'
t.value = {
'category' : t.type,
'value' : t.value
}
return t
def t_OPERADOR_RELACIONAL(self, t):
r'>= | <= | = | \? | > | <'
t.type = self.operadores.get(t.value, t.type)
t.value = {
'category' : self.categorias.get(t.type, t.type),
'value' : t.value
}
return t
def t_OPERADOR_ARITMETICO(self, t):
r'\+ | \* | - | \/ | %'
t.type = self.operadores.get(t.value, t.type)
t.value = {
'category' : self.categorias.get(t.type,t.type),
'value' : t.value
}
return t
def t_OPERADOR_LOGICO(self, t):
r'\| | &'
t.type = self.operadores.get(t.value, t.type)
t.value = {
'category' : self.categorias.get(t.type, t.type),
'value' : t.value
}
return t
def t_SIGNOS_PUNTUACION(self, t):
r', | : | ; | \.'
t.type = self.operadores.get(t.value, t.type)
t.value = {
'category' : self.categorias.get(t.type, t.type),
'value' : t.value
}
return t
def t_SIGNOS_AGRUPACION(self, t):
r'\( | \) | \{ | \}'
t.type = self.operadores.get(t.value, t.type)
t.value = {
'category' : self.categorias.get(t.type, t.type),
'value' : t.value
}
return t
def build(self):
self.lexer = lex.lex(module=self)
def analyze(self, data):
self.lexer.input(data)
self.output = self.lexer
def printTokens(self):
for token in self.lexer:
print(token)