-
Notifications
You must be signed in to change notification settings - Fork 1
/
LL1_checker.py
195 lines (163 loc) · 5.77 KB
/
LL1_checker.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
"""
Syed Alfran Ali
2015KUCP1032
Python 3.6.4 is used
'~' is epsilion
"""
from collections import OrderedDict
grammar_rules = []
firsts = []
# Dictionary to store all the rules
dictionary_all = OrderedDict()
# Dictionary to store all the firsts
dictionary_first = OrderedDict()
# Dictionary that stores all follows
dictionary_follow = OrderedDict()
def nullChecker(rule):
flag = True
for i in range(3,len(rule)):
if rule[i].isupper():
if '~' not in dictionary_first[rule[i]]:
flag = False
break
else:
flag = False
break
if flag is True:
dictionary_first[rule[0]].append("~")
def first_finder(rule):
for i in range(3,len(rule)-1):
if "~" in dictionary_first[rule[i]]:
if rule[i+1].isupper():
for item in dictionary_first[rule[i+1]]:
if item != "~":
dictionary_first[rule[0]].append(item)
elif rule[i+1].islower():
dictionary_first[rule[0]].extend(rule[i+1])
break
else:
break
def finder(rule,var1):
for i in range(3,len(rule)-1):
if "~" in dictionary_first[rule[i]]:
if rule[i+1].isupper():
for item in dictionary_first[rule[i+1]]:
if item != "~":
var1.append(item)
elif rule[i+1].islower():
var1.extend(rule[i+1])
break
else:
break
return var1
def follow_finder(rule,ind,nonTerminal):
if ind+3 == len(rule)-1:
dictionary_follow[nonTerminal].extend(dictionary_follow[rule[0]])
for i in range(ind+3,len(rule)-1):
if "~" in dictionary_first[rule[i]]:
if rule[i+1].isupper():
for item in dictionary_first[rule[i+1]]:
if item != "~":
dictionary_follow[nonTerminal].append(item)
if i+1 == len(rule)-1 and "~" in dictionary_first[rule[i+1]]:
dictionary_follow[nonTerminal].extend(dictionary_follow[rule[0]])
elif rule[i+1].islower():
dictionary_follow[nonTerminal].extend(rule[i+1])
break
else:
break
def add_nonTerminal(firsts, grammar_rules):
for rule in grammar_rules:
if rule[0][0] not in firsts:
firsts.append(rule[0][0])
dictionary_first[rule[0][0]] = []
dictionary_follow[rule[0][0]] = []
def findOccurences(s,ch):
return [i for i, letter in enumerate(s) if letter == ch]
with open("input.txt", "r") as fp:
for line in fp:
grammar_rules.append(line.strip().split('\n'))
# To find FIRSTS
number_of_rules = len(grammar_rules)
rule_count = first_count = 0
add_nonTerminal(firsts, grammar_rules)
for i in range(20):
for rule in grammar_rules:
if rule[0][3].islower() or rule[0][3] is "~":
dictionary_first[rule[0][0]].extend(rule[0][3])
for rule in grammar_rules:
if rule[0][3].isupper():
for item in dictionary_first[rule[0][3]]:
if item != "~":
dictionary_first[rule[0][0]].append(item)
first_finder(rule[0])
nullChecker(rule[0])
FIRST = {}
for key in dictionary_first:
FIRST[key] = list(OrderedDict.fromkeys(dictionary_first[key]))
listForRules = []
for rule in grammar_rules:
listForRules.append(rule[0])
dictionary_follow[listForRules[0][0]].append("$")
for nonTerminal in firsts:
for rule in listForRules:
ret = findOccurences(rule[3:],nonTerminal)
if len(ret) > 0:
for ind in ret:
if 3+ind+1<len(rule):
if rule[3+ind+1].islower():
dictionary_follow[nonTerminal].extend(rule[3+ind+1])
else:
for item in dictionary_first[rule[3+ind+1]]:
if item != "~":
dictionary_follow[nonTerminal].append(item)
follow_finder(rule,ind+1,nonTerminal)
else:
dictionary_follow[nonTerminal].extend(dictionary_follow[rule[0]])
FOLLOW = {}
for key in dictionary_follow:
FOLLOW[key] = list(OrderedDict.fromkeys(dictionary_follow[key]))
# LL(1) check using predictive parsing table
listTerminal = []
listNonTerminal = []
for rule in listForRules:
listNonTerminal.append(rule[0])
for i in range(3,len(rule)):
if rule[i] not in firsts:
listTerminal.append(rule[i])
listTerminal = list(set(listTerminal))
listTerminal.append("$")
listNonTerminal = list(set(listNonTerminal))
parseTable = OrderedDict()
for nonTerminal in listNonTerminal:
for val in listTerminal:
if val != "~":
parseTable[(nonTerminal,val)] = []
for rule in listForRules:
var1 = []
if rule[3].islower() or rule[3] is "~":
var1.append(rule[3])
if rule[3].isupper():
for item in dictionary_first[rule[3]]:
if item != "~":
var1.append(item)
ret = finder(rule,var1)
var1.extend(ret)
for first in var1:
temp = str(rule)
if first != "~":
parseTable[(rule[0],first)].append(temp)
else:
for val in FOLLOW[rule[0]]:
parseTable[(rule[0],val)].append(temp)
TABLE = {}
for key in parseTable:
TABLE[key] = list(OrderedDict.fromkeys(parseTable[key]))
flag = 0
for key in TABLE:
if len(TABLE[key]) > 1:
flag = 1
if flag == 1:
print("Grammar is not LL(1)")
else:
print("Grammar is LL(1)")