-
Notifications
You must be signed in to change notification settings - Fork 2
/
ll1.py
267 lines (213 loc) · 7.81 KB
/
ll1.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
from collections import OrderedDict
def isterminal(char):
if(char.isupper() or char == "`"):
return False
else:
return True
def insert(grammar, lhs, rhs):
if(lhs in grammar and rhs not in grammar[lhs] and grammar[lhs] != "null"):
grammar[lhs].append(rhs)
elif(lhs not in grammar or grammar[lhs] == "null"):
grammar[lhs] = [rhs]
return grammar
def first(lhs, grammar, grammar_first):
rhs = grammar[lhs]
for i in rhs:
k = 0
flag = 0
current = []
confirm = 0
flog = 0
if(lhs in grammar and "`" in grammar_first[lhs]):
flog = 1
while(1):
check = []
if(k>=len(i)):
if(len(current)==0 or flag == 1 or confirm == k or flog == 1):
grammar_first = insert(grammar_first, lhs, "`")
break
if(i[k].isupper()):
if(grammar_first[i[k]] == "null"):
grammar_first = first(i[k], grammar, grammar_first)
# print("state ", lhs, "i ", i, "k, ", k, grammar_first[i[k]])
for j in grammar_first[i[k]]:
grammar_first = insert(grammar_first, lhs, j)
check.append(j)
else:
grammar_first = insert(grammar_first, lhs, i[k])
check.append(i[k])
if(i[k]=="`"):
flag = 1
current.extend(check)
if("`" not in check):
if(flog == 1):
grammar_first = insert(grammar_first, lhs, "`")
break
else:
confirm += 1
k+=1
grammar_first[lhs].remove("`")
return(grammar_first)
def rec_follow(k, next_i, grammar_follow, i, grammar, start, grammar_first, lhs):
if(len(k)==next_i):
if(grammar_follow[i] == "null"):
grammar_follow = follow(i, grammar, grammar_follow, start)
for q in grammar_follow[i]:
grammar_follow = insert(grammar_follow, lhs, q)
else:
if(k[next_i].isupper()):
for q in grammar_first[k[next_i]]:
if(q=="`"):
grammar_follow = rec_follow(k, next_i+1, grammar_follow, i, grammar, start, grammar_first, lhs)
else:
grammar_follow = insert(grammar_follow, lhs, q)
else:
grammar_follow = insert(grammar_follow, lhs, k[next_i])
return(grammar_follow)
def follow(lhs, grammar, grammar_follow, start):
for i in grammar:
j = grammar[i]
for k in j:
if(lhs in k):
next_i = k.index(lhs)+1
grammar_follow = rec_follow(k, next_i, grammar_follow, i, grammar, start, grammar_first, lhs)
if(lhs==start):
grammar_follow = insert(grammar_follow, lhs, "$")
return(grammar_follow)
def show_dict(dictionary):
for key in dictionary.keys():
print(key+" : ", end = "")
for item in dictionary[key]:
if(item == "`"):
print("Epsilon, ", end = "")
else:
print(item+", ", end = "")
print("\b\b")
def get_rule(non_terminal, terminal, grammar, grammar_first):
for rhs in grammar[non_terminal]:
#print(rhs)
for rule in rhs:
if(rule == terminal):
string = non_terminal+"~"+rhs
return string
elif(rule.isupper() and terminal in grammar_first[rule]):
string = non_terminal+"~"+rhs
return string
def generate_parse_table(terminals, non_terminals, grammar, grammar_first, grammar_follow):
parse_table = [[""]*len(terminals) for i in range(len(non_terminals))]
for non_terminal in non_terminals:
for terminal in terminals:
#print(terminal)
#print(grammar_first[non_terminal])
if terminal in grammar_first[non_terminal]:
rule = get_rule(non_terminal, terminal, grammar, grammar_first)
#print(rule)
elif("`" in grammar_first[non_terminal] and terminal in grammar_follow[non_terminal]):
rule = non_terminal+"~`"
elif(terminal in grammar_follow[non_terminal]):
rule = "Sync"
else:
rule = ""
parse_table[non_terminals.index(non_terminal)][terminals.index(terminal)] = rule
return(parse_table)
def display_parse_table(parse_table, terminal, non_terminal):
print("\t\t\t\t",end = "")
for terminal in terminals:
print(terminal+"\t\t", end = "")
print("\n\n")
for non_terminal in non_terminals:
print("\t\t"+non_terminal+"\t\t", end = "")
for terminal in terminals:
print(parse_table[non_terminals.index(non_terminal)][terminals.index(terminal)]+"\t\t", end = "")
print("\n")
def parse(expr, parse_table, terminals, non_terminals):
stack = ["$"]
stack.insert(0, non_terminals[0])
print("\t\t\tMatched\t\t\tStack\t\t\tInput\t\t\tAction\n")
print("\t\t\t-\t\t\t", end = "")
for i in stack:
print(i, end = "")
print("\t\t\t", end = "")
print(expr+"\t\t\t", end = "")
print("-")
matched = "-"
while(True):
action = "-"
if(stack[0] == expr[0] and stack[0] == "$"):
break
elif(stack[0] == expr[0]):
if(matched == "-"):
matched = expr[0]
else:
matched = matched + expr[0]
action = "Matched "+expr[0]
expr = expr[1:]
stack.pop(0)
else:
action = parse_table[non_terminals.index(stack[0])][terminals.index(expr[0])]
stack.pop(0)
i = 0
for item in action[2:]:
if(item != "`"):
stack.insert(i,item)
i+=1
print("\t\t\t"+matched+"\t\t\t", end = "")
for i in stack:
print(i, end = "")
print("\t\t\t", end = "")
print(expr+"\t\t\t", end = "")
print(action)
################################# Main_Driver #################################
grammar = OrderedDict()
grammar_first = OrderedDict()
grammar_follow = OrderedDict()
f = open('grammar.txt')
for i in f:
i = i.replace("\n", "")
lhs = ""
rhs = ""
flag = 1
for j in i:
if(j=="~"):
flag = (flag+1)%2
continue
if(flag==1):
lhs += j
else:
rhs += j
grammar = insert(grammar, lhs, rhs)
grammar_first[lhs] = "null"
grammar_follow[lhs] = "null"
print("Grammar\n")
show_dict(grammar)
for lhs in grammar:
if(grammar_first[lhs] == "null"):
grammar_first = first(lhs, grammar, grammar_first)
print("\n\n\n")
print("First\n")
show_dict(grammar_first)
start = list(grammar.keys())[0]
for lhs in grammar:
if(grammar_follow[lhs] == "null"):
grammar_follow = follow(lhs, grammar, grammar_follow, start)
print("\n\n\n")
print("Follow\n")
show_dict(grammar_follow)
non_terminals = list(grammar.keys())
terminals = []
for i in grammar:
for rule in grammar[i]:
for char in rule:
if(isterminal(char) and char not in terminals):
terminals.append(char)
terminals.append("$")
#print(non_terminals)
#print(terminals)
print("\n\n\n\n\t\t\t\t\t\t\tParse Table\n\n")
parse_table = generate_parse_table(terminals, non_terminals, grammar, grammar_first, grammar_follow)
display_parse_table(parse_table, terminals, non_terminals)
#expr = input("Enter the expression ending with $ : ")
expr = "i+i*i$"
print("\n\n\n\n\n\n")
print("\t\t\t\t\t\t\tParsing Expression\n\n")
parse(expr, parse_table, terminals, non_terminals)