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
|
#!/bin/env python
# -*- coding: utf-8 -*-
# Copyright (C) 2022 Iván Alejandro Ávalos Díaz <avalos@disroot.org>
# Edgar Alexis López Martínez <edgarmlmp@gmail.com>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from enum import Enum
from tabla import LexToken, TablaLex, Token, tokens, reservadas
from parser import Parser
from shared import Control
from errors import Error
op_compuestos = ['>=', '<=', '==', '!=', '&&', '||', '++', '--']
op_simples_a = ['=', '+', '-', '&', '|'] # pueden ir al final del op compuesto
op_simples_b = ['!', '<', '>'] # solo pueden ir al inicio del op compuesto
op_simples = op_simples_a + op_simples_b
otros_tokens = ['{', '}', '(', ')', ',', '.', ';']
class Selector(Enum):
NINGUNO = 0
STRING_LIT = 1
CHAR_LIT = 2
ID_RESERVADA = 3
COMMENT = 4
ENTERO = 5
class Lexer:
tabla: TablaLex = None
def __init__(self, input_file: str):
self.tabla = TablaLex()
self.numlinea = 1
self.selector = Selector.NINGUNO
self.recol_string = ''
self.recol_caracter = ''
self.recol_comentario = ''
self.recol_operador = ''
self.recol_ident = ''
self.recol_entero = ''
self.input_file = input_file
with open(input_file) as f:
self.data = f.read()
def inicio(self):
for l in self.data.splitlines():
for c in l + "\n":
r = self.procesar(c)
if r == Control.ERROR: return 1
while r != Control.SIGUIENTE:
r = self.procesar(c)
if r == Control.ERROR: return 1
self.numlinea += 1
# End of file (EOF)
self.insertar_tabla(Token.EOF, None, None)
# Exportar tabla de símbolos
self.tabla.exportar(self.input_file + '.tab')
return 0
def procesar(self, c):
# if c != "\t" and c != "\n":
# print (c + ' (' + str(self.selector) + ')')
if self.selector == Selector.NINGUNO:
# Entrada a string
if c == '"':
self.selector = Selector.STRING_LIT
return Control.SIGUIENTE
# Entrada a caracter
elif c == '\'':
self.selector = Selector.CHAR_LIT
return Control.SIGUIENTE
# Entrada a id o palabra reservada
elif c.isalpha() or c == '_':
self.selector = Selector.ID_RESERVADA
# Entrada a entero
elif c.isdigit():
self.selector = Selector.ENTERO
# Entrada a comentario
elif c == '/':
self.recol_comentario = '/'
# Entrada a operador (simple o compuesto)
elif c in op_simples_a and self.recol_operador == '':
self.recol_operador = c
return Control.SIGUIENTE
# Entrada a operador (simple)
elif c in op_simples_b:
self.recol_operador = c
return Control.SIGUIENTE
# Entrada a tokens simples
elif c in otros_tokens or (c == '*' and self.recol_comentario == ''):
self.insertar_tabla(Token(c), None, None)
return Control.SIGUIENTE
# Apertura de comentario
if self.recol_comentario == '/' and c == '*':
self.selector = Selector.COMMENT
self.recol_comentario = ''
return Control.SIGUIENTE
# Apertura de operador compuesto
if len(self.recol_operador) > 0:
rc = self.recol_operador + c
if rc in op_compuestos:
# Operador compuesto
self.insertar_tabla(Token(rc), None, None)
self.recol_operador = ''
return Control.SIGUIENTE
else:
# Operador simple
self.insertar_tabla(Token(self.recol_operador), None, None)
if c in op_simples:
self.insertar_tabla(Token(c), None, None)
self.recol_operador = ''
return Control.SIGUIENTE
# Cadenas de texto
if self.selector == Selector.STRING_LIT:
return self.procesar_cadena(c)
# Caracteres
if self.selector == Selector.CHAR_LIT:
return self.procesar_caracter(c)
# Comentarios
if self.selector == Selector.COMMENT:
return self.procesar_comentario(c)
# Identificador o palabra reservada
if self.selector == Selector.ID_RESERVADA:
return self.procesar_identificador(c)
# Enteros
if self.selector == Selector.ENTERO:
return self.procesar_entero(c)
return Control.SIGUIENTE
def procesar_cadena(self, c):
if c == '"':
self.insertar_tabla(Token.STRING_LIT, None, self.recol_string)
self.selector = Selector.NINGUNO
self.recol_string = ''
else:
self.recol_string += c
return Control.SIGUIENTE
def procesar_caracter(self, c):
if len(self.recol_caracter) > 1:
print(Error.lex('L_CAR_LARGO', self.numlinea).message)
return Control.ERROR
if c == '\'':
if len(self.recol_caracter) == 0:
print(Error('L_CAR_VACIO', self.numlinea).message)
return Control.ERROR
self.insertar_tabla(Token.CHAR_LIT, None, self.recol_caracter)
self.selector = Selector.NINGUNO
self.recol_caracter = ''
else:
self.recol_caracter += c
return Control.SIGUIENTE
def procesar_comentario(self, c):
if c == '*':
self.recol_comentario = c
elif self.recol_comentario == '*':
if c == '/':
self.selector = Selector.NINGUNO
self.recol_comentario = ''
else:
self.recol_comentario = ''
return Control.SIGUIENTE
def procesar_identificador(self, c):
if c.isalnum() or c == '_':
self.recol_ident += c
else:
if self.recol_ident in reservadas:
self.insertar_tabla(Token(self.recol_ident), None, None)
elif self.recol_ident == 'verdadero':
self.insertar_tabla(Token.BOOLEAN_LIT, None, True)
elif self.recol_ident == 'falso':
self.insertar_tabla(Token.BOOLEAN_LIT, None, False)
else:
self.insertar_tabla(Token.IDENT, self.recol_ident, None)
self.recol_ident = ''
self.selector = Selector.NINGUNO
return Control.REPETIR
return Control.SIGUIENTE
def procesar_entero(self, c):
if c.isdigit():
self.recol_entero += c
else:
self.insertar_tabla(Token.INT_LIT, None, int(self.recol_entero))
self.recol_entero = ''
self.selector = Selector.NINGUNO
return Control.REPETIR
return Control.SIGUIENTE
def insertar_tabla(self, token, nombre, valor):
self.tabla.insertar(LexToken(token, nombre, valor, self.numlinea))
|