-
Notifications
You must be signed in to change notification settings - Fork 1
/
parsert.py
executable file
·442 lines (331 loc) · 10.7 KB
/
parsert.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
import our_ast
from lexer import tokens, main as lex_main
from ply import yacc
'''
parser module
'''
# todo es necesario un ObjectNode????????
# todo espero que no me falte ningun token
# todo donde esta el self_type en la gramatica?????????
precedence = (
('right', 'ASSIGNAMENT'),
('right', 'NOT'),
('nonassoc', 'LESS_EQUAL_THAN', 'LESS_THAN', 'EQUAL'),
('left', 'PLUS', 'MINUS'),
('left', 'STAR', 'DIV'),
('right', 'ISVOID'),
('right', 'DESTRUCTOR'),
('left', 'AT'),
('left', 'POINT')
)
def p_program(parse):
"""
program : class_list
"""
parse[0] = our_ast.ProgramNode(parse[1])
def p_class_list(parse):
"""
class_list : class_list class POINT_AND_COMMA
| class POINT_AND_COMMA
"""
if len(parse) == 3:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_class(parse):
"""
class : CLASS TYPE_ID O_KEY features_list_opt C_KEY
"""
methods = []
attributes = []
for feature in parse[4]:
if isinstance(feature, our_ast.MethodNode):
methods.append(feature)
elif isinstance(feature, our_ast.AttrNode):
attributes.append(feature)
print('******************************')
print(parse.lineno(2))
parse[0] = our_ast.ClassNode(parse[2], "Object", attributes, methods)
def p_class_inherits(parse):
"""
class : CLASS TYPE_ID INHERITS TYPE_ID O_KEY features_list_opt C_KEY
"""
methods = []
attributes = []
for feature in parse[6]:
if isinstance(feature, our_ast.MethodNode):
methods.append(feature)
elif isinstance(feature, our_ast.AttrNode):
attributes.append(feature)
parse[0] = our_ast.ClassNode(parse[2], parse[4], attributes, methods)
# todo ver lo del slice y lo del empty
def p_feature_list_opt(parse):
"""
features_list_opt : features_list
| empty
"""
parse[0] = tuple() if parse.slice[1].type == "empty" else parse[1]
# todo cambiar las tuplas por listas
def p_feature_list(parse):
"""
features_list : features_list feature POINT_AND_COMMA
| feature POINT_AND_COMMA
"""
if len(parse) == 3:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_feature_method(parse):
"""
feature : ID O_PAR formal_params_list C_PAR TWO_POINTS TYPE_ID O_KEY expression C_KEY
"""
parse[0] = our_ast.MethodNode(parse[1], parse[3], parse[6], parse[8])
def p_feature_method_no_formals(parse):
"""
feature : ID O_PAR C_PAR TWO_POINTS TYPE_ID O_KEY expression C_KEY
"""
parse[0] = our_ast.MethodNode(parse[1], [], parse[5], parse[7])
def p_feature_attr_initialized(parse):
"""
feature : ID TWO_POINTS TYPE_ID ASSIGNAMENT expression
"""
parse[0] = our_ast.AttrNode(parse[1], parse[3], parse[5])
def p_feature_attr(parse):
"""
feature : ID TWO_POINTS TYPE_ID
"""
parse[0] = our_ast.AttrNode(parse[1], parse[3])
def p_formal_list_many(parse):
"""
formal_params_list : formal_params_list COMMA formal_param
| formal_param
"""
if len(parse) == 2:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[3],)
def p_formal(parse):
"""
formal_param : ID TWO_POINTS TYPE_ID
"""
parse[0] = our_ast.ParamNode(parse[1], parse[3])
def p_expression_object_identifier(parse):
"""
expression : ID
"""
parse[0] = our_ast.IdNode(parse[1])
def p_expression_integer_constant(parse):
"""
expression : INT
"""
parse[0] = our_ast.IntegerNode(parse[1])
def p_expression_boolean_constant(parse):
"""
expression : BOOL
"""
parse[0] = our_ast.BoolNode(parse[1])
def p_expression_string_constant(parse):
"""
expression : STRING
"""
parse[0] = our_ast.StringNode(parse[1])
def p_expr_self(parse):
"""
expression : SELF
"""
parse[0] = our_ast.SelfNode()
def p_expression_block(parse):
"""
expression : O_KEY block_list C_KEY
"""
parse[0] = our_ast.BlockNode(parse[2])
def p_block_list(parse):
"""
block_list : block_list expression POINT_AND_COMMA
| expression POINT_AND_COMMA
"""
if len(parse) == 3:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_expression_assignment(parse):
"""
expression : ID ASSIGNAMENT expression
"""
parse[0] = our_ast.AssigNode(parse[1], parse[3])
def p_expression_dispatch(parse):
"""
expression : expression POINT ID O_PAR arguments_list_opt C_PAR
"""
parse[0] = our_ast.DispatchStaticNode(parse[3], parse[5], parse[1])
def p_arguments_list_opt(parse):
"""
arguments_list_opt : arguments_list
| empty
"""
parse[0] = tuple() if parse.slice[1].type == "empty" else parse[1]
def p_arguments_list(parse):
"""
arguments_list : arguments_list COMMA expression
| expression
"""
if len(parse) == 2:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[3],)
def p_expression_inherits_dispatch(parse):
"""
expression : expression AT TYPE_ID POINT ID O_PAR arguments_list_opt C_PAR
"""
parse[0] = our_ast.DispatchInheritsNode(parse[5], parse[7], parse[3],
parse[1])
def p_expression_self_dispatch(parse):
"""
expression : ID O_PAR arguments_list_opt C_PAR
"""
parse[0] = our_ast.DispatchSelfNode(parse[1], parse[3])
def p_expression_math_operations(parse):
"""
expression : expression PLUS expression
| expression MINUS expression
| expression STAR expression
| expression DIV expression
"""
if parse[2] == '+':
parse[0] = our_ast.PlusNode(parse[1], parse[3])
elif parse[2] == '-':
parse[0] = our_ast.MinusNode(parse[1], parse[3])
elif parse[2] == '*':
parse[0] = our_ast.StarNode(parse[1], parse[3])
elif parse[2] == '/':
parse[0] = our_ast.DivNode(parse[1], parse[3])
def p_expression_math_comparisons(parse):
"""
expression : expression LESS_THAN expression
| expression LESS_EQUAL_THAN expression
| expression EQUAL expression
"""
if parse[2] == '<':
parse[0] = our_ast.LessThanNode(parse[1], parse[3])
elif parse[2] == '<=':
parse[0] = our_ast.LessOrEqualNode(parse[1], parse[3])
elif parse[2] == '=':
parse[0] = our_ast.EqualNode(parse[1], parse[3])
# todo sobra el ParNode????????????????????
def p_expression_with_parenthesis(parse):
"""
expression : O_PAR expression C_PAR
"""
parse[0] = parse[2]
def p_expression_if_conditional(parse):
"""
expression : IF expression THEN expression ELSE expression FI
"""
parse[0] = our_ast.IfNode(parse[2], parse[4], parse[6])
def p_expression_while_loop(parse):
"""
expression : WHILE expression LOOP expression POOL
"""
parse[0] = our_ast.WhileNode(parse[2], parse[4])
# todo faltan los lets en los que tengo dudas con la forma de la gramatica que
# tienen
def p_expression_case(parse):
"""
expression : CASE expression OF actions_list ESAC
"""
parse[0] = our_ast.CaseNode(parse[2], parse[4])
def p_actions_list(parse):
"""
actions_list : actions_list action
| action
"""
if len(parse) == 2:
parse[0] = (parse[1],)
else:
parse[0] = parse[1] + (parse[2],)
def p_action_expr(parse):
"""
action : ID TWO_POINTS TYPE_ID IMPLICATION expression POINT_AND_COMMA
"""
parse[0] = our_ast.BranchNode(parse[1], parse[3], parse[5])
def p_expression_new(parse):
"""
expression : NEW TYPE_ID
"""
parse[0] = our_ast.NewNode(parse[2])
def p_expression_isvoid(parse):
"""
expression : ISVOID expression
"""
parse[0] = our_ast.IsVoidNode(parse[2])
def p_expression_integer_complement(parse):
"""
expression : DESTRUCTOR expression
"""
parse[0] = our_ast.NegationNode(parse[2])
def p_expression_boolean_complement(parse):
"""
expression : NOT expression
"""
parse[0] = our_ast.NotNode(parse[2])
######################### LET EXPRESSIONS ########################################
def p_expression_let(parse):
"""
expression : let_expression
"""
parse[0] = parse[1]
def p_let_expression(parse):
"""
let_expression : LET generate_instances IN expression
"""
parse[0] = our_ast.LetNode(parse[4], parse[2])
def p_generate_instances(parse):
"""
generate_instances : ID TWO_POINTS TYPE_ID
| ID TWO_POINTS TYPE_ID ASSIGNAMENT expression
| generate_instances COMMA ID TWO_POINTS TYPE_ID
| generate_instances COMMA ID TWO_POINTS TYPE_ID ASSIGNAMENT expression
"""
if len(parse) == 4:
parse[0] = (our_ast.AttrNode(parse[1], parse[3]),)
elif len(parse) == 6 and isinstance(parse[1], str):
parse[0] = (our_ast.AttrNode(parse[1], parse[3], parse[5]),)
elif len(parse) == 6:
parse[0] = parse[1] + (our_ast.AttrNode(parse[3], parse[5]),)
elif len(parse) == 8:
parse[0] = parse[1] + (our_ast.AttrNode(parse[3], parse[5], parse[7]),)
def p_empty(parse):
"""
empty :
"""
# parse[0] = None
pass
def p_error(parse):
"""
Error rule for Syntax Errors handling and reporting.
"""
if parse is None:
print("Error! Unexpected end of input!")
else:
print("Syntax error! Line: {}, position: {}, character: {}, "
"type: {}".format(parse.lineno, parse.lexpos, parse.value,
parse.type))
parser.errok()
def main(source_path=None,testing_mode=True):
global parser
parser = yacc.yacc()
result = None
lex = lex_main(source_path='', testing_mode=False)
data = None
with open(source_path, encoding="utf-8") as file:
data = file.read()
if testing_mode:
result = parser.parse(input=data, lexer=lex, debug=testing_mode)
else:
result = parser.parse(data, lex)
print(result)
return result
if not data:
return parser
# main(r'C:\Users\LsW\Desktop\Segundo Proyecto de Compilacion '
# r'Rayniel Ramos Gonzalez c412\code\test_cases\palindrome.cl', testing_mode=False)