-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.py
389 lines (354 loc) · 11.1 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
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
import lexer
import ply.lex as lex
import ply.yacc as yacc
import re
import sys
precedence = (
('left', 'OR'),
('left', 'AND'),
('left', 'ISEQUAL'),
('left', 'LT', 'LE', 'GT', 'GE'),
('left', 'PLUS', 'MINUS'),
('left', 'MULTIPLY', 'DIVIDE'),
('left', 'POWER'),
('right', 'NOT'),
)
tokens = (
'DOT',
'STRUCT',
'LBRACE',
'RBRACE',
'VARTYPE',
'INT', #done
'DOUBLE', #done
'CHAR',
'STRING',
'BOOL',
'IDENTIFIER',
'COMMA',
'SEMICOLON',
'PLUS', #done
'MINUS', #done
'DIVIDE', #done
'MULTIPLY', #done
'POWER', #done
'MODULO', #done
'INCREMENT', #done
'DECREMENT', #done
'LT', #done
'GT', #done
'LE', #done
'GE', #done
'EQUAL',
'NOTEQUAL', #done
'ISEQUAL', #done
'NOT',
'AND', #done
'OR', #done
'LPARAN',
'RPARAN',
'PRINT',
)
def p_stmt_multiline(p):
'line : stmts'
p[0] = ("statements", p[1])
def p_stmt_end(p):
'''stmts : stmt SEMICOLON
| stmt SEMICOLON stmts
'''
if len(p) == 3:
p[0] = [p[1]]
else:
p[0] = [p[1]] + p[3]
def p_struct_variable(p):
'''
stmt : IDENTIFIER IDENTIFIER
'''
p[0] = ('declarestruct', p[1], p[2])
def p_struct_members_init(p):
'''
stmt : IDENTIFIER DOT IDENTIFIER EQUAL exp
'''
p[0] = ('initmember', p[1], p[3], p[5])
def p_struct(p):
'''
stmt : STRUCT IDENTIFIER LBRACE assignments RBRACE
'''
p[0] = ('initstruct', p[2], p[4])
def p_struct_initialisation(p):
'''
assignments : VARTYPE IDENTIFIER SEMICOLON
| VARTYPE IDENTIFIER SEMICOLON assignments
'''
if len(p) == 4:
p[0] = [[p[1],p[2]]]
else:
p[0] = [[p[1],p[2]]] + p[4]
def p_stmt_exp(p):
'stmt : exp'
p[0] = p[1]
def p_stmt_assign(p):
'stmt : VARTYPE IDENTIFIER EQUAL exp'
p[0] = ("assign", p[1],p[2],p[4])
def p_stmt_change(p):
'stmt : IDENTIFIER EQUAL exp'
p[0] = ("change", p[1],p[3])
def p_stmt_print(p):
'stmt : PRINT LPARAN exps RPARAN'
p[0] = ("printexps",p[3])
def p_struct_print(p):
'stmt : PRINT LPARAN IDENTIFIER DOT IDENTIFIER RPARAN'
p[0] = ("printstruct", p[3], p[5])
def p_stmt_many_exps(p):
'''exps : exp
| exp COMMA exps
'''
if len(p) == 2:
p[0] = [p[1]]
else:
p[0] = [p[1]] + p[3]
def p_stmt_increment(p):
'''stmt : IDENTIFIER INCREMENT'''
p[0] = ('increment', p[1])
def p_stmt_decrement(p):
'''stmt : IDENTIFIER DECREMENT'''
p[0] = ('decrement', p[1])
def p_exp_neg_number(p):
'''exp : MINUS IDENTIFIER'''
p[0] = ('negate', p[2])
def p_exp_binop(p):
'''
exp : exp PLUS exp
| exp MINUS exp
| exp MULTIPLY exp
| exp DIVIDE exp
| exp MODULO exp
| exp POWER exp
| exp LE exp
| exp GE exp
| exp ISEQUAL exp
| exp NOTEQUAL exp
| exp LT exp
| exp GT exp
| exp AND exp
| exp OR exp'''
p[0] = ("binop", p[1], p[2], p[3])
def p_exp_paran(p):
'exp : LPARAN exp RPARAN'
p[0] = p[2]
def p_exp_not(p):
'exp : NOT exp'
p[0] = ("NOT", p[2])
def p_exp_int(p):
'exp : INT'
p[0] = ("int",p[1])
def p_exp_double(p):
'exp : DOUBLE'
p[0] = ("double",p[1])
def p_exp_char(p):
'exp : CHAR'
p[0] = ("char", p[1])
def p_exp_string(p):
'exp : STRING'
p[0] = ("string", p[1])
def p_exp_bool(p):
'exp : BOOL'
p[0] = ("bool", p[1])
def p_exp_identifier(p):
'exp : IDENTIFIER'
p[0] = ("identifier", p[1])
def p_error(p):
if p == None:
token = "end of file"
else:
token = f"{p.type}({p.value}) on line {p.lineno}"
print(f"Syntax error: Unexpected {token}")
storage = {}
structure = {}
def evaluate(tree, store):
global storage
nodetype = tree[0]
if nodetype == 'int':
return int(tree[1])
elif nodetype == 'identifier':
var = tree[1]
if var in storage:
return storage[var][1]
else:
return var
elif nodetype == 'double':
return float(tree[1])
elif nodetype == 'char':
return tree[1]
elif nodetype == 'string':
return tree[1]
elif nodetype == 'bool':
return tree[1]
elif nodetype == 'binop':
binop, left_exp, right_exp = tree[2], tree[1], tree[3]
if binop == '+':
return evaluate(left_exp, store) + evaluate(right_exp, store)
elif binop == '-':
return evaluate(left_exp, store) - evaluate(right_exp, store)
elif binop == '/':
return evaluate(left_exp, store) / evaluate(right_exp, store)
elif binop == '*':
return evaluate(left_exp, store) * evaluate(right_exp, store)
elif binop == '^':
return evaluate(left_exp, store) ** evaluate(right_exp, store)
elif binop == '%':
return evaluate(left_exp, store) % evaluate(right_exp, store)
elif binop == '<=':
return evaluate(left_exp, store) <= evaluate(right_exp, store)
elif binop == '>=':
return evaluate(left_exp, store) >= evaluate(right_exp, store)
elif binop == '==':
return evaluate(left_exp, store) == evaluate(right_exp, store)
elif binop == '!=':
return evaluate(left_exp, store) != evaluate(right_exp, store)
elif binop == '<':
return evaluate(left_exp, store) < evaluate(right_exp, store)
elif binop == '>':
return evaluate(left_exp, store) > evaluate(right_exp, store)
elif binop == '&&':
return evaluate(left_exp, store) and evaluate(right_exp, store)
elif binop == '||':
return evaluate(left_exp, store) or evaluate(right_exp, store)
elif nodetype == 'NOT':
return not(evaluate(tree[1], store))
elif nodetype == 'assign':
var_name = tree[2]
if var_name in storage:
print("RedeclarationError")
sys.exit()
else:
var_type = tree[1]
var_value = evaluate(tree[3], store)
if var_type == 'STRING':
if type(var_value) is str:
storage[var_name] = [str,var_value]
else:
print('Invalid assignment')
sys.exit()
elif var_type == 'INT':
if type(var_value) is int:
storage[var_name] = [int,var_value]
else:
print('Invalid assignment')
sys.exit()
elif var_type == 'CHAR':
if type(var_value) is str and len(var_name) == 1:
storage[var_name] = [str,var_value]
else:
print('Invalid assignment')
sys.exit()
elif var_type == 'DOUBLE':
if type(var_value) is float:
storage[var_name] = [float,var_value]
else:
print('Invalid assignment')
sys.exit()
elif var_type == 'BOOL':
if var_value == 'TRUE':
storage[var_name] = [bool,True]
elif var_value == 'FALSE':
storage[var_name] = [bool,False]
else:
print('Invalid assignment')
sys.exit()
elif nodetype == 'printexps':
exp_list = tree[1]
for exp in exp_list:
print(evaluate(exp,storage),end=" ")
print()
elif nodetype == 'statements':
stmts_list = tree[1]
for stmt in stmts_list:
result = evaluate(stmt, storage)
if result is not None:
print(result)
elif nodetype == 'change':
var_name = tree[1]
var_val = tree[2]
if var_name not in storage:
print('variable "{}" does not exist'.format(var_name))
sys.exit()
else:
var_type = type(tree[2][1])
if storage[var_name][0] == var_type:
storage[var_name][1] = var_val[1]
else:
print("types do not match")
sys.exit()
elif nodetype == 'increment':
var_name = tree[1]
if var_name in storage and type(storage[var_name][1]) is int:
storage[var_name][1] = storage[var_name][1] + 1
else:
print('variable "{}" does not exist'.format(var_name))
sys.exit()
elif nodetype == 'decrement':
var_name = tree[1]
if var_name in storage and type(storage[var_name][1]) is int:
storage[var_name][1] = storage[var_name][1] - 1
else:
print('variable "{}" does not exist'.format(var_name))
sys.exit()
elif nodetype == 'negate':
var_name = tree[1]
if var_name not in storage:
print('variable "{}" does not exist'.format(var_name))
sys.exit()
else:
if type(storage[var_name][1]) is float or type(storage[var_name][1]) is int:
return -storage[var_name][1]
elif nodetype == 'initstruct':
struct_name = tree[1]
if struct_name in storage:
print('A struct by name {} already exists'.format(struct_name))
sys.exit()
else:
global structure
temp_struct = []
type_map = {'STRING':str, 'INT':int, 'BOOL':bool, 'DOUBLE':float, 'CHAR':str }
for i in range(len(tree[2])):
temp_struct.append([tree[2][i][1],type_map[tree[2][i][0]]])
structure[struct_name] = temp_struct
elif nodetype == 'declarestruct':
struct_type = tree[1]
struct_name = tree[2]
if struct_type not in structure:
print('A struct by name {} does not exist'.format(struct_type))
sys.exit()
else:
if struct_name in storage:
print("RedeclarationError")
sys.exit()
else:
temp_strct = {}
for i in range(len(structure[struct_type])):
temp_strct[structure[struct_type][i][0]]=None
storage[struct_name] = temp_strct
elif nodetype == 'initmember':
struct_name = tree[1]
struct_member = tree[2]
member_val = evaluate(tree[3],store)
if struct_name not in storage:
print('variable "{}" does not exist'.format(struct_name))
sys.exit()
else:
if struct_member not in store[struct_name]:
print("AttributeError")
sys.exit()
store[struct_name][struct_member] = member_val
elif nodetype == 'printstruct':
struct_name = tree[1]
member_name = tree[2]
if struct_name not in storage:
print('variable "{}" does not exist'.format(struct_name))
sys.exit()
else:
if member_name not in store[struct_name]:
print("AttributeError")
sys.exit()
print(store[struct_name][member_name])