-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathparser.py
More file actions
318 lines (283 loc) · 5.87 KB
/
parser.py
File metadata and controls
318 lines (283 loc) · 5.87 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
import ply.yacc as yacc
import logging
from lexer import LA
from lexer import symbol_table
import lexer
tokens=LA.tokens
#Precedence rules for parsing
precedence = (
('left', 'plus', 'minus'),
('left', 'times', 'divide'),
)
#Grammar for language(Assignment,Selection & loop)
end_label=[]
queue=[]
queue_cond=[]
queue_labels=[]
temp_counter=0
label_counter=0
def p_PROGRAM(p):
'''PROGRAM : BLOCKSTMT
'''
r=open('icg.txt','w')
r.write(p[1])
def p_BLOCKSTMT(p):
'''BLOCKSTMT : T STMT T BLOCKSTMT
| T STMT T
'''
if(len(p)==5):
if(p[2] is None):
p[2]=''
if(p[4] is None):
p[4]=''
p[0]=p[2] + '\n' + p[4]
elif(len(p)==4):
if(p[2] is None):
p[2]=''
if('\n' in p[2]):
p[0]=p[2]
else:
p[0]=p[2]
def p_T(p):
'''T : T newline
|
'''
def p_STMT(p):
'''STMT : ASSGN
| SELECT
| ITER
'''
p[0]=p[1]
def p_SELECT(p):
'''SELECT : if CONDEXPR EMPTQC then_tok T BLOCKSTMT LABELMAKER LABEL_E ELSIF ELSE end
'''
global end_label
a=end_label.pop()
p[0] = p[3]+'ifFalse ' + str(p[2]) +' '+ 'goto' + ' ' +p[7][:-1] +'\n'+ str(p[6])+'\n'+'goto '+ a + '\n'+ str(p[7]) + str(p[9])+ '\n' + str(p[10])+'\n' + a + ':'
def p_LABEL_E(p):
'''LABEL_E :'''
global label_counter
end_label.append('L'+str(label_counter))
label_counter+=1
def p_EMPTQC(p):
'''EMPTQC :'''
temp2=''
flag=False
if(len(queue_cond)!=0):
a=queue_cond.pop(0)
flag=True
while(len(queue_cond)!=0):
temp2+=a+'\n'
a=queue_cond.pop(0)
if(flag):
temp2+=a
p[0]=temp2
def p_LABELMAKER(p):
'''LABELMAKER :'''
global label_counter
p[0]="L" + str(label_counter)+':'
label_counter+=1
def p_ELSIF(p):
'''ELSIF : elsif CONDEXPR then_tok T BLOCKSTMT LABELMAKER
|
'''
if len(p)==1 :
p[0]=''
else:
#p[0]= str(p[2])
temp2=''
flag=False
if(len(queue_cond)!=0):
a=queue_cond.pop(0)
flag=True
while(len(queue_cond)!=0):
temp2+=a+'\n'
a=queue_cond.pop(0)
if(flag):
temp2+=a
p[0]= temp2+'ifFalse ' + str(p[2])+' goto '+str(p[6][:-1]) + '\n' + str(p[5]) +'\n' + 'goto '+ end_label[-1] +'\n' +str(p[6])
def p_ELSE(p):
'''ELSE : else BLOCKSTMT
|
'''
if len(p)==1 :
p[0]=''
else:
p[0]= str(p[2])
def p_CONDEXPR(p):
'''CONDEXPR : EXPR less EXPR
| EXPR equals equals EXPR
| EXPR great EXPR
| EXPR great equals EXPR
| EXPR less equals EXPR
'''
if(len(p)==4):
flag=False
if(len(queue)!=0):
a=queue.pop(0)
flag=True
while(len(queue)!=0):
queue_cond.append(a)
a=queue.pop(0)
if(flag):
queue_cond.append(a)
#print(p[1],"hello")
if(('=' in str(p[1])) or ('=' in str(p[3]))):
temp1=''
#print(p[1],"hello")
if('=' in str(p[1])):
i=''
for i in str(p[1]):
if '=' not in i:
temp1+=i
else:
break
queue_cond.append(p[1])
p[1]=temp1
#print(p[1],"hello")
temp2=''
if('=' in str(p[3])):
i=''
for i in str(p[3]):
if '=' not in i:
temp2+=i
else:
break
queue_cond.append(p[3])
p[3]=temp2
#print(p[1],p[3])
p[0] = str(p[1]) + ' ' + str(p[2]) + ' ' + str(p[3])
else:
flag=False
if(len(queue)!=0):
a=queue.pop(0)
flag=True
while(len(queue)!=0):
queue_cond.append(a)
a=queue.pop(0)
if(flag):
queue_cond.append(a)
#print(p[1],"hello")
if(('=' in str(p[1])) or ('=' in str(p[3]))):
temp1=''
#print(p[1],"hello")
if('=' in str(p[1])):
i=''
for i in str(p[1]):
if '=' not in i:
temp1+=i
else:
break
queue_cond.append(p[1])
p[1]=temp1
#print(p[1],"hello")
temp2=''
if('=' in str(p[4])):
i=''
for i in str(p[4]):
if '=' not in i:
temp2+=i
else:
break
queue_cond.append(p[4])
p[4]=temp2
#print(p[1],p[3])
p[0] = str(p[1]) + ' ' + str(p[2]) + str(p[3]) + ' ' + str(p[4])
def p_ASSGN(p):
'''ASSGN : LHS equals EXPR
'''
temp1=''
temp2=''
#print(p[1],"hello")
if('=' in str(p[3])):
i=''
for i in str(p[3]):
if '=' not in i:
temp1+=i
else:
break
#print(p[3])
queue.append(p[3])
p[3]=temp1
#print(p[1],"hello")
a=queue.pop(0)
while(len(queue)!=0):
temp2+=a +'\n'
a=queue.pop(0)
temp2+=a
try:
a=int(p[3])
symbol_table[p[1]]=a
#print('symbol table updated')
#print(symbol_table)
except:
symbol_table[str(p[1])]='defined'
p[0]=temp2 + str(p[1]) + ' ' + str(p[2]) + ' ' + str(p[3])
def p_LHS(p):
'''LHS : name
'''
p[0]=p[1]
#print(p[1])
def p_EXPR(p):
'''EXPR : EXPR plus EXPR
| EXPR minus EXPR
| EXPR times EXPR
| EXPR divide EXPR
| name
| number
'''
if(len(p)==4):
global temp_counter
if(('=' in str(p[1])) or ('=' in str(p[3]))):
temp1=''
#print(p[1],"hello")
if('=' in str(p[1])):
i=''
for i in str(p[1]):
if '=' not in i:
temp1+=i
else:
break
queue.append(p[1])
p[1]=temp1
#print(p[1],"hello")
temp2=''
if('=' in str(p[3])):
i=''
for i in str(p[3]):
if '=' not in i:
temp2+=i
else:
break
queue.append(p[3])
p[3]=temp2
#print(p[1],p[3])
p[0]='t'+str(temp_counter)+' = '+str(p[1])+' '+str(p[2])+' '+str(p[3]) + '\n'
temp_counter+=1
elif(len(p)==2): #check in symbol table here
try:
int(p[1])
except ValueError:
if(symbol_table[p[1]]==None):
raise ValueError("Variable "+ str(p[1])+ ' has not been defined')
p[0]=p[1]
#check for nos here
def p_ITER(p):
'''ITER : while LABELMAKER CONDEXPR EMPTQC do T BLOCKSTMT T end LABELMAKER
'''
p[0]= str(p[2]) + str(p[4]) + '\n' +'ifFalse ' + p[3] + ' goto ' + p[10][:-1] + '\n' + p[7] + '\n' + 'goto ' + str(p[2][:-1]) + '\n' + p[10]
logging.basicConfig(
level = logging.DEBUG,
filename = "parselog.txt",
filemode = "w",
format = "%(filename)10s:%(lineno)4d:%(message)s"
)
log = logging.getLogger()
# Build the parser
parser = yacc.yacc()
s = open('test.rb','r').read()
result = parser.parse(s,debug=log)
#print(log)
if result is not None:
with open("AST.txt",'w') as f:
f.write(str(result))