-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws_8.py
300 lines (242 loc) · 6.93 KB
/
ws_8.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
# -*- coding: utf-8 -*-
"""ws_8.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/134p9yxecGbZwOoYgmjahaO4gL4l4yyEj
---
> **1. Write a program to remove left recursion (Both immediate and indirect) from the given grammar.**
---
"""
def remove_left_recursion(grammar):
non_terminals = list(grammar.keys())
new_grammar = {}
for A in non_terminals:
new_grammar[A] = []
alpha = []
beta = []
for production in grammar[A]:
if production[0] == A:
alpha.append(production[1:])
else:
beta.append(production)
if len(alpha) > 0:
new_non_terminal = A + "'"
new_grammar[new_non_terminal] = [a + new_non_terminal for a in alpha] + ['e']
new_grammar[A] = [b + new_non_terminal for b in beta]
else:
new_grammar[A] = grammar[A]
return new_grammar
def print_grammar(grammar):
for non_terminal, productions in grammar.items():
print(non_terminal + " -> " + " | ".join(productions))
# Example grammar
grammar = {
"S": ["S+T", "T"],
"T": ["T*F", "F"],
"F": ["a"]
}
print("Original Grammar:")
print_grammar(grammar)
new_grammar = remove_left_recursion(grammar)
print("\nGrammar after removing left recursion:")
print_grammar(new_grammar)
"""
---
> **2. Write a program to apply left factoring on the grammar.**
---
"""
from itertools import takewhile
def groupby(ls):
d = {}
ls = [ y[0] for y in rules ]
initial = list(set(ls))
for y in initial:
for i in rules:
if i.startswith(y):
if y not in d:
d[y] = []
d[y].append(i)
return d
def prefix(x):
return len(set(x)) == 1
starting=""
rules=[]
common=[]
alphabetset=["A'","B'","C'","D'","E'","F'","G'","H'","I'","J'","K'","L'","M'","N'","O'","P'","Q'","R'","S'","T'","U'","V'","W'","X'","Y'","Z'"]
s= "S->iEtS|iEtSeS|a"
print()
print("Grammar before left factoring : ")
s1= "S->iEtS | iEtSeS | a"
print(s1)
print()
print("Grammar after left factoring : ")
while(True):
rules=[]
common=[]
split=s.split("->")
starting=split[0]
for i in split[1].split("|"):
rules.append(i)
for k, l in groupby(rules).items():
r = [l[0] for l in takewhile(prefix, zip(*l))]
common.append(''.join(r))
for i in common:
newalphabet=alphabetset.pop()
print(starting+" -> "+i+newalphabet)
index=[]
for k in rules:
if(k.startswith(i)):
index.append(k)
print(newalphabet+" -> ",end="")
for j in index[:-1]:
stringtoprint=j.replace(i,"", 1)+"| "
if stringtoprint=="| ":
print("\u03B5","| ",end="")
else:
print(j.replace(i,"", 1)+"| ",end="")
stringtoprint=index[-1].replace(i,"", 1)+"| "
if stringtoprint=="| ":
print("\u03B5","",end="")
else:
print(index[-1].replace(i,"", 1)+" ",end="")
print("")
break
"""
---
> **3. Write a program to find first and follow for the non-terminals in the given grammar.**
---
"""
print("Recursive Desent Parsing For following grammar\n")
print("E->TE'\nE'->+TE'/e\nT->FT'\nT'->*FT'/e\nF->(E)/i\n")
print("Enter the string want to be checked\n")
global s
s=list(input())
global i
i=0
def match(a):
global s
global i
if(i>=len(s)):
return False
elif(s[i]==a):
i+=1
return True
else:
return False
def F():
if(match("(")):
if(E()):
if(match(")")):
return True
else:
return False
else:
return False
elif(match("i")):
print('i')
return True
else:
return False
def Tx():
if(match("*")):
print('*')
if(F()):
if(Tx()):
return True
else:
return False
else:
return False
else:
return True
def T():
if(F()):
if(Tx()):
return True
else:
return False
else:
return False
def Ex():
if(match("+")):
print('+')
if(T()):
if(Ex()):
return True
else:
return False
else:
return False
else:
return True
def E():
if(T()):
if(Ex()):
return True
else:
return False
else:
return False
if(E()):
if(i==len(s)):
print("String is accepted")
else:
print("String is not accepted")
else:
print("string is not accepted")
"""
---
> **4. Write a program to implement predictive parsing technique.**
---
"""
#Some helper functions
def print_iter(Matched,Stack,Input,Action,verbose=True):
if verbose==True:
print(".".join(Matched).ljust(30)," | ",".".join(Stack).ljust(25)," | ",".".join(Input).ljust(30)," | ",Action)
#The predictive parsing algorithm
def predictive_parsing(sentence,parsingtable,terminals,start_state="S",verbose=True): #Set verbose to false to not see the stages of the algorithm
status = None
match = []
stack = [start_state,"$"]
Inp = sentence.split(".")
if verbose==True:
print_iter(["Matched"],["Stack"],["Input"],"Action")
print_iter(match,stack,Inp,"Initial",verbose)
action=[]
while(len(sentence)>0 and status!=False):
top_of_input = Inp[0]
pos = top_of_input
if stack[0] =="$" and pos == "$" :
print_iter(match,stack,Inp,"Accepted",verbose)
return "Accepted"
if stack[0] == pos:
print_iter(match,stack,Inp,"Pop",verbose)
match.append(stack[0])
del(stack[0])
del(Inp[0])
continue
if stack[0]=="epsilon":
print_iter(match,stack,Inp,"Poping Epsilon",verbose)
del(stack[0])
continue
try:
production=parsingtable[stack[0]][pos]
print_iter(match,stack,Inp,stack[0]+" -> "+production,verbose)
except:
return "error for "+str(stack[0])+" on "+str(pos),"Not Accepted"
new = production.split(".")
stack=new+stack[1:]
return "Not Accepted"
if __name__=="__main__":
#Example for the working of the predictive parsing :-
#input for the grammar : E->TE1;E1->+TE1|epsilon;T->FT1 ...
parsingtable = {
"E" : {"id" : "T.E1", "(" : "T.E1"},
"E1" : {"+":"+.T.E1", ")":"epsilon", "$" : "epsilon"},
"T" : {"id" : "F.T1", "(" : "F.T1" },
"T1" : {"+" : "epsilon", "*" : "*.F.T1", ")" : "epsilon", "$" : "epsilon"},
"F":{"id":"id","(":"(.E.)"}
}
terminals = ["id","(",")","+","*"]
#Another Example done in class:-
print(predictive_parsing(sentence="c.c.c.c.d.d.$",parsingtable={"S" : {"c":"C.C","d":"C.C"},"C":{"c":"c.C","d":"d"}},terminals=["c,d"],start_state="S"))