-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.py
290 lines (215 loc) · 6.45 KB
/
parser.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
#!/usr/bin/python
import scanner
import ply.yacc as yacc
from ast import *
names_dictionary = {}
tokens = scanner.tokens
precedence = (
('nonassoc', 'IFY'),
('nonassoc', 'ELSE'),
('nonassoc', '<', '>', 'EQ', 'LTE', 'GTE', 'NE',),
('right', '=', 'ADDASSIGN', 'SUBASSIGN', 'MULASSIGN', 'DIVASSIGN'),
("left", '+', '-'),
("left", '*', '/'),
("left", "DOTADD", "DOTSUB"),
("left", "DOTMUL", "DOTDIV"),
)
def p_error(p):
if p:
print("Syntax error at line {0}, column {1}: LexToken({2}, '{3}')".format(p.lineno, "1",
p.type, p.value))
else:
print("Unexpected end of input")
def p_program(p):
"""program : instructions
|
"""
p[0] = p[1]
def p_instructions_1(p):
"""instructions : instructions instruction
| instruction"""
if len(p) == 3:
p[0] = Instr(p[1], p[2])
else:
p[0] = p[1]
def p_instruction(p):
""" instruction : assign_instruction ";"
| print_instruction ";"
| return_instruction ";"
| break_instruction ";"
| continue_instruction ";"
| for_instruction
| while_instruction
| choice_instruction """
p[0] = p[1]
def p_expression_group(p):
'expression : "(" expression ")"'
p[0] = p[2]
def p_assign_instruction(p):
'''assign_instruction : var assign_operand expression
| var assign_operand assign_instruction
| var "=" matrix_init_instruction
'''
p[0] = AssignInstruction(p[2], p[1], p[3])
def p_var(p):
'''
var : ID
| ID '[' INTNUM ']'
| ID '[' INTNUM ',' INTNUM ']'
'''
if len(p) == 2:
p[0] = Variable(p[1])
elif len(p) == 5:
p[0] = SingleMatrixRef(p[1], p[3])
elif len(p) == 7:
p[0] = DoubleMatrixRef(p[1], p[3], p[5])
def p_assign_operand(p):
'''assign_operand : "="
| ADDASSIGN
| SUBASSIGN
| MULASSIGN
| DIVASSIGN
'''
p[0] = p[1]
def p_expression_binop(p):
'''expression : expression "+" expression
| expression "-" expression
| expression "*" expression
| expression "/" expression
| expression "<" expression
| expression ">" expression
| expression DOTADD expression
| expression DOTSUB expression
| expression DOTMUL expression
| expression DOTDIV expression
| expression EQ expression
| expression LTE expression
| expression GTE expression
| expression NE expression '''
p[0] = BinOp(p[1], p[2], p[3])
def p_expression_number(p):
'''expression : INTNUM'''
p[0] = Number(p[1])
def p_expression_float(p):
'''expression : FLOATNUM'''
p[0] = Number(p[1])
def p_expression_var(p):
'''expression : var'''
p[0] = p[1]
# def p_exp_name(p):
# '''expression : ID'''
# try:
# p[0] = names_dictionary[p[1]]
# except LookupError:
# print("Name not defined")
# p[0] = 0
def p_expression_unop(p):
'''expression : "-" expression
| "!" expression
| expression "'"
'''
if p[2] != "'":
p[0] = UnOp(p[1], p[2])
else:
p[0] = UnOp(p[2], p[1])
def p_expression_block(p):
'''block : "{" instructions "}"
| instruction '''
if len(p) == 3:
p[0] = p[2]
else:
p[0] = p[1]
def p_choice_instruction(p):
"""choice_instruction : if_else """
p[0] = p[1]
def p_if_else(p):
'''if_else : IF "(" expression ")" block %prec IFY
| IF "(" expression ")" block ELSE block'''
if len(p) == 7:
p[0] = IfElse(p[3], p[5], p[7])
else:
p[0] = IfElse(p[3], p[5], None)
def p_for_instruction(p):
"""for_instruction : for_1
| for_2"""
p[0] = p[1]
def p_for_1(p):
'for_1 : FOR ID "=" expression ":" expression "{" instructions "}"'
p[0] = ForLoop(p[2], p[4], p[6], p[8])
def p_for_2(p):
'for_2 : FOR ID "=" expression ":" expression instruction '
p[0] = ForLoop(p[2], p[4], p[6], p[7])
def p_while_instruction(p):
"""while_instruction : while_1
| while_2"""
p[0] = p[1]
def p_while_1(p):
'while_1 : WHILE "(" expression ")" instruction'
p[0] = WhileLoop(p[3], p[5])
def p_while_2(p):
'while_2 : WHILE "(" expression ")" "{" instructions "}"'
p[0] = WhileLoop(p[3], p[6])
def p_ones(p):
'ones : ONES "(" INTNUM ")"'
p[0] = Ones(p[3])
def p_zeros(p):
'zeros : ZEROS "(" INTNUM ")"'
p[0] = Zeros(p[3])
def p_eye(p):
'eye : EYE "(" INTNUM ")"'
p[0] = Eye(p[3])
def p_break_instruction(p):
'break_instruction : BREAK'
p[0] = BreakInstruction()
def p_return_instruction(p):
'return_instruction : RETURN'
p[0] = ReturnInstruction()
def p_continue_instruction(p):
'continue_instruction : CONTINUE'
p[0] = ContinueInstruction()
def p_print_instruction(p):
'''print_instruction : PRINT vars_to_print
| PRINT '"' vars_to_print '"' '''
if (p[2] != '"'):
p[0] = PrintInstruction(p[2])
else:
p[0] = PrintInstruction(p[3])
def p_print_list(p):
"""vars_to_print : vars_to_print ',' expression
| expression"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[3]
def p_matrix_init_instruction(p):
'''matrix_init_instruction : matrix_init_fun
| '[' matrix_row ']'
| '[' matrix_rows ']'
'''
if len(p) == 2:
p[0] = p[1]
else:
p[0] = p[2]
def p_matrix_init_fun(p):
'''matrix_init_fun : zeros
| ones
| eye
'''
p[0] = p[1]
def p_matrix_row(p):
"""matrix_row : matrix_row ',' INTNUM
| INTNUM """
p[0] = Row()
if len(p) == 4:
p[0].concat(p[1],p[3])
else:
p[0].append(p[1])
def p_matrix_rows(p):
"""matrix_rows : matrix_row ';' matrix_rows
| matrix_row"""
p[0] = Matrix()
if len(p) == 4:
p[0].concat(p[3], p[1])
else:
p[0].append(p[1])
parser = yacc.yacc()