-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.py
More file actions
418 lines (386 loc) · 12.6 KB
/
Parser.py
File metadata and controls
418 lines (386 loc) · 12.6 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
from lexicalAnalyzer import screen
from Structures import Stack
from TreeNode import *
stack = Stack("AST")
def parse(input_file):
global tokens
tokens, has_invalid, invalid_token = screen(input_file)
# If there are invalid tokens, parsing cannot proceed.
match has_invalid:
case True:
print("Invalid token present")
exit(1)
case False:
E()
match stack.Empty():
case False:
root = stack.pop()
case True:
print("Stack is empty")
exit(1)
return root
# This function builds the abstract syntax tree.
def constructAST(node_value, child_count):
ast_node = TreeNode(node_value)
ast_node.childList = [None] * child_count
index = 0
while index < child_count:
match stack.Empty():
case True:
print("Stack is empty")
exit(1)
case False:
ast_node.childList[child_count - index - 1] = stack.pop()
index += 1
stack.push(ast_node)
# This function checks and processes the expected token.
def consumeToken(expected_value):
match tokens[0].content == expected_value:
case False:
print("Syntax error")
exit(1)
case True:
match tokens[0].is_last_token:
case False:
del tokens[0]
case True:
match tokens[0].type == ")":
case False:
tokens[0].type = ")"
case True:
pass
# This function prints the abstract syntax tree in preorder traversal.
def printAST(tree_root):
preorderTraversal(tree_root)
##############################################################
def E():
switch_token = tokens[0].content
match switch_token:
case "let":
consumeToken("let")
D()
if tokens[0].content == "in":
consumeToken("in")
E()
constructAST("let", 2)
else:
print("Syntax error in line " + str(tokens[0].line) + ": 'in' expected")
exit(1)
case "fn":
consumeToken("fn")
n = 0
for _ in iter(lambda: tokens[0].type in ["<IDENTIFIER>", "("], False):
Vb()
n += 1
if n == 0:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier or '(' expected")
exit(1)
if tokens[0].content == ".":
consumeToken(".")
E()
constructAST("lambda", n + 1)
else:
print("Syntax error in line " + str(tokens[0].line) + ": '.' expected")
exit(1)
case _:
Ew()
##############################################################
def Ew():
T()
match tokens[0].content:
case "where":
consumeToken("where")
Dr()
constructAST("where", 2)
case _:
pass
##############################################################
def T():
Ta()
n = 0
for _ in iter(lambda: tokens[0].content == ",", False):
consumeToken(",")
Ta()
n += 1
if n > 0:
constructAST("tau", n + 1)
##############################################################
def Ta():
Tc()
for _ in iter(lambda: tokens[0].content == "aug", False):
consumeToken("aug")
Tc()
constructAST("aug", 2)
##############################################################
def Tc():
B()
match tokens[0].content:
case "->":
consumeToken("->")
Tc()
if tokens[0].content == "|":
consumeToken("|")
Tc()
constructAST("->", 3)
else:
print("Syntax error in line " + str(tokens[0].line) + ": '|' expected")
exit(1)
case _:
pass
##############################################################
def B():
Bt()
for _ in iter(lambda: tokens[0].content == "or", False):
consumeToken("or")
Bt()
constructAST("or", 2)
##############################################################
def Bt():
Bs()
for _ in iter(lambda: tokens[0].content == "&", False):
consumeToken("&")
Bs()
constructAST("&", 2)
##############################################################
def Bs():
match tokens[0].content:
case "not":
consumeToken("not")
Bp()
constructAST("not", 1)
case _:
Bp()
##############################################################
def Bp():
A()
match tokens[0].content:
case "gr" | ">":
consumeToken(tokens[0].content)
A()
constructAST("gr", 2)
case "ge" | ">=":
consumeToken(tokens[0].content)
A()
constructAST("ge", 2)
case "ls" | "<":
consumeToken(tokens[0].content)
A()
constructAST("ls", 2)
case "le" | "<=":
consumeToken(tokens[0].content)
A()
constructAST("le", 2)
case "eq":
consumeToken(tokens[0].content)
A()
constructAST("eq", 2)
case "ne":
consumeToken(tokens[0].content)
A()
constructAST("ne", 2)
case _:
pass
##############################################################
def A():
match tokens[0].content:
case "+":
consumeToken("+")
At()
case "-":
consumeToken("-")
At()
constructAST("neg", 1)
case _:
At()
for _ in iter(lambda: tokens[0].content in ["+", "-"], False):
match tokens[0].content:
case "+":
consumeToken("+")
At()
constructAST("+", 2)
case "-":
consumeToken("-")
At()
constructAST("-", 2)
##############################################################
def At():
Af()
for _ in iter(lambda: tokens[0].content in ["*", "/"], False):
match tokens[0].content:
case "*":
consumeToken("*")
Af()
constructAST("*", 2)
case "/":
consumeToken("/")
Af()
constructAST("/", 2)
##############################################################
def Af():
Ap()
match tokens[0].content:
case "**":
consumeToken("**")
Af()
constructAST("**", 2)
case _:
pass
##############################################################
def Ap():
R()
for _ in iter(lambda: tokens[0].content == "@", False):
consumeToken("@")
if tokens[0].type == "<IDENTIFIER>":
constructAST("<ID:" + tokens[0].content + ">", 0)
consumeToken(tokens[0].content)
R()
constructAST("@", 3)
else:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier expected")
exit(1)
##############################################################
def R():
Rn()
for _ in iter(lambda: tokens[0].type in ["<IDENTIFIER>", "<INTEGER>", "<STRING>"] or tokens[0].content in ["true", "false", "nil", "(", "dummy"], False):
Rn()
constructAST("gamma", 2)
##############################################################
def Rn():
value = tokens[0].content
match tokens[0].type:
case "<IDENTIFIER>":
consumeToken(value)
constructAST("<ID:" + value + ">", 0)
case "<INTEGER>":
consumeToken(value)
constructAST("<INT:" + value + ">", 0)
case "<STRING>":
consumeToken(value)
constructAST("<STR:" + value + ">", 0)
case _:
match value:
case "true" | "false" | "nil" | "dummy":
consumeToken(value)
constructAST("<" + value + ">", 0)
case "(":
consumeToken("(")
E()
if tokens[0].content == ")":
consumeToken(")")
else:
print("Syntax error in line " + str(tokens[0].line) + ": ')' expected")
exit(1)
case _:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier, Integer, String, 'true', 'false', 'nil', 'dummy' or '(' expected")
exit(1)
##############################################################
def D():
Da()
match tokens[0].content:
case "within":
consumeToken("within")
D()
constructAST("within", 2)
case _:
pass
##############################################################
def Da():
Dr()
n = 0
for _ in iter(lambda: tokens[0].content == "and", False):
consumeToken("and")
Dr()
n += 1
if n > 0:
constructAST("and", n + 1)
##############################################################
def Dr():
match tokens[0].content:
case "rec":
consumeToken("rec")
Db()
constructAST("rec", 1)
case _:
Db()
##############################################################
def Db():
value = tokens[0].content
match value:
case "(":
consumeToken("(")
D()
if tokens[0].content == ")":
consumeToken(")")
else:
print("Syntax error in line " + str(tokens[0].line) + ": ')' expected")
exit(1)
case _ if tokens[0].type == "<IDENTIFIER>":
consumeToken(value)
constructAST("<ID:" + value + ">", 0)
if tokens[0].content in [",", "="]:
Vl()
consumeToken("=")
E()
constructAST("=", 2)
else:
n = 0
for _ in iter(lambda: tokens[0].type in ["<IDENTIFIER>", "("], False):
Vb()
n += 1
if n == 0:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier or '(' expected")
exit(1)
if tokens[0].content == "=":
consumeToken("=")
E()
constructAST("function_form", n + 2)
else:
print("Syntax error in line " + str(tokens[0].line) + ": '=' expected")
exit(1)
case _:
print("Syntax error in line " + str(tokens[0].line) + ": '(' or Identifier expected")
exit(1)
##############################################################
def Vb():
value_1 = tokens[0].content
match tokens[0].type:
case "<IDENTIFIER>":
consumeToken(value_1)
constructAST("<ID:" + value_1 + ">", 0)
case _ if value_1 == "(":
consumeToken("(")
value_2 = tokens[0].content
match value_2:
case ")":
consumeToken(")")
constructAST("()", 0)
case _ if tokens[0].type == "<IDENTIFIER>":
consumeToken(value_2)
constructAST("<ID:" + value_2 + ">", 0)
Vl()
if tokens[0].content == ")":
consumeToken(")")
else:
print("Syntax error in line " + str(tokens[0].line) + ": ')' expected")
exit(1)
case _:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier or ')' expected")
exit(1)
case _:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier or '(' expected")
exit(1)
##############################################################
def Vl():
n = 0
for _ in iter(lambda: tokens[0].content == ",", False):
consumeToken(",")
if tokens[0].type == "<IDENTIFIER>":
value = tokens[0].content
consumeToken(value)
constructAST("<ID:" + value + ">", 0)
n += 1
else:
print("Syntax error in line " + str(tokens[0].line) + ": Identifier expected")
exit(1)
if n > 0:
constructAST(",", n + 1)