-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathex7.py
207 lines (186 loc) · 3.6 KB
/
ex7.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
# #example for direct left recursion
# gram = {"A":["Aa","Ab","c","d"]
# }
#example for indirect left recursion
gram = {
"E":["E+T","T"],
"T":["T*F","F"],
"F":["(E)","i"]
}
def removeDirectLR(gramA, A):
"""gramA is dictonary"""
temp = gramA[A]
tempCr = []
tempInCr = []
for i in temp:
if i[0] == A:
#tempInCr.append(i[1:])
tempInCr.append(i[1:]+[A+"'"])
else:
#tempCr.append(i)
tempCr.append(i+[A+"'"])
tempInCr.append(["e"])
gramA[A] = tempCr
gramA[A+"'"] = tempInCr
return gramA
def checkForIndirect(gramA, a, ai):
if ai not in gramA:
return False
if a == ai:
return True
for i in gramA[ai]:
if i[0] == ai:
return False
if i[0] in gramA:
return checkForIndirect(gramA, a, i[0])
return False
def rep(gramA, A):
temp = gramA[A]
newTemp = []
for i in temp:
if checkForIndirect(gramA, A, i[0]):
t = []
for k in gramA[i[0]]:
t=[]
t+=k
t+=i[1:]
newTemp.append(t)
else:
newTemp.append(i)
gramA[A] = newTemp
return gramA
def rem(gram):
c = 1
conv = {}
gramA = {}
revconv = {}
for j in gram:
conv[j] = "A"+str(c)
gramA["A"+str(c)] = []
c+=1
for i in gram:
for j in gram[i]:
temp = []
for k in j:
if k in conv:
temp.append(conv[k])
else:
temp.append(k)
gramA[conv[i]].append(temp)
#print(gramA)
for i in range(c-1,0,-1):
ai = "A"+str(i)
for j in range(0,i):
aj = gramA[ai][0][0]
if ai!=aj :
if aj in gramA and checkForIndirect(gramA,ai,aj):
gramA = rep(gramA, ai)
for i in range(1,c):
ai = "A"+str(i)
for j in gramA[ai]:
if ai==j[0]:
gramA = removeDirectLR(gramA, ai)
break
op = {}
for i in gramA:
a = str(i)
for j in conv:
a = a.replace(conv[j],j)
revconv[i] = a
for i in gramA:
l = []
for j in gramA[i]:
k = []
for m in j:
if m in revconv:
k.append(m.replace(m,revconv[m]))
else:
k.append(m)
l.append(k)
op[revconv[i]] = l
return op
result = rem(gram)
terminals = []
for i in result:
for j in result[i]:
for k in j:
if k not in result:
terminals+=[k]
terminals = list(set(terminals))
#print(terminals)
def first(gram, term):
a = []
if term not in gram:
return [term]
for i in gram[term]:
if i[0] not in gram:
a.append(i[0])
elif i[0] in gram:
a += first(gram, i[0])
return a
firsts = {}
for i in result:
firsts[i] = first(result,i)
# print(f'First({i}):',firsts[i])
def follow(gram, term):
a = []
for rule in gram:
for i in gram[rule]:
if term in i:
temp = i
indx = i.index(term)
if indx+1!=len(i):
if i[-1] in firsts:
a+=firsts[i[-1]]
else:
a+=[i[-1]]
else:
a+=["e"]
if rule != term and "e" in a:
a+= follow(gram,rule)
return a
follows = {}
for i in result:
follows[i] = list(set(follow(result,i)))
if "e" in follows[i]:
follows[i].pop(follows[i].index("e"))
follows[i]+=["$"]
# print(f'Follow({i}):',follows[i])
resMod = {}
for i in result:
l = []
for j in result[i]:
temp = ""
for k in j:
temp+=k
l.append(temp)
resMod[i] = l
# create predictive parsing table
tterm = list(terminals)
tterm.pop(tterm.index("e"))
tterm+=["$"]
pptable = {}
for i in result:
for j in tterm:
if j in firsts[i]:
pptable[(i,j)]=resMod[i][0]
else:
pptable[(i,j)]=""
if "e" in firsts[i]:
for j in tterm:
if j in follows[i]:
pptable[(i,j)]="e"
pptable[("F","i")] = "i"
toprint = f'{"": <10}'
for i in tterm:
toprint+= f'|{i: <10}'
print(toprint)
for i in result:
toprint = f'{i: <10}'
for j in tterm:
if pptable[(i,j)]!="":
toprint+=f'|{i+"->"+pptable[(i,j)]: <10}'
else:
toprint+=f'|{pptable[(i,j)]: <10}'
print(f'{"-":-<76}')
print(toprint)