-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.py
296 lines (185 loc) · 7.19 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
import os
import LexicalAnalyzer as la
from pandas import read_excel
my_sheet = 'Parse Table edited'
file_name = 'ptable.xlsx'
dff = read_excel(file_name, sheet_name = my_sheet)
dff['TRUE'] = dff[True]
dff['FALSE'] = dff[False]
del dff[True]
del dff[False]
df = dff
def tokenProcessor(i):
j = i.split(',')
if j[1] == '':
return [',',j[-1][:-1]]
j[0] = j[0][1:]
j[-1] = j[-1][:-1]
return j
def token2terminal(x):
terminals = []
ls = ['keyword','logical-operator','operator']
for i in x:
j = tokenProcessor(i)
if j[0] in ls:
if j[1] == 'END':
terminals.append(('$',j[-1]))
print('prob:' ,i)
else:
terminals.append((j[1],j[-1]))
## elif j[0] == 'operator':
##
## terminals.append(j[1])
elif j[0] == 'identifier':
if j[0] == '':
print('prob:' ,i)
terminals.append(('identifier',j[-1]))
elif j[0] == 'str':
if j[0] == '':
print('prob:' ,i)
terminals.append(('stringliteral',j[-1]))
elif j[0] == 'int' or j[0] == 'float':
if j[0] == '':
print('prob:' ,i)
terminals.append(('numberliteral',j[-1]))
else:
#print('Not reckoned: ',i)
if j[0] == '':
print('Not reckoned: ',i)
terminals.append((',',j[-1]))
else:
terminals.append((j[0],j[-1]))
return terminals
#tokenList = []
#errorList = []
#symbolTable = []
#filename = input("Input File Name: ")
#prog = open(fname, 'r').read()
#fileName = fname.split('.', 1)[0]
#print(fileName)
#F = [('Identifier'),('(',E),')']
#T0 = [('Op-Multiply',F,T0),('null')]
#T = [(F,T0)]
#E0 = [('Op-Plus',T,E0),('null')]
#E = [(T),(E0)]
import pandas as pd
rws = ['E','E0','T','T0','F']
cls = ['Identifier','Op-Plus','Op-Multiply','(',')','$']
#df = pd.DataFrame( index=rws, columns=cls)
#df = pd.read_pickle('df.pkl')
#rules={'E':''}
case1 = ['*','id','+','(','id','*','(',')','id',')','*','$']
case2 = [')','+','*','(','*','(','id','+','id','id',')','*','id','$']
er=[]
def start():
global er, ptStack, pt
#w = ['(','*',')','+','*','(','*','(','id','+','id','id',')','*','id','$']
w = token2terminal(tokenList)
w = w[::-1]
buffer = ['$','Program']
parseTree = []
match = []
ptStack = []
pt = []
while (w != []):
#parseTree.append(df.loc[buffer[-1],w[-1]])
#print('Buffer,Input: ' ,buffer[-1],w[-1])
#print('Prod: ', df.loc[buffer[-1],w[-1]])
if (buffer[-1] == 'ε'):
del buffer[-1]
if (buffer [-1] not in df.columns):
x = 'Exiting: ' + ptStack[-1]
pt.append(x)
del ptStack[-1]
elif w[-1][0] == buffer [-1]:
match.append(w[-1][0])
x = 'Matched: ' + str(w[-1])
pt.append(x)
del buffer[-1]
del w[-1]
if buffer != []:
if (buffer [-1] not in df.columns):
x = 'Exiting: ' + ptStack[-1]
pt.append(x)
del ptStack[-1]
elif (buffer [-1] in df.columns):
del buffer[-1]
elif (pd.isnull(df.loc[buffer[-1],w[-1][0]]) == False and df.loc[buffer[-1],w[-1][0]] != 'Sync'):
parseTree.append(df.loc[buffer[-1],w[-1][0]])
prod = df.loc[buffer[-1],w[-1][0]]
p = list(filter(None, prod.split('→')[1].split(' ')))
p = list(map(lambda x: x.replace('\xa0',''),p))
x = 'In: ' + buffer[-1]
pt.append(x)
ptStack.append(buffer[-1])
del buffer[-1]
buffer = buffer + p[::-1]
elif (df.loc[buffer[-1],w[-1][0]] == 'Sync'): #Panic Mode error recovery - sync
#Panic Mode error recovery - delete production upon sync match in parse table
if buffer[-2] == '$':
print('rule 3')
#print('Deleted: ', w[-1])
parseTree.append('Rule 3 Error')
errorGenerator(7,buffer,w)
del w[-1]
else:
print('rule 1')
parseTree.append('Rule 1 Error')
#print('Deleted: ', buffer[-1])
errorGenerator(8,buffer,w)
if (buffer [-1] not in df.columns)and buffer != []:
x = 'Exiting: ' + ptStack[-1]
pt.append(x)
del ptStack[-1]
del buffer[-1]
#continue
elif (pd.isnull(df.loc[buffer[-1],w[-1][0]])):
#Panic Mode error recovery - delete input symbol upon null matching entries in the parsing table
#print(df.loc[buffer[-1],w[-1]] == 'sync')
#print('Error: Expected \'' + buffer[-1] + '\' - Recieved \'' + w[-1] + '\'.' )
#print('\nBuffer,Input: ' ,buffer[-1],w[-1[0]])
print('rule 2')
parseTree.append('Rule 2 Error')
c = 'Buffer,Input: ' + buffer[-1],w[-1][0]
parseTree.append(c)
errorGenerator(7,buffer,w)
# print('Deleted: ', w[-1])
del w[-1]
#w = []
else:
print('BREAK')
#print('Buffer,Input: ' ,buffer,w)
#for i in parseTree:
# print(i)
return match,parseTree, pt
def errorGenerator(x,buffer,w):
global errorList
if x == 7:
rr = ('Line: '+ w[-1][-1] + ' - Error Code: 7' + ' - Error: Syntax Error. Expected " '
+ buffer[-1] + ' "'+ ', Got " '+ w[-1][0] +' "' +
' - Status: Resolved - Action: Input token deleted')
else:
rr = ('Line: '+ w[-1][-1] + ' - Error Code: 8' + ' - Error: Syntax Error. Expected " '
+ buffer[-1] + ' "'+ ', Got " '+ w[-1][0] +' "' +
' - Status: Resolved - Action: Expected token popped')
errorList.append(rr)
def saveFiles():
global ptStack
fname = filename.split('.', 1)[0]
with open(fname + '.err', "a") as myfile:
for i in errorList:
myfile.write(i + '\n')
ptStack = ptStack[::-1]
for i in ptStack:
a = 'Exiting: ' + i
pt.append(a)
f = open(fname + '.tr',"w+")
for i in pt:
f.write(i + '\n')
f.close()
filename = 'test11.pj'
tokenList,errorList, symbolTable = la.lexMain(filename)
print('ErrorList: ', errorList)
start()
saveFiles()
#Wla.saveFiles(tokenList,errorList, symbolTable)