-
Notifications
You must be signed in to change notification settings - Fork 1
/
clr.py
373 lines (273 loc) · 10.4 KB
/
clr.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
from collections import deque
from collections import OrderedDict
from functions import *
from tkinter import *
import tkinter as tk
from tkinter import ttk
from firstfollow import *
import firstfollow
from firstfollow import production_list, nt_list as ntl, t_list as tl
nt_list, t_list=[], []
Data = []
class State:
_id=0
def __init__(self, closure):
self.closure=closure
self.no=State._id
State._id+=1
class Item(str):
def __new__(cls, item, lookahead=list()):
self=str.__new__(cls, item)
self.lookahead=lookahead
return self
def __str__(self):
return super(Item, self).__str__()+", "+'|'.join(self.lookahead)
def closure(items):
def exists(newitem, items):
for i in items:
if i==newitem and sorted(set(i.lookahead))==sorted(set(newitem.lookahead)):
return True
return False
global production_list
while True:
flag=0
for i in items:
if i.index('.')==len(i)-1: continue
Y=i.split('->')[1].split('.')[1][0]
if i.index('.')+1<len(i)-1:
lastr=list(firstfollow.compute_first(i[i.index('.')+2])-set(chr(1013)))
else:
lastr=i.lookahead
for prod in production_list:
head, body=prod.split('->')
if head!=Y: continue
newitem=Item(Y+'->.'+body, lastr)
if not exists(newitem, items):
items.append(newitem)
flag=1
if flag==0: break
return items
def goto(items, symbol):
global production_list
initial=[]
for i in items:
if i.index('.')==len(i)-1: continue
head, body=i.split('->')
seen, unseen=body.split('.')
if unseen[0]==symbol and len(unseen) >= 1:
initial.append(Item(head+'->'+seen+unseen[0]+'.'+unseen[1:], i.lookahead))
return closure(initial)
def calc_states():
def contains(states, t):
for s in states:
if len(s) != len(t): continue
if sorted(s)==sorted(t):
for i in range(len(s)):
if s[i].lookahead!=t[i].lookahead: break
else: return True
return False
global production_list, nt_list, t_list
head, body=production_list[0].split('->')
states=[closure([Item(head+'->.'+body, ['$'])])]
while True:
flag=0
for s in states:
for e in nt_list+t_list:
t=goto(s, e)
if t == [] or contains(states, t): continue
states.append(t)
flag=1
if not flag: break
return states
def make_table(states):
global nt_list, t_list
def getstateno(t):
for s in states:
if len(s.closure) != len(t): continue
if sorted(s.closure)==sorted(t):
for i in range(len(s.closure)):
if s.closure[i].lookahead!=t[i].lookahead: break
else: return s.no
return -1
def getprodno(closure):
closure=''.join(closure).replace('.', '')
return production_list.index(closure)
SLR_Table=OrderedDict()
for i in range(len(states)):
states[i]=State(states[i])
for s in states:
SLR_Table[s.no]=OrderedDict()
for item in s.closure:
head, body=item.split('->')
if body=='.':
for term in item.lookahead:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getprodno(item))}
else: SLR_Table[s.no][term] |= {'r'+str(getprodno(item))}
continue
nextsym=body.split('.')[1]
if nextsym=='':
if getprodno(item)==0:
SLR_Table[s.no]['$']='accept'
else:
for term in item.lookahead:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getprodno(item))}
else: SLR_Table[s.no][term] |= {'r'+str(getprodno(item))}
continue
nextsym=nextsym[0]
t=goto(s.closure, nextsym)
if t != []:
if nextsym in t_list:
if nextsym not in SLR_Table[s.no].keys():
SLR_Table[s.no][nextsym]={'s'+str(getstateno(t))}
else: SLR_Table[s.no][nextsym] |= {'s'+str(getstateno(t))}
else: SLR_Table[s.no][nextsym] = str(getstateno(t))
return SLR_Table
def augment_grammar():
for i in range(ord('Z'), ord('A')-1, -1):
if chr(i) not in nt_list:
start_prod=production_list[0]
production_list.insert(0, chr(i)+'->'+start_prod.split('->')[0])
return
def main(MainInput):
global production_list, ntl, nt_list, tl, t_list
firstfollow.main(MainInput)
print(production_list)
Data.append("FIRST AND FOLLOW OF NON-TERMINALS")
Data.append(" ")
for nt in ntl:
firstfollow.compute_first(nt)
firstfollow.compute_follow(nt)
Data.append("{} :".format(nt))
Data.append("FIRST : "+str(firstfollow.get_first(nt)))
Data.append("FOLLOW : "+str(firstfollow.get_follow(nt)))
Data.append(" ")
augment_grammar()
nt_list=list(ntl.keys())
t_list=list(tl.keys()) + ['$']
#Data.append(nt_list)
#Data.append(t_list)
j=calc_states()
ctr=0
Data.append("ITEM SETS :")
for s in j:
Data.append(" ")
Data.append("Item{}:".format(ctr))
for i in s:
Data.append(" "+str(i))
ctr+=1
table=make_table(j)
Data.append(' ')
Data.append("CLR(1) TABLE :")
sym_list = nt_list + t_list
sr, rr=0, 0
Data.append('_____________________________________________________________________')
Data.append(' | '+' | '.join(sym_list)+' |')
Data.append('_____________________________________________________________________')
for i, j in table.items():
Data.append(str(i)+' | '+' | '.join(list(j.get(sym,' ') if type(j.get(sym))in (str , None) else next(iter(j.get(sym,' '))) for sym in sym_list))+' |')
s, r=0, 0
for p in j.values():
if p!='accept' and len(p)>1:
p=list(p)
if('r' in p[0]): r+=1
else: s+=1
if('r' in p[1]): r+=1
else: s+=1
if r>0 and s>0: sr+=1
elif r>0: rr+=1
Data.append('_____________________________________________________________________')
Data.append(str(sr)+ " s/r conflicts | "+str(rr)+ " r/r conflicts")
Data.append(' ')
return table
# CLR MAIN
root = tk.Tk()
root.geometry("600x500")
root.title("CLR Parser")
root.config(bg='#D9D8D7')
paddings = {'padx': 5, 'pady': 5}
style = ttk.Style()
style.configure("TButton", font=("Calibri", 15, "bold"), borderwidth=4)
style.configure("TLabel", font=("Calibri", 15,"bold"), borderwidth=4)
style.configure("TEntry", font=("Calibri", 15,"bold"), borderwidth=4)
style.map("TButton", foreground=[("active", "disabled", "green")],background=[("active", "black")])
#style.theme_use("classic")
frame = tk.Frame(root)
frame.configure(background='#D9D8D7')
frame.pack()
MainInput = []
Input = tk.StringVar()
ttk.Label(frame, text ="Enter the Productions :",background='#D9D8D7').pack(padx=10,pady=10,anchor=W)
InputEntry = ttk.Entry(frame,width=50,textvariable=Input)
InputEntry.pack(ipadx=10,ipady=10,padx=10,anchor=W)
table = None
def display():
scrollbar1 = Scrollbar(root, bg="green")
scrollbar1.pack( side = RIGHT, fill = Y )
scrollbar2 = Scrollbar(root, bg="green")
scrollbar2.pack( side = RIGHT, fill = Y )
mylist = Listbox(root,yscrollcommand=scrollbar1.set,xscrollcommand=scrollbar2.set,background='#D9D8D7',font=("Calibri", 15,"bold"),height=100,width=0,highlightthickness=5)
for i in Data:
mylist.insert(END,' '+i)
#ttk.Label(frame, text=i,background='#D9D8D7').pack(anchor=SW)
mylist.pack(anchor=CENTER, fill = BOTH,padx=60,pady=10,ipadx=20,ipady=20 )
scrollbar1.config(command=mylist.yview)
scrollbar2.config(command=mylist.xview)
def add_input():
MainInput.append(Input.get())
#ttk.Label(frame, text =Input.get()).pack()
InputEntry.delete(0,END)
print(Input.get(),MainInput)
def submit_input():
print("entered submit")
MainInput.append(Input.get())
table = main(MainInput)
InputEntry.delete(0,END)
display()
#ttk.Label(frame, text ="\n",background='#D9D8D7').pack(padx=10,pady=10,anchor=W)
#for i in Data:
# ttk.Label(frame, text=i,background='#D9D8D7').pack(side=BOTTOM)
#rc+=1
ttk.Button(frame, text = "Add Production", command = add_input).pack(side=LEFT,padx=10,pady=10)
ttk.Button(frame, text = "Submit Productions", command = submit_input).pack(side=LEFT,padx=10,pady=10)
ttk.Button(frame, text = "Exit", command = root.destroy).pack(side=LEFT,padx=10,pady=10)
ttk.Label(frame, text='',background='#D9D8D7').pack(anchor=S,fill=X)
root.mainloop()
'''
Data.append("Enter the string to be parsed")
Input=input()+'$'
try:
stack=['0']
a=list(table.items())
Data.append("productions : "+str(production_list))
Data.append('stack'+" "+'Input')
Data.append(str(*stack)+" "+str(*Input))
while(len(Input)!=0):
b=list(a[int(stack[-1])][1][Input[0]])
if(b[0][0]=="s" ):
#s=Input[0]+b[0][1:]
stack.append(Input[0])
stack.append(b[0][1:])
Input=Input[1:]
Data.append(str(*stack)+" "+str(*Input))
elif(b[0][0]=="r" ):
s=int(b[0][1:])
#print(len(production_list),s)
l=len(production_list[s])-3
#print(l)
prod=production_list[s]
l*=2
l=len(stack)-l
stack=stack[:l]
s=a[int(stack[-1])][1][prod[0]]
#print(s,b)
stack+=list(prod[0])
stack.append(s)
Data.append(str(*stack)+" "+str(*Input))
elif(b[0][0]=="a"):
Data.append(" String Accepted!")
break
except:
Data.append(' String INCORRECT for given Grammar!')
'''