-
Notifications
You must be signed in to change notification settings - Fork 0
/
oaf_yacc_func.py
329 lines (255 loc) · 7.37 KB
/
oaf_yacc_func.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
import ply.yacc as yacc
# Get semantic variables
import oaf_sem as sem
# Parsing state module
import oaf_state as state
# Get tokens from the lexer
from oaf_lex import tokens
# Get the lexer
from oaf_lex import lexer
from oaf_lex import find_column
def p_program(p):
'''Program : Declaration Function Main'''
def p_main(p):
'''Main : MAIN LPAREN RPAREN Seen_Main FBlock'''
def p_declaration(p):
'''Declaration : Primitive ID Declaration1
| empty'''
def p_declaration_1(p):
'''Declaration1 : Array SEMI Declaration
| LPAREN ParamList RPAREN Seen_Function FBlock'''
def p_local_declaration(p):
'''Local_Declaration : Primitive ID Array SEMI Local_Declaration
| empty'''
def p_array(p):
'''Array : LBRACKET ICONST RBRACKET Array
| empty'''
def p_function(p):
'''Function : Function1
| RFunction
| empty'''
def p_function_1(p):
'''Function1 : VOID ID LPAREN ParamList RPAREN Seen_Function FBlock Function'''
def p_rfunction(p):
'''RFunction : Primitive ID LPAREN ParamList RPAREN Seen_Function FBlock Function'''
def p_block(p):
'''Block : LBRACE Instruction RBRACE'''
def p_fblock(p):
'''FBlock : LBRACE Local_Declaration Instruction RBRACE'''
def p_conditional(p):
'''Conditional : IF LPAREN SuperExpr RPAREN Block Else'''
def p_else(p):
'''Else : ELSE Block
| empty'''
def p_superexpr(p):
'''SuperExpr : Expression SuperExpr1'''
p[0] = p[1]
def p_superexpr_1(p):
'''SuperExpr1 : AND SuperExpr
| OR SuperExpr
| empty'''
def p_expression(p):
'''Expression : Expr Expression1'''
p[0] = p[1]
def p_expression_1(p):
'''Expression1 : LESSTHAN Expr
| GREATHAN Expr
| DIFFERENT Expr
| TWOEQUAL Expr
| GREATEQUAL Expr
| LESSEQUAL Expr
| empty'''
def p_expr(p):
'''Expr : Term Expr1'''
p[0] = p[1]
def p_expr_1(p):
'''Expr1 : MINUS Expr
| PLUS Expr
| empty'''
def p_term(p):
'''Term : Factor Term1'''
p[0] = p[1]
def p_term_1(p):
'''Term1 : DIVIDE Term
| TIMES Term
| empty'''
def p_factor(p):
'''Factor : LPAREN SuperExpr RPAREN
| Factor1'''
p[0] = p[1]
def p_factor_1(p):
'''Factor1 : Factor2
| Factor3
| Length'''
p[0] = p[1]
def p_factor_2(p):
'''Factor2 : MINUS Factor3
| PLUS Factor3
| MINUS Length
| PLUS Length
| NOT Not'''
p[0] = p[2]
def p_factor_3(p):
'''Factor3 : Constant
| Call'''
p[0] = p[1]
def p_not(p):
'''Not : Factor3
| LPAREN SuperExpr RPAREN'''
p[0] = p[1]
# Agregar tipo STRING para variables y funciones
def p_params(p):
'''Params : Params1
| empty'''
def p_params_1(p):
'''Params1 : SuperExpr Params2'''
p[0] = p[1]
def p_params_2(p):
'''Params2 : COMMA Params1
| empty'''
def p_params_3(p):
'''Params3 : SuperExpr Params4
| STRING Params4'''
p[0] = p[1]
def p_params_4(p):
'''Params4 : COMMA Params3
| empty'''
def p_loop(p):
'''Loop : LOOP LPAREN SuperExpr RPAREN Block'''
def p_assign(p):
'''Assign : ID Array2 EQUAL Assign1'''
def p_assign_1(p):
'''Assign1 : SuperExpr
| STRING'''
def p_array_2(p):
'''Array2 : LBRACKET SuperExpr RBRACKET Array2
| empty'''
def p_call(p):
'''Call : ID LPAREN Params RPAREN'''
p[0] = p[1]
def p_read(p):
'''Read : READ LPAREN Type COMMA ID RPAREN'''
def p_type(p):
'''Type : Primitive
| STRING'''
p[0] = p[1]
def p_print(p):
'''Print : PRINT LPAREN Params3 RPAREN'''
def p_brush(p):
'''Brush : BRUSH LPAREN SuperExpr RPAREN'''
def p_color(p):
'''Color : COLOR LPAREN SuperExpr COMMA SuperExpr COMMA SuperExpr RPAREN'''
def p_pendown(p):
'''PenDown : PD LPAREN RPAREN'''
def p_penup(p):
'''PenUp : PU LPAREN RPAREN'''
def p_clear(p):
'''Clear : CLEAR LPAREN RPAREN '''
def p_home(p):
'''Home : HOME LPAREN RPAREN'''
def p_forward(p):
'''Forward : FD LPAREN SuperExpr RPAREN'''
def p_rotate(p):
'''Rotate : RT LPAREN SuperExpr RPAREN'''
def p_circle(p):
'''Circle : CIRCLE LPAREN SuperExpr RPAREN'''
def p_fig(p):
'''Figure : FIGURE LPAREN SuperExpr COMMA SuperExpr RPAREN'''
def p_arc(p):
'''Arc : ARC LPAREN SuperExpr COMMA SuperExpr RPAREN'''
def p_speed(p):
'''Speed : SPEED LPAREN SuperExpr RPAREN '''
def p_triangle(p):
'''Triangle : TRIANGLE LPAREN SuperExpr RPAREN '''
def p_square(p):
'''Square : SQUARE LPAREN SuperExpr RPAREN '''
def p_param(p):
'''Param : Primitive ID Array1 Update_Signature'''
def p_array_1(p):
'''Array1 : LBRACKET RBRACKET Add_Dimension Array1
| empty'''
def p_add_dimension(p):
'''Add_Dimension : '''
state.arr_dim_str += "[]"
def p_primitive(p):
'''Primitive : INT
| FLOAT
| BOOL
| CHAR'''
p[0] = p[1]
def p_paramlist(p):
'''ParamList : ParamList1
| empty'''
def p_paramlist_1(p):
'''ParamList1 : Param ParamList2'''
def p_paramlist_2(p):
'''ParamList2 : COMMA ParamList1
| empty'''
def p_instruction(p):
'''Instruction : Instruction1 SEMI Instruction
| empty'''
def p_instruction_1(p):
'''Instruction1 : Loop
| Conditional
| Assign
| Call
| Brush
| Read
| Print
| PenDown
| PenUp
| Home
| Forward
| Rotate
| Color
| Circle
| Arc
| Square
| Length
| Triangle
| Speed
| Figure
| Clear
| Return'''
def p_length(p):
'''Length : LENGTH LPAREN ID Array1 RPAREN'''
def p_return(p):
'''Return : RETURN RType'''
def p_rtype(p):
'''RType : SuperExpr
| empty'''
def p_constant(p):
'''Constant : ID Array2
| FCONST
| ICONST
| CCONST
| FALSE
| TRUE'''
p[0] = p[1]
# Function rules
def p_seen_function(p):
'''Seen_Function : '''
sem.fill_symbol_table_function(p[-4], [[p[-5]], state.signature, state.f_param_list])
state.f_param_list = []
state.signature = []
def p_seen_main(p):
'''Seen_Main : '''
# Main has no signature, parameters, or return type
sem.fill_symbol_table_function(p[-3], [[], [], [], [], 0])
def p_update_signature_size(p):
'''Update_Signature : '''
state.signature.append(p[-3] + state.arr_dim_str)
state.f_param_list.append(p[-2])
state.arr_dim_str = ""
# Empty production
def p_empty(p):
'''empty : '''
pass
# Error rule for syntax errors
def p_error(p):
if(p):
raise NameError("Syntax error at line {0} col {1}, unexpected '{2}'".format(p.lineno, find_column(lexer.lexdata, p), p.value))
else:
raise NameError("Abrupt file termination")
# Build the parser
parser = yacc.yacc(tabmodule="preparser")