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
|
from enum import Enum
from symbol import LexToken, TablaLex, tokens
def inicio_lexer(data):
tabla = TablaLex()
selector = ''
recol_string = ''
recol_caracter = ''
recol_comentario = ''
recol_operador = ''
recol_ident = ''
for c in data + "\n":
# print (c + ' (' + selector + ')')
if selector == '':
# Entradas a tokens
if c == '"':
selector = 'STRING_LIT'
continue
elif c == '\'':
selector = 'CHAR_LIT'
continue
elif c.isalpha() or c == '_':
selector = 'ID/RESERVADA'
elif c == '/':
recol_comentario = '/'
elif (c == '=' or c == '+' or c == '-' or c == '&' or c == '|') and recol_operador == '':
recol_operador = c
continue
elif c == '!' or c == '<' or c == '>':
recol_operador = c
continue
elif (c == '{' or c == '}' or c == '(' or c == ')' or
c == ',' or c == ';' or (c == '*' and recol_comentario == '')):
tabla.insertar(LexToken(c, None, None, 1))
continue
# Apertura de comentario
if recol_comentario == '/' and c == '*':
selector = 'COMMENT'
recol_comentario = ''
continue
# Apertura de operador compuesto
if len(recol_operador) > 0:
rc = recol_operador + c
if (rc == '>=' or rc == '<=' or rc == '==' or rc == '!=' or
rc == '&&' or rc == '||' or rc == '++' or rc == '--'):
# Operador compuesto
tabla.insertar(LexToken(rc, None, None, 1))
recol_operador = ''
continue
else:
# Operador simple
tabla.insertar(LexToken(recol_operador, None, None, 1))
recol_operador = ''
# Cadenas de texto
if selector == 'STRING_LIT':
if c == '"':
tabla.insertar(LexToken('STRING_LIT', None, recol_string, 1))
selector = ''
recol_string = ''
else:
recol_string += c
# Caracteres
if selector == 'CHAR_LIT':
if len(recol_caracter) > 1:
print ('Error: más de un caracter en una literal de caracter')
break
if c == '\'':
if len(recol_caracter) == 0:
print ('Error: literal de caracter vacía')
break
tabla.insertar(LexToken('CHAR_LIT', None, recol_caracter, 1))
selector = ''
recol_caracter = ''
else:
recol_caracter += c
# Comentarios
if selector == 'COMMENT':
if c == '*':
recol_comentario = c
elif recol_comentario == '*':
if c == '/':
selector = ''
recol_comentario = ''
else:
recol_comentario = ''
# Identificador o palabra reservada
if selector == 'ID/RESERVADA':
if c.isalnum() or c == '_':
recol_ident += c
else:
if recol_ident == 'booleano':
tabla.insertar(LexToken('BOOLEAN', None, None, 1))
elif recol_ident == 'detener':
tabla.insertar(LexToken('BREAK', None, None, 1))
elif recol_ident == 'byte':
tabla.insertar(LexToken('BYTE', None, None, 1))
elif recol_ident == 'caracter':
tabla.insertar(LexToken('CHAR', None, None, 1))
elif recol_ident == 'doble':
tabla.insertar(LexToken('DOUBLE', None, None, 1))
elif recol_ident == 'sino':
tabla.insertar(LexToken('ELSE', None, None, 1))
elif recol_ident == 'porcada':
tabla.insertar(LexToken('FOR', None, None, 1))
elif recol_ident == 'si':
tabla.insertar(LexToken('IF', None, None, 1))
elif recol_ident == 'entero':
tabla.insertar(LexToken('INT', None, None, 1))
elif recol_ident == 'imprimir':
tabla.insertar(LexToken('PRINT', None, None, 1))
elif recol_ident == 'leer':
tabla.insertar(LexToken('READ', None, None, 1))
elif recol_ident == 'retorna':
tabla.insertar(LexToken('RETURN', None, None, 1))
elif recol_ident == 'vacio':
tabla.insertar(LexToken('VOID', None, None, 1))
elif recol_ident == 'mientras':
tabla.insertar(LexToken('WHILE', None, None, 1))
else:
tabla.insertar(LexToken('IDENT', recol_ident, None, 1))
recol_ident = ''
selector = ''
# Imprimir tabla de símbolos
print (str(tabla))
|