-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCparser.py
299 lines (259 loc) · 9.49 KB
/
Cparser.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
import AST as ast
from scanner import Scanner
class Cparser(object):
def __init__(self):
self.scanner = Scanner()
self.scanner.build()
tokens = Scanner.tokens
precedence = (
("nonassoc", 'IFX'),
("nonassoc", 'ELSE'),
("right", '='),
("left", 'OR'),
("left", 'AND'),
("left", '|'),
("left", '^'),
("left", '&'),
("nonassoc", '<', '>', 'EQ', 'NEQ', 'LE', 'GE'),
("left", 'SHL', 'SHR'),
("left", '+', '-'),
("left", '*', '/', '%'),
)
def p_error(self, p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno,
self.scanner.find_tok_column(p),
p.type, p.value))
else:
print("Unexpected end of input")
def p_program(self, p):
"""program : segments"""
if len(p) == 2:
p[0] = ast.Program(p.lineno(1), p[1])
# list
def p_segments(self, p):
"""segments : segments segment
| """
if len(p) == 3:
p[0] = p[1]
p[0].append(p[2])
elif len(p) == 1:
p[0] = []
# 1-1
def p_segment(self, p):
"""segment : declaration
| function_definition
| instruction """
if len(p) == 2:
p[0] = p[1]
def p_declaration(self, p):
"""declaration : TYPE inits ';'
| error ';' """
if len(p) == 4:
p[0] = ast.Declaration(p.lineno(1), p[1], p[2])
# list
def p_inits(self, p):
"""inits : inits ',' init
| init """
if len(p) == 4:
p[0] = p[1]
p[0].append(p[3])
elif len(p) == 2:
p[0] = [p[1]]
def p_init(self, p):
"""init : ID '=' expression """
if len(p) == 4:
p[0] = ast.Init(p.lineno(1), p[1], p[3])
# list
def p_instructions(self, p):
"""instructions : instructions instruction
| instruction """
if len(p) == 3:
p[0] = p[1]
p[0].append(p[2])
elif len(p) == 2:
p[0] = [p[1]]
# 1-1
def p_instruction(self, p):
"""instruction : print_instruction
| labeled_instruction
| assignment
| choice_instruction
| while_instruction
| repeat_instruction
| return_instruction
| break_instruction
| continue_instruction
| compound_instructions
| expression ';'"""
if len(p) == 2 or len(p) == 3:
p[0] = p[1]
def p_print_instruction(self, p):
"""print_instruction : PRINT expr_list ';'
| PRINT error ';' """
if len(p) == 4:
p[0] = ast.PrintInstruction(p.lineno(1), p[2])
def p_labeled_instruction(self, p):
"""labeled_instruction : ID ':' instruction """
p[0] = ast.LabeledInstruction(p.lineno(1), p[1], p[3])
def p_assignment(self, p):
"""assignment : ID '=' expression ';' """
if len(p) == 5:
identifier = ast.Identifier(p.lineno(1), p[1])
p[0] = ast.Assignment(p.lineno(1), identifier, p[3])
def p_choice_instruction(self, p):
"""choice_instruction : IF '(' condition ')' instruction %prec IFX
| IF '(' condition ')' instruction ELSE instruction
| IF '(' error ')' instruction %prec IFX
| IF '(' error ')' instruction ELSE instruction """
if len(p) >= 6:
p[0] = ast.ChoiceInstruction(p.lineno(1), p[3], p[5], None)
if len(p) == 8:
p[0] = ast.ChoiceInstruction(p.lineno(1), p[3], p[5], p[7])
def p_while_instruction(self, p):
"""while_instruction : WHILE '(' condition ')' instruction
| WHILE '(' error ')' instruction """
if len(p) == 6:
p[0] = ast.WhileInstruction(p.lineno(1), p[3], p[5])
def p_repeat_instruction(self, p):
"""repeat_instruction : REPEAT instructions UNTIL condition ';' """
if len(p) == 6:
p[0] = ast.RepeatInstruction(p.lineno(1), p[2], p[4])
def p_return_instruction(self, p):
"""return_instruction : RETURN expression ';' """
if len(p) == 4:
p[0] = ast.ReturnInstruction(p.lineno(1), p[2])
def p_break_instruction(self, p):
"""break_instruction : BREAK ';' """
if len(p) == 3:
p[0] = ast.BreakInstruction(p.lineno(1))
def p_continue_instruction(self, p):
"""continue_instruction : CONTINUE ';' """
if len(p) == 3:
p[0] = ast.ContinueInstruction(p.lineno(1))
def p_compound_instructions(self, p):
"""compound_instructions : '{' compound_segments '}' """
if len(p) == 4:
p[0] = ast.CompoundInstructions(p.lineno(1), p[2], p.lineno(3))
# list
def p_compound_segments(self, p):
"""compound_segments : compound_segments compound_segment
| """
if len(p) == 3:
p[0] = p[1]
p[0].append(p[2])
elif len(p) == 1:
p[0] = []
# 1-1
def p_compound_segment(self, p):
"""compound_segment : declaration
| instruction """
if len(p) == 2:
p[0] = p[1]
# 1-1
def p_condition(self, p):
"""condition : expression"""
if len(p) == 2:
p[0] = p[1]
# 1-1
def p_const(self, p):
"""const : integer
| float
| string """
if len(p) == 2:
p[0] = p[1]
def p_integer(self, p):
"""integer : INTEGER"""
if len(p) == 2:
p[0] = ast.Integer(p.lineno(1), p[1])
def p_float(self, p):
"""float : FLOAT"""
if len(p) == 2:
p[0] = ast.Float(p.lineno(1), p[1])
def p_string(self, p):
"""string : STRING"""
if len(p) == 2:
p[0] = ast.String(p.lineno(1), p[1])
def p_identifier_expression(self, p):
"""identifier_expression : ID"""
p[0] = ast.Identifier(p.lineno(1), p[1])
def p_function_call_expression(self, p):
"""function_call_expression : ID '(' expr_list_or_empty ')'
| ID '(' error ')' """
p[0] = ast.FunctionCallExpression(p.lineno(1), p[1], p[3])
# 1-1
def p_expression(self, p):
"""expression : binary_expression
| '(' expression ')'
| '(' error ')'
| identifier_expression
| function_call_expression
| const """
if len(p) == 4:
if p[1] == '(':
p[0] = p[2]
else:
p[0] = p[1]
def p_binary_expression(self, p):
"""binary_expression : expression '+' expression
| expression '-' expression
| expression '*' expression
| expression '/' expression
| expression '%' expression
| expression '|' expression
| expression '&' expression
| expression '^' expression
| expression AND expression
| expression OR expression
| expression SHL expression
| expression SHR expression
| expression EQ expression
| expression NEQ expression
| expression '>' expression
| expression '<' expression
| expression LE expression
| expression GE expression """
if len(p) == 4:
p[0] = ast.BinExpr(p.lineno(2), p[1], p[2], p[3])
def p_function_definition(self, p):
"""function_definition : TYPE ID '(' args_list_or_empty ')' compound_instructions """
if len(p) == 7:
p[0] = ast.FunctionDefinition(p.lineno(1), p[1], p[2], p[4], p[6], p[6].end_lineno)
# list
def p_args_list(self, p):
"""args_list : args_list ',' arg
| arg """
if len(p) == 4:
p[0] = p[1]
p[0].append(p[3])
elif len(p) == 2:
p[0] = [p[1]]
def p_arg(self, p):
"""arg : TYPE ID """
if len(p) == 3:
p[0] = ast.Argument(p.lineno(1), p[1], p[2])
# list
def p_expr_list_or_empty(self, p):
"""expr_list_or_empty : expr_list
| """
if len(p) == 2:
p[0] = p[1]
else:
p[0] = []
# list
def p_expr_list(self, p):
"""expr_list : expr_list ',' expression
| expression """
if len(p) == 4:
p[0] = p[1]
p[0].append(p[3])
elif len(p) == 2:
p[0] = [p[1]]
# 1-1
def p_args_list_or_empty(self, p):
"""args_list_or_empty : args_list
| """
if len(p) == 2:
p[0] = p[1]
elif len(p) == 1:
p[0] = []