-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
447 lines (413 loc) · 9.31 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
import sys
import tokenize
symbolTable, inp_list = None, None
temp_count = 0
label_count = 0
i = 0
code = ""
def getNextToken():
global i
if i==len(inp_list):
#print(i,-1)
return -1
i+=1
return inp_list[i-1].id
def num(s):
try:
int(s)
return True
except ValueError:
return False
def parse():
global symbolTable, inp_list
symbolTable, inp_list=tokenize.generateTokens()
#for i in inp_list:
#i.printToken()
inp_list = inp_list[5:-4] #to ignore int main(){ return 0;}
if(Statement()):
print(code)
#symbolTable.display_table()
else:
print("Syntax error")
def Statements():
if Statement():
if Statements():
return True
else:
return False
elif i == len(inp_list)-1:
return True
else:
return False
def Statement():
global i, temp_count, label_count, code
next=getNextToken()
if next =="while":
label_count += 1
code += "L"+str(label_count)+":"
next = getNextToken()
if next == '(':
if Cond():
next = getNextToken()
if next == ')':
code += "\nifFalse t" + str(temp_count)+ " goto L"+str(label_count+1)
next = getNextToken()
if next == '{':
if Statements():
next = getNextToken()
if next=='}':
code += "\ngoto L"+str(label_count)+"\n"
label_count += 1
code += "L"+str(label_count)+":\n"
return True
else:
print("Expected matching } in while")
sys.exit(1)
else:
sys.exit(1)
return False
else:
print("Expected { after ) in while")
sys.exit(1)
return False
else:
print("Expected matching ) for while")
sys.exit(1)
return False
else:
sys.exit(1)
return False
else:
print("Expected ( after while")
return False
#S-> ;S1
elif next==";":
new = getNextToken()
i -= 1
if Statements():
return True
else:
print("Missing semicolon")
return False
elif Decl():
next = getNextToken()
if next==";":
if Statements():
return True
else:
print("Missing semic")
return False
return True
elif Assign():
return True
else:
return False
def Decl():
global i
i -= 1
ret, inferred_type, size = Type()
if ret:
if Var(inferred_type, size):
return True
else:
return False
else:
return False
def Type():
global i
next = getNextToken()
if next=="int":
return True, "int", 4
elif next=="float":
return True, "float", 4
elif next=="char":
return True, "char", 1
elif next=="long":
return True, "long", 8
else:
i -= 1
return False, False, False
def Var(inferred_type, size):
global symbolTable, i
next = getNextToken()
symbolTable.setDataType(next,inferred_type)
symbolTable.setSize(next,size)
if Var1(inferred_type, size):
next = getNextToken()
if next == ';':
i-=1
return True
else:
i-=1
print("Missing semicolons")
sys.exit(1)
return False
def Var1(inferred_type, size):
global i
next = getNextToken()
if next == ',':
if Var(inferred_type, size):
return True
else:
return False
else:
i -= 1
return True
def Assign():
global i, code
next=getNextToken()
temp = next
if not num(next) and symbolTable.isVar(next):
next=getNextToken()
if next=="=":
if E0():
code += "\n"+temp + "= t"+str(temp_count)
return True
else:
return False
else:
return False
else:
i-=1
return False
return False
'''
+--------------------------------------+
| |
| BRACE YOURSELVES. |
| WINTER IS HERE. |
| |
+--------------------------------------+
'''
def Cond():
return E0()
def E0():
global temp_count, code
temp_count += 1
code += "\nt"+str(temp_count)+" = "
if E1():
if E01():
return True
else:
return False
else:
return False
def E01():
global i, code
#E01-> '||'E1E01 | e
next=getNextToken()
if next=="||":
code += next
if E1():
if E01():
return True
else:
return False
else:
return False
else:
i-=1
return True
def E1():
#E1-> E20E11
if E20():
if E11():
return True
else:
return False
else:
return False
def E11():
#E11-> &&E20E11 | e
global i, code
next=getNextToken()
if next=="&&":
code += next
if E20():
if E11():
return True
else:
return False
else:
return False
else:
i-=1
return True
def E20():
#E20-> E2E201
if E2():
if E201():
return True
else:
return False
else:
return False
def E201():
#E201-> ==E2E201 | !=E2E201 | e
global i, code
next=getNextToken()
if next=="==":
code += next
if E2():
if E201():
return True
else:
return False
else:
return False
elif next=="!=":
code += next
if E2():
if E201():
return True
else:
return False
else:
return False
else:
i-=1
return True
def E2():
#E2-> E3E21
if E3():
if E21():
return True
else:
return False
else:
return False
def E21():
#E21-> >E22 | <E22 | e
global i, code
next=getNextToken()
if next==">":
code += next
if E22():
return True
else:
return False
elif next=="<":
code += next
if E22():
return True
else:
return False
else:
i-=1
return True
def E22():
#E22-> E3E21 | =E3E21
if E3():
if E21():
return True
else:
return False
else:
next=getNextToken()
if next=="=":
code += next
if E3():
if E21():
return True
else:
return False
else:
return False
else:
return False
def E3():
#E3-> E4E31
if E4():
if E31():
return True
else:
return False
else:
return False
def E31():
#E31-> +E4E31 | -E4E31 | e
global i, code
next=getNextToken()
if next=="+":
code += next
if E4():
if E31():
return True
else:
return False
else:
return False
elif next=="-":
code += next
if E4():
if E31():
return True
else:
return False
else:
return False
else:
i-=1
return True
def E4():
#E4-> E5E41
if E5():
if E41():
return True
else:
return False
else:
return False
def E41():
#E41-> *E5E41 | /E5E41 | e
global i, code
next=getNextToken()
if next=="*":
code += next
if E5():
if E41():
return True
else:
return False
else:
return False
elif next=="/":
code += next
if E5():
if E41():
return True
else:
return False
else:
return False
else:
i-=1
return True
def E5():
#E5->(E0) | id | num
global i, symbolTable, code
next=getNextToken()
if next=="(":
if E0():
next=getNextToken()
if next==")":
return True
else:
print("Missing paranthesis")
sys.exit(1)
return False
else:
return False
elif num(next):
code += next
return True
elif not num(next):
if symbolTable.isVar(next):
code += next
return True
else:
print("Unrecognized identifier "+next)
sys.exit(1)
return False
else:
return False