-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.py
341 lines (316 loc) · 14.4 KB
/
lexer.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Sep 26 20:16:00 2019
@author: cmdrlias
"""
import sys
from ts import ts
from token_model import token_model as tkm
from tag import tag
from colors import colors as clr
class lexer:
def __init__(self, file):
try:
self._file = open(file, 'rb')
self._lookahead = 0
self._lookbehind = 0
self._nline = 1
self._ncolumn = 1
self._ts = ts()
self._sts = True
self._erros = 0
self._anterior = 0
self._comment = False
self._string_tag = False
self._par = False
self._brack = False
self._dash = False
except IOError:
print(clr.FAIL + 'FATAL! Erro ao ler arquivo de código.')
sys.exit(0)
def closeFile(self):
try:
self._file.close()
except IOError:
print(clr.FAIL + 'FATAL! Erro ao fechar arquivo de código.')
sys.exit(0)
def erroLexico(self, msg):
print(clr.FAIL + '[Erro lexico]: ' + msg + '\n' + clr.ENDC)
self._sts = False
self._erros += 1
def printTabelaDeSimbolos(self):
self._ts.printTS()
def contarLinha(self):
self._ncolumn = 1
self._nline += 1
def contarColuna(self):
self._ncolumn += 1
def proximoToken(self):
state = 0
word = ""
char = '\u0000'
lookahead = True
while(True):
if(lookahead):
self._lookahead = self._file.read(1)
char = self._lookahead.decode('ascii')
lookahead = True
if (self._lookahead == b'\t'):
self._ncolumn += 4
if(self._lookahead == b'\n'):
self.contarLinha()
elif(self._lookahead == b' '):
self.contarColuna()
if(char == '#'):
self._comment = True
if(self._comment):
state = 25
if(self._string_tag):
state = 24
if(state == 0):
#verificação de caracteres não iseridas
if(self._string_tag):
self.erroLexico('Caractere [ \" ] esperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
self._string_tag = False
if(char == ''):
return tkm(tag.EOF, "EOF", self._nline, self._ncolumn)
elif(char == ' ' or char == '\t' or char == '\n' or char == '\r'):
state = 0
elif(char.isalpha()):
word += char
state = 2
elif(char.isdigit()):
word += char
state = 3
elif(char == '='):
state = 4
elif(char == '+'):
state = 5
elif(char == '-'):
x = self.voltaPonteiro()
if x == tag.ID or x == tag.NUM_INTEIRO:
estado = 28
else:
estado = 29
state = 6
elif(char == '*'):
state = 7
elif(char == '/'):
state = 8
elif(char == '['):
lookahead = False
state = 9
elif(char == ']'):
state = 10
elif(char == '('):
lookahead = False
state = 11
elif(char == ')'):
state = 12
elif(char == '.'):
state = 13
elif(char == ','):
state = 14
elif(char == ':'):
state = 15
elif(char == ';'):
state = 16
elif(char == '<'):
state = 17
elif(char == '>'):
state = 18
elif(char == '!'):
state = 19
elif(char == 'and'):
state = 20
elif(char == 'or'):
state = 21
elif(char == 'true'):
state = 22
elif(char == 'false'):
state = 23
elif(char == '\"'):
state = 24
elif(char == "#"):
state = 25
self._comment = True
else:
self.erroLexico('[Erro Sintático] Caractere "' + char + '" inválido. (' + str(self._nline) +
',' + str(self._ncolumn) + ')')
elif(state == 2):
if(char.isalnum() or char == '_'):
word += char
else:
self.voltaPonteiro()
tk = self._ts.getToken(word)
self._anterior = tk
if(tk is None):
tk = tkm(tag.ID, word, self._nline, self._ncolumn)
self._anterior = tk
self._ts.addToken(word, tk)
else:
self._ts.getToken(word).setLinha(self._nline)
self._ts.getToken(word).setColuna(self._ncolumn)
return tk
elif(state == 3):
if(char.isdigit()):
word += char
else:
self.voltaPonteiro()
tk = self._ts.getToken(word)
self._anterior = tk
if(tk is None):
tk = tkm(tag.NUM, word, self._nline, self._ncolumn)
self._anterior = tk
self._ts.addToken(word, tk)
else:
self._ts.getToken(word).setLinha(self._nline)
self._ts.getToken(word).setColuna(self._ncolumn)
return tk
elif(state == 4):
if(char == '='):
self._anterior = tkm(tag.OP_EQL, '==', self._nline, self._ncolumn)
return tkm(tag.OP_EQL, '==', self._nline, self._ncolumn)
elif(char == ' ' or char == '\t' or char == '\n'):
self._anterior = tkm(tag.OP_ATR, '=', self._nline, self._ncolumn)
return tkm(tag.OP_ATR, '=', self._nline, self._ncolumn)
else:
self.erroLexico('Caractere [' + char + '] inesperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
return None
self._anterior = tkm(tag.OP_EQL, '=', self._nline, self._ncolumn)
return tkm(tag.OP_EQL, '=', self._nline, self._ncolumn)
elif(state == 5):
self._anterior = tkm(tag.OP_PLUS, '+', self._nline, self._ncolumn)
return tkm(tag.OP_PLUS, '+', self._nline, self._ncolumn)
elif(state == 6):
if (char == '-'):
return tkm(tag.OP_NEG, "-", self._nline, self._ncolumn)
else:
self.retornaPonteiro()
return tkm(tag.OP_SUB, "-", self.n_line, self.n_column)
if(self._anterior is not None and self._anterior.getNome() == tag.NUM):
self._anterior = tkm(tag.OP_SUB, '-', self._nline, self._ncolumn)
return tkm(tag.OP_SUB, '-', self._nline, self._ncolumn)
else:
self._anterior = tkm(tag.OP_NEG, '-', self._nline, self._ncolumn)
return tkm(tag.OP_NEG, '-', self._nline, self._ncolumn)
elif(state == 7):
self._anterior = tkm(tag.OP_MULT, '*', self._nline, self._ncolumn)
return tkm(tag.OP_MULT, '*', self._nline, self._ncolumn)
elif(state == 8):
self._anterior = tkm(tag.OP_SLSH, '/', self._nline, self._ncolumn)
return tkm(tag.OP_SLSH, '/', self._nline, self._ncolumn)
elif(state == 9):
self._brack = True
self._anterior = tkm(tag.OP_OBRACK, '[', self._nline, self._ncolumn)
return tkm(tag.OP_OBRACK, '[', self._nline, self._ncolumn)
elif(state == 10):
self._anterior = tkm(tag.OP_CBRACK, ']', self._nline, self._ncolumn)
return tkm(tag.OP_CBRACK, ']', self._nline, self._ncolumn)
elif(state == 11):
self._par = True
self._anterior = tkm(tag.OP_OPAR, '(', self._nline, self._ncolumn)
return tkm(tag.OP_OPAR, '(', self._nline, self._ncolumn)
elif(state == 12):
self._par = False
self._anterior = tkm(tag.OP_CPAR, ')', self._nline, self._ncolumn)
return tkm(tag.OP_CPAR, ')', self._nline, self._ncolumn)
elif(state == 13):
if(char == '' or char == ' '):
self._anterior = tkm(tag.OP_DOT, '.', self._nline, self._ncolumn)
return tkm(tag.OP_DOT, '.', self._nline, self._ncolumn)
else:
self.erroLexico('Caractere [' + char + '] inesperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
return None
elif(state == 14):
if(char == '' or char == ' '):
self._anterior = tkm(tag.OP_COMMA, ',', self._nline, self._ncolumn)
return tkm(tag.OP_COMMA, ',', self._nline, self._ncolumn)
else:
self.erroLexico('Caractere [' + char + '] inesperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
return None
elif(state == 15):
self._anterior = tkm(tag.OP_COL, ':', self._nline, self._ncolumn)
return tkm(tag.OP_COL, ':', self._nline, self._ncolumn)
elif(state == 16):
self._anterior = tkm(tag.OP_SCOL, ';', self._nline, self._ncolumn)
return tkm(tag.OP_SCOL, ';', self._nline, self._ncolumn)
elif(state == 17):
if(char == '='):
self._anterior = tkm(tag.OP_EQL, '<=', self._nline, self._ncolumn)
return tkm(tag.OP_EQL, '<=', self._nline, self._ncolumn)
elif(char == ' ' or char == '\t' or char == '\n' or char == '\r'):
self._anterior = tkm(tag.OP_LESS, '<', self._nline, self._ncolumn)
return tkm(tag.OP_LESS, '<', self._nline, self._ncolumn)
else:
self.erroLexico('Caractere [' + char + '] inesperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
return None
elif(state == 18):
if(char == '='):
self._anterior = tkm(tag.OP_GRTEQ, '>=', self._nline, self._ncolumn)
return tkm(tag.OP_GRTEQ, '>=', self._nline, self._ncolumn)
elif(char == ' ' or char == '\t' or char == '\n' or char == '\r'):
self._anterior = tkm(tag.OP_GRTR, '>', self._nline, self._ncolumn)
return tkm(tag.OP_GRTR, '>', self._nline, self._ncolumn)
else:
self.erroLexico('Caractere [' + char + '] inesperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
return None
elif(state == 19):
if(char == '='):
self._anterior = tkm(tag.OP_DIF, '!=', self._nline, self._ncolumn)
return tkm(tag.OP_DIF, '!=', self._nline, self._ncolumn)
elif(char == ' ' or char == '\t' or char == '\n' or char == '\r'):
self._anterior = tkm(tag.OP_GRTR, '!', self._nline, self._ncolumn)
return tkm(tag.OP_GRTR, '!', self._nline, self._ncolumn)
else:
self.erroLexico('Caractere [' + char + '] inesperado em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
return None
elif(state == 20):
self._anterior = tkm(tag.OP_AND, 'and', self._nline, self._ncolumn)
return tkm(tag.OP_AND, 'and', self._nline, self._ncolumn)
elif(state == 21):
self._anterior = tkm(tag.OP_OR, 'or', self._nline, self._ncolumn)
return tkm(tag.OP_OR, 'or', self._nline, self._ncolumn)
elif(state == 22):
self._anterior = tkm(tag.OP_TRUE, 'true', self._nline, self._ncolumn)
return tkm(tag.OP_TRUE, 'true', self._nline, self._ncolumn)
elif(state == 23):
self._anterior = tkm(tag.OP_FALSE, 'false', self._nline, self._ncolumn)
return tkm(tag.OP_FALSE, 'false', self._nline, self._ncolumn)
elif(state == 24):
word += char
self._string_tag = True
if(char == '\"'):
if(word == '\"'):
tk = tkm(tag.STRING, "null", self._nline, self._ncolumn)
self._anterior = tk
self._ts.addToken("null", tk)
else:
tk = tkm(tag.STRING, word[:-1], self._nline, self._ncolumn)
self._anterior = tk
self._ts.addToken(word[:-1], tk)
self._string_tag = False
return tk
elif(state == 25):
if(self._lookahead == b'\n'):
state = 0
self._nline += 1
self._ncolumn = 0
self._comment = False
elif(state == 26):
self.retornaPonteiro()
return tkm(tag.OP_SUB, "-", self._nline, self._ncolumn)
# elif(state == 4):
# if(char == '='):
# return tkm(tag.OP_EQL, '==', self._nline, self._ncolumn)
# else:
# self.erroLexico('Caractere [' + char + '] inválido em (' + str(self._nline) + ',' + str(self._ncolumn) + ')')
# return None
def voltaPonteiro(self):
if(self._lookahead.decode('ascii') != ''):
self._file.seek(self._file.tell()-1)
def status(self):
return self._sts
def numErros(self):
return self._erros