-
Notifications
You must be signed in to change notification settings - Fork 1
/
prules.py
366 lines (293 loc) · 6.65 KB
/
prules.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
#!/usr/bin/env python # -*- coding: utf-8
#
#* -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.
# File Name : parserules.py
# Creation Date : 02-04-2012
# Last Modified : Sat 12 May 2012 09:12:40 PM EEST
#_._._._._._._._._._._._._._._._._._._._._.*/
from tokrules import *
import ply.yacc as yacc
from tree import *
from sys import argv
perrors = []
precedence = ( ('nonassoc','If'),
('nonassoc','Else'),
('nonassoc',','),
('right','ASSIGN'),
('left','OR'),
('left','^'),
('left','BINAND'),
('left','EQ','NOTEQ'),
('left','>','<','LEQ','GEQ'),
('left','+','-'),
('left','*','/','%'),
('nonassoc','UNAR','Size'),
('right','[',']','(',')')
)
start = 'program'
def gen_p_out(title,p,symbol=None,ptype=None):
children = []
for i in p[1:]:
children.append(i)
r = node(title, {'symbol':symbol,'title':title, 'type':ptype}, children)
return r
def p_empty(p):
'''empty :'''
#p[0] = gen_p_out('empty',p)
pass
def p_error(p):
perrors.append(p)
while 1:
tok = yacc.token()
if not tok or tok.value == '}': break
yacc.restart()
def p_program(p):
'''
program : def
'''
p[0] = TranslationUnit(p[1])
def p_program_2(p):
'''
program : def program
'''
p[0] = p[1].add(p[2])
def p_def_v(p):
'''
def : var_def ';'
'''
p[0] = p[1]
def p_def_void(p):
'''
def : Void Id '(' ')' block
'''
p[0] = FunctionDefn(Declaration(p[2],'void'),p[5])
def p_def_void_params(p):
'''
def : Void Id '(' formal_params ')' block
'''
p[0] = FunctionExpression(FunctionDefn(Declaration(p[2],'void'),p[6]),p[4])
def p_def_ext(p):
'''
def : type Id '(' ')' block
'''
p[0] = FunctionDefn(Declaration(p[2],p[1]),p[5])
def p_def_ext_params(p):
'''
def : type Id '(' formal_params ')' block
'''
p[0] = FunctionExpression(FunctionDefn(Declaration(p[2],p[1]),p[6]),p[4])
def p_var_def(p):
'''
var_def : type Id
'''
p[0] = Declaration(p[2],p[1])
def p_var_def_assign(p):
'''
var_def : type Id ASSIGN expr
'''
p[0] = Binop(Declaration(p[2],p[1]),p[4],p[3])
def p_type(p):
'''
type : simple_type
'''
p[0] = p[1]
def p_type_2(p):
'''
type : simple_type '[' ']'
'''
p[0] = ArrayOf(p[1])
def p_simple_type(p):
'''
simple_type : Bool
| Int
| Char
'''
p[0] = BaseType(p[1].lower())
def p_formal_params(p):
'''
formal_params : type Id rep_formal_params
'''
p[0] = ParamList(Declaration(p[2],p[1]))
p[0].add(p[3])
def p_rep_formal_params_empty(p):
'''
rep_formal_params : empty
'''
pass
def p_rep_formal_params(p):
'''
rep_formal_params : ',' type Id rep_formal_params
'''
p[0] = ParamList(Declaration(p[3],p[2]))
p[0].add(p[4])
def p_actual_params(p):
'''
actual_params : expr rep_actual_params
'''
p[0] = ArgumentList(p[1])
p[0].add(p[2])
def p_rep_actual_params_empty(p):
'''
rep_actual_params : empty
'''
pass
def p_rep_actual_params(p):
'''
rep_actual_params : ',' expr rep_actual_params
'''
p[0] = ArgumentList(p[2])
p[0].add(p[3])
def p_block(p):
'''
block : '{' '}'
'''
pass
def p_block_full(p):
'''
block : '{' stmt_list '}'
'''
p[0] = p[2]
def p_stmt_list(p):
'''
stmt_list : stmt
'''
p[0] = Statement(p[1])
def p_stmt_list_2(p):
'''
stmt_list : stmt stmt_list
'''
p[0] = StatementList(p[1])
p[0].add(p[2])
def p_stmt_if(p):
'''
stmt : If '(' expr ')' stmt
'''
p[0] = IfStatement(p[3], p[5])
def p_stmt_ifelse(p):
'''
stmt : If '(' expr ')' stmt Else stmt
'''
p[0] = IfElseStatement(p[3], p[5], p[7])
def p_stmt_for(p):
'''
stmt : For '(' var_def ';' expr ';' stmt ')' stmt
'''
p[0] = ForLoop(p[3], p[4], p[5], p[6])
def p_stmt_while(p):
'''
stmt : While '(' expr ')' stmt
'''
p[0] = WhileLoop(p[3], p[5])
def p_stmt(p):
'''
stmt : var_def ';'
| expr ';'
| block
'''
p[0] = p[1]
def p_stmt_del(p):
'''
stmt : Delete expr ';'
'''
p[0] = DeleteStatement([2])
def p_stmt_ret(p):
'''
stmt : Ret expr ';'
| Ret ';'
'''
try:
p[0] = ReturnStatement(p[2])
except:
p[0] = ReturnStatement()
def p_expr(p):
'''
expr : Id
'''
p[0] = Id(p[1],p.lineno)
def p_expr_2(p):
'''
expr : expr '[' expr ']'
'''
p[0] = ArrayExpression(p[1],p[3])
def p_expr_3(p):
'''
expr : Id '(' ')'
'''
p[0] = FunctionExpression(Id(p[1],p.lineno),NodeList(NullNode))
def p_expr_4(p):
'''
expr : Id '(' actual_params ')'
'''
p[0] = FunctionExpression(Id(p[1],p.lineno),p[3])
def p_expr_new(p):
'''
expr : New simple_type '[' expr ']'
'''
p[0] = NewStatement(p[2], p[4])
def p_expr_size(p):
'''
expr : Size expr
'''
p[0] = SizeStatement(p[2])
def p_expr_atom_int(p):
'''
expr : Const_int
'''
p[0] = Const(p[1],BaseType('int'))
def p_expr_atom_char(p):
'''
expr : Const_char
'''
p[0] = Const(p[1],BaseType('char'))
def p_expr_atom_str(p):
'''
expr : Const_str
'''
p[0] = Const(p[1],BaseType('str'))
def p_expr_atom_bool(p):
'''
expr : True
| False
'''
p[0] = Const(p[1],BaseType('bool'))
def p_bin_op(p):
'''
expr : expr ASSIGN expr
'''
p[0] = Binop(p[1], p[3], p[2])
def p_bin_op_int(p):
'''
expr : expr '+' expr
| expr '-' expr
| expr '*' expr
| expr '/' expr
| expr '%' expr
'''
p[0] = Binop(p[1], p[3], p[2])
def p_bin_op_bool(p):
'''
expr : expr EQ expr
| expr NOTEQ expr
| expr '>' expr
| expr '<' expr
| expr GEQ expr
| expr LEQ expr
| expr BINAND expr
| expr OR expr
| expr '^' expr
'''
p[0] = Binop(p[1], p[3], p[2])
def p_un_op(p):
'''
expr : '!' expr %prec UNAR
'''
p[0] = Not(p[2])
def p_un_op_int(p):
'''
expr : '-' expr %prec UNAR
| '+' expr %prec UNAR
'''
if p[1] == '+':
p[0] = Positive(p[2])
else:
p[0] = Negative(p[2])