-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntactic.py
311 lines (267 loc) · 11.3 KB
/
syntactic.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
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.current = 0
def programa(self):
self.match('KEYWORD', 'program')
self.match('ID')
self.match('DELIMITER', ';')
self.corpo()
self.match('DELIMITER', '.')
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == '.':
return
else:
self.error("Não é possível declarar nada fora do escopo principal!")
def corpo(self):
self.declara()
self.match('KEYWORD', 'begin')
self.comandos()
self.match('KEYWORD', 'end')
def corpo_funcao(self):
self.declara()
self.match('KEYWORD', 'begin')
self.comandos()
self.match('KEYWORD', 'end')
self.match('DELIMITER', ';')
def comandos(self):
self.comando()
self.mais_comandos()
def chamada_procedimento(self):
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == '(':
self.match('DELIMITER', '(')
self.var()
self.match('DELIMITER', ')')
def comando(self):
if self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'read':
self.match('KEYWORD', 'read')
self.match('DELIMITER', '(')
self.var()
self.match('DELIMITER', ')')
self.match('DELIMITER', ';')
self.comando()
elif self.tokens[self.current][0] == 'ID':
self.expressao()
self.comando()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'return':
self.match('KEYWORD', 'return')
self.match('DELIMITER', ';')
self.comando()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'write':
self.match('KEYWORD', 'write')
self.match('DELIMITER', '(')
self.var()
self.match('DELIMITER', ')')
self.match('DELIMITER', ';')
self.comando()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'for':
self.match('KEYWORD', 'for')
self.match('ID')
self.match('OPERATOR', ':=')
self.var()
self.match('KEYWORD', 'to')
self.var()
self.match('KEYWORD', 'do')
self.match('KEYWORD', 'begin')
self.comando()
self.match('KEYWORD', 'end')
self.comando()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'repeat':
self.match('KEYWORD', 'repeat')
self.comando()
self.match('KEYWORD', 'until')
self.match('DELIMITER', '(')
self.termo()
self.match('DELIMITER', ')')
self.comando()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'while':
self.match('KEYWORD', 'while')
self.match('DELIMITER', '(')
self.termo()
self.match('DELIMITER', ')')
self.match('KEYWORD', 'do')
self.match('KEYWORD', 'begin')
self.comando()
self.match('KEYWORD', 'end')
self.comando()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'if':
self.match('KEYWORD', 'if')
self.match('DELIMITER', '(')
self.termo()
self.match('DELIMITER', ')')
self.match('KEYWORD', 'then')
#self.match('KEYWORD', 'begin')
self.comando()
#self.match('KEYWORD', 'end')
#self.pfalsa()
#self.comando()
def pfalsa(self):
if self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'else':
self.match('KEYWORD', 'else')
self.match('KEYWORD', 'begin')
self.comando()
self.match('KEYWORD', 'end')
def expressao(self):
self.match('ID')
if self.tokens[self.current][0] == 'OPERATOR' and self.tokens[self.current][1] == ':=':
self.match('OPERATOR', ':=')
self.var()
self.match('DELIMITER', ';')
elif self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == '(':
self.match('DELIMITER', '(')
self.var()
self.match('DELIMITER', ')')
self.match('DELIMITER', ';')
else:
self.match(':= ou ()')
def mais_comandos(self):
if self.tokens[self.current][1] in ('read', 'write', 'for', 'while', 'if', 'repeat') or self.tokens[self.current][0] == 'ID':
self.comandos()
def var(self):
if self.tokens[self.current][0] in ['INTEGER_NUM', 'REAL_NUM']:
self.match(self.tokens[self.current][0])
elif self.tokens[self.current][0] == 'ID':
self.match('ID')
self.chamada_procedimento()
else:
self.termo()
self.mais_var_comando()
def mais_var_comando(self):
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ',':
self.match('DELIMITER', ',')
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ')':
self.match("IDENTIFICADOR OU NUM OU CHAMADA PROCEDIMENTO")
else:
self.var()
def corpo_procedimento(self):
self.declara()
self.match('KEYWORD', 'begin')
self.comandos()
self.match('KEYWORD', 'end')
self.match('DELIMITER', ';')
def funcao(self):
self.match('KEYWORD', 'function')
self.match('ID')
self.parametros()
self.match('DELIMITER', ':')
self.tipo_var()
self.match('DELIMITER', ';')
self.corpo_funcao()
def procedimento(self):
self.match('KEYWORD','procedure')
self.match('ID')
self.parametros()
self.match('DELIMITER', ';')
self.corpo_procedimento()
def parametros(self):
self.match('DELIMITER', '(')
self.lista_parametros()
self.match('DELIMITER', ')')
def termo(self):
if self.tokens[self.current][0] == 'ID' or self.tokens[self.current][0] in ['INTEGER_NUM', 'REAL_NUM']:
self.match(self.tokens[self.current][0])
elif self.tokens[self.current][0] == 'OPERATOR' and self.tokens[self.current][1] in ['+', '-', '*', '/', '>', '<', '>=', '<=', '=', '<>']:
self.match('OPERATOR', self.tokens[self.current][1])
self.match('DELIMITER', '(')
self.termo()
self.match('DELIMITER', ',')
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ')':
self.match('IDENTIFICADOR OU NUMERO OU CHAMADA PROCEDIMENTO')
else:
self.termo()
self.match('DELIMITER', ')')
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] in ('input', 'output', 'length','inverte'):
self.match('KEYWORD', self.tokens[self.current][1])
self.match('DELIMITER', '(')
self.um_conteudo()
self.match('DELIMITER', ')')
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'concatena':
self.match('KEYWORD', self.tokens[self.current][1])
self.match('DELIMITER', '(')
self.dois_conteudo()
self.match('DELIMITER', ')')
elif self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ')':
return
else:
self.match('IDENTIFICADOR OU NUMERO OU OPERADOR')
def um_conteudo(self):
self.match('DELIMITER', '#')
self.valor_conteudo()
self.match('DELIMITER', '#')
def dois_conteudo(self):
self.match('DELIMITER', '#')
self.valor_conteudo()
self.match('DELIMITER', '#')
self.match('DELIMITER', ',')
self.match('DELIMITER', '#')
self.valor_conteudo()
self.match('DELIMITER', '#')
def valor_conteudo(self):
if self.tokens[self.current][0] in ['INTEGER_NUM', 'REAL_NUM']:
self.match(self.tokens[self.current][0])
self.mais_valor_conteudo()
def mais_valor_conteudo(self):
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ',':
self.match('DELIMITER', ',')
self.valor_conteudo()
def lista_parametros(self):
self.declaracao_param()
self.mais_param()
def declaracao_param(self):
if self.tokens[self.current][0] == 'ID':
self.match('ID')
self.match('DELIMITER', ':')
self.tipo_var()
def mais_param(self):
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ';':
self.match('DELIMITER', ';')
if self.tokens[self.current][0] == 'ID':
self.declaracao_param()
self.mais_param()
else:
self.match('ID')
def declara(self):
if self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] == 'var':
self.match('KEYWORD', 'var')
self.dvar()
self.match('DELIMITER', ';')
self.mais_dc()
self.declara()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] in ('function'):
self.funcao()
self.declara()
elif self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] in ('procedure'):
self.procedimento()
self.declara()
def mais_dc(self):
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ';':
self.match('DELIMITER', ';')
self.cont_dc()
def dvar(self):
self.variaveis()
self.match('DELIMITER', ':')
self.tipo_var()
def tipo_var(self):
if self.tokens[self.current][0] == 'KEYWORD' and self.tokens[self.current][1] in ['integer', 'real', 'pilha_of_integer', 'pilha_of_real']:
self.match('KEYWORD', self.tokens[self.current][1])
else:
self.error("Tipo de variável inválido.")
def variaveis(self):
self.match('ID')
self.mais_var()
def mais_var(self):
if self.tokens[self.current][0] == 'DELIMITER' and self.tokens[self.current][1] == ',':
self.match('DELIMITER', ',')
self.variaveis()
def match(self, expected_type, expected_value=None):
if self.current >= len(self.tokens):
self.error("Fim inesperado do arquivo.")
token_type, token_value, _ = self.tokens[self.current]
if token_type != expected_type or (expected_value is not None and token_value != expected_value):
self.error(f"Token inesperado: {token_type} '{token_value}', esperava-se: {expected_type} '{expected_value if expected_value else ''}'.")
if self.current == len(self.tokens)-1:
return
else:
self.current += 1
def error(self, message):
_, _, line = self.tokens[self.current]
raise SyntaxError(f"Linha {line}: {message}")