-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsintatica.py
268 lines (219 loc) · 5.02 KB
/
sintatica.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
from ply.yacc import yacc
from lexica import *
# --- Parser
def p_program(p):
'''
program : statement
| funclist
| epsilon
'''
def p_funclist(p):
'''
funclist : funcdef funclist2
'''
def p_funclist2(p):
'''
funclist2 : funclist
| epsilon
'''
def p_funcdef(p):
'''
funcdef : DEF IDENT LPAREN paramlist RPAREN LCHAVES statelist RCHAVES
'''
def p_paramlist(p):
'''
paramlist : INT IDENT paramlist2
| FLOAT IDENT paramlist2
| STRING IDENT paramlist2
| epsilon
'''
def p_paramlist2(p):
'''
paramlist2 : VIRGULA paramlist
| epsilon
'''
def p_statement(p):
'''
statement : vardecl PONTO_VIRGULA
| atribstat PONTO_VIRGULA
| printstat PONTO_VIRGULA
| readstat PONTO_VIRGULA
| returnstat PONTO_VIRGULA
| ifstat
| forstat
| LCHAVES statelist RCHAVES
| BREAK PONTO_VIRGULA
| PONTO_VIRGULA
'''
def p_vardecl(p):
'''
vardecl : INT IDENT a
| FLOAT IDENT a
| STRING IDENT a
'''
def p_a(p):
'''
a : LCOLCHETES INT_CONSTANT RCOLCHETES a
| epsilon
'''
def p_atribstat(p):
'''
atribstat : lvalue ATRIBUICAO atribstat2
'''
def p_atribstat2(p):
'''
atribstat2 : expression
| allocexpression
| funccall
'''
def p_funccall(p):
'''
funccall : CALL IDENT LPAREN paramlistcall RPAREN
'''
def p_paramlistcall(p):
'''
paramlistcall : IDENT paramlistcall2
| epsilon
'''
def p_paramlistcall2(p):
'''
paramlistcall2 : VIRGULA paramlistcall
| epsilon
'''
def p_printstat(p):
'''
printstat : PRINT expression
'''
def p_readstat(p):
'''
readstat : READ lvalue
'''
def p_returnstat(p):
'''
returnstat : RETURN
'''
def p_ifstat(p):
'''
ifstat : IF LPAREN expression RPAREN statement ifstat2
'''
def p_ifstat2(p):
'''
ifstat2 : ELSE statement PONTO_VIRGULA
| PONTO_VIRGULA
'''
def p_forstat(p):
'''
forstat : FOR LPAREN atribstat PONTO_VIRGULA expression PONTO_VIRGULA atribstat RPAREN statement
'''
def p_statelist(p):
'''
statelist : statement statelist2
'''
def p_statelist2(p):
'''
statelist2 : statelist
| epsilon
'''
def p_allocexpression(p):
'''
allocexpression : NEW allocexpression2
'''
def p_allocexpression2(p):
'''
allocexpression2 : INT e
| FLOAT e
| STRING e
'''
def p_e(p):
'''
e : LCOLCHETES numexpression RCOLCHETES e2
'''
def p_e2(p):
'''
e2 : e
| epsilon
'''
def p_expression(p):
'''
expression : numexpression expression2
'''
def p_expression2(p):
'''
expression2 : MENOR numexpression
| MAIOR numexpression
| MENORIGUAL numexpression
| MAIORIGUAL numexpression
| IGUAL numexpression
| DIFERENTE numexpression
| epsilon
'''
def p_numexpression(p):
'''
numexpression : term d
'''
def p_d(p):
'''
d : SOMA term d
| SUBTRACAO term d
| epsilon
'''
def p_term(p):
'''
term : unaryexpr b
'''
def p_b(p):
'''
b : MULTIPLICACAO unaryexpr b
| DIVISAO unaryexpr b
| RESTO unaryexpr b
| epsilon
'''
def p_unaryexpr(p):
'''
unaryexpr : SOMA factor
| SUBTRACAO factor
| factor
'''
def p_factor(p):
'''
factor : INT_CONSTANT
| FLOAT_CONSTANT
| STRING_CONSTANT
| NULL
| lvalue
| LPAREN numexpression RPAREN
'''
def p_lvalue(p):
'''
lvalue : IDENT c
'''
def p_c(p):
'''
c : LCOLCHETES numexpression RCOLCHETES c
| epsilon
'''
def p_epsilon(p):
'epsilon :'
class Erros:
def __init__(self, sem_erros):
self.sem_erros = sem_erros
def set_erros(self):
self.sem_erros = False
e = Erros(True)
def p_error(p):
e.set_erros()
print('INE5622')
print('Leitura de p: ', p)
print(f'Erro de sintaxe em: {p.value!r}')
print()
# Build the parser
parser = yacc()
# Parse an expression
#ast = parser.parse('def nomefuncao ( int A , int B ) { break ; }')
#ast = parser.parse('call def funcao ( int A , int B ) { }')
# ast = parser.parse(data())
def parse(data):
# ast = parser.parse(data)
parser.parse(data)
if (e.sem_erros):
print('Não foram detectados erros sintáticos')