Skip to content

Commit 660dd65

Browse files
Add files via upload
1 parent a359511 commit 660dd65

17 files changed

+834
-0
lines changed

LL1parsing.py

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
from pprint import pprint
2+
def add(listA,x):
3+
if x not in listA: listA.append(x)
4+
def first_set(G,T,Nt):
5+
firstA = {}
6+
tempA = {}
7+
for NonT in Nt: firstA[NonT] = []
8+
while True:
9+
for NonT in firstA:
10+
tempA[NonT] = [i for i in firstA[NonT]]
11+
for i in range(len(G)):
12+
if G[i][1] == '': add(tempA[G[i][0]],'')
13+
elif G[i][1][0] in Nt:
14+
if '' not in firstA[G[i][1][0]]:
15+
for k in firstA[G[i][1][0]]: add(tempA[G[i][0]],k)
16+
else:
17+
listA = [ k for k in firstA[G[i][1][0]] ]
18+
r = listA.index('')
19+
del listA[r]
20+
for k in listA: add(tempA[G[i][0]],k)
21+
add(tempA[G[i][0]],'')
22+
elif G[i][1][0] in T: add(tempA[G[i][0]],G[i][1][0])
23+
flag = True
24+
for NonT in Nt: flag = flag and (tempA[NonT] == firstA[NonT])
25+
if flag: break
26+
for NonT in Nt: firstA[NonT] = [i for i in tempA[NonT]]
27+
return firstA
28+
def follow_set(G,S,T,Nt):
29+
followA = {}
30+
tempA = {}
31+
for NonT in Nt: followA[NonT] = ['$'] if NonT == S else []
32+
first_set_list = first_set(G,T,Nt)
33+
while True:
34+
for NonT in followA:
35+
tempA[NonT] = [i for i in followA[NonT]]
36+
for production in G:
37+
Aj,rhs = production[0],list(production[1])
38+
for i in range(len(rhs)):
39+
Ai = rhs[i]
40+
if Ai in Nt:
41+
if i+1 == len(rhs): w = ''
42+
else: w = rhs[i+1]
43+
if w == '' or (w in Nt and '' in first_set_list[w]):
44+
for k in followA[Aj]: add(tempA[Ai],k)
45+
if w in T: add(tempA[Ai],w)
46+
if w in Nt:
47+
for k in first_set_list[w]: add(tempA[Ai],k) if k!='' else add(tempA[Ai],'$')
48+
flag = True
49+
for NonT in Nt: flag = flag and (tempA[NonT] == followA[NonT])
50+
if flag: break
51+
for NonT in Nt: followA[NonT] = [i for i in tempA[NonT]]
52+
return followA
53+
def import_grammar(fileHandle):
54+
G,T,Nt = [],[],[]
55+
for lines in fileHandle:
56+
production = lines.split(' -> ')
57+
if production[0] not in Nt: Nt.append(production[0])
58+
listStr = list(production[1])
59+
del listStr[-1]
60+
production[1] = ''.join(i for i in listStr)
61+
for char in production[1]:
62+
if 65<=ord(char) and ord(char)<= 90:
63+
if char not in Nt: Nt.append(char)
64+
else:
65+
if char not in T: T.append(char)
66+
if production not in G: G.append((production[0],production[1]))
67+
T.append('$')
68+
return G,T,Nt
69+
def parse_table(G,terminal,first,follow):
70+
M = {}
71+
grammar = {}
72+
for i in range(len(G)):
73+
if G[i][0] not in grammar: grammar[G[i][0]] = []
74+
add(grammar[G[i][0]],G[i][1])
75+
print 'Grammar for Parse Table'
76+
pprint(grammar)
77+
for A in grammar:
78+
if A not in M: M[A] = {}
79+
for alpha in grammar[A]:
80+
print A,'->',alpha
81+
if alpha == '':
82+
for b in follow[A]:
83+
if b not in M[A]: M[A][b] = {}
84+
if A not in M[A][b]: M[A][b][A] = []
85+
M[A][b][A].append(alpha)
86+
elif alpha[0] in terminal:
87+
if alpha[0] not in M[A]: M[A][alpha[0]] = {}
88+
if A not in M[A][alpha[0]]: M[A][alpha[0]][A] = []
89+
M[A][alpha[0]][A].append(alpha)
90+
else:
91+
for a in first[alpha[0]]:
92+
if a not in M[A] and a != '': M[A][a] = {}
93+
if A not in M[A][a]: M[A][a][A] = []
94+
if a!='': M[A][a][A].append(alpha)
95+
return M
96+
stack,post = [],''
97+
def predictive_parsing(M,T):
98+
global stack,post
99+
ip,X,top = 0,stack[0],0
100+
while X != '$':
101+
a = post[ip]
102+
print a,X,stack
103+
if X==a:
104+
del stack[top]
105+
ip = ip + 1
106+
elif X in T and X!='$':
107+
print 'terminal not encountered',X
108+
print 'Syntax error'
109+
return
110+
elif a not in M[X]:
111+
print 'terminal not in parse table',a,X
112+
print 'Syntax error'
113+
return
114+
elif len(M[X][a][X]) == 1:
115+
Y = list(M[X][a][X][0])
116+
del stack[top]
117+
for i in range(len(Y)): stack.insert(top,Y[len(Y)-1-i])
118+
elif len(M[X][a][X]) > 1:
119+
print 'Grammar not in LL1'
120+
break
121+
X = stack[top]
122+
print 'Accepted'
123+
stack,post = [],''
124+
def driver():
125+
global stack,post
126+
fileHandle = open('rev.txt')
127+
G,T,Nt = import_grammar(fileHandle)
128+
pprint(G)
129+
print T,Nt
130+
first_set_list = first_set(G,T,Nt)
131+
follow_set_list = follow_set(G,G[0][0],T,Nt)
132+
print 'First'
133+
print first_set_list
134+
print 'Follow'
135+
print follow_set_list
136+
parseTable = parse_table(G,T,first_set_list,follow_set_list)
137+
pprint(parseTable)
138+
post = raw_input('Enter some string to test ....')+'$'
139+
stack = [G[0][0],'$']
140+
predictive_parsing(parseTable,T)
141+
driver()

LR1parsing3.py

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
from pprint import pprint
2+
from sys import exit
3+
def add(listA,x):
4+
if x not in listA: listA.append(x)
5+
def import_grammar(fileHandle):
6+
G,T,Nt = [],[],[]
7+
for lines in fileHandle:
8+
production = lines.split(' -> ')
9+
if production[0] not in Nt: Nt.append(production[0])
10+
listStr = list(production[1])
11+
del listStr[-1]
12+
production[1] = ''.join(i for i in listStr)
13+
for char in production[1]:
14+
if 65<=ord(char) and ord(char)<= 90:
15+
if char not in Nt: Nt.append(char)
16+
else:
17+
if char not in T: T.append(char)
18+
if production not in G: G.append((production[0],production[1]))
19+
T.append('$')
20+
return G,T,Nt
21+
def first_set(G,T,Nt):
22+
firstA = {}
23+
tempA = {}
24+
for NonT in Nt: firstA[NonT] = []
25+
while True:
26+
for NonT in firstA:
27+
tempA[NonT] = [i for i in firstA[NonT]]
28+
for i in range(len(G)):
29+
if G[i][1] == '': add(tempA[G[i][0]],'')
30+
elif G[i][1][0] in Nt:
31+
if '' not in firstA[G[i][1][0]]:
32+
for k in firstA[G[i][1][0]]: add(tempA[G[i][0]],k)
33+
else:
34+
listA = [ k for k in firstA[G[i][1][0]] ]
35+
r = listA.index('')
36+
del listA[r]
37+
for k in listA: add(tempA[G[i][0]],k)
38+
add(tempA[G[i][0]],'')
39+
elif G[i][1][0] in T: add(tempA[G[i][0]],G[i][1][0])
40+
flag = True
41+
for NonT in Nt: flag = flag and (tempA[NonT] == firstA[NonT])
42+
if flag: break
43+
for NonT in Nt: firstA[NonT] = [i for i in tempA[NonT]]
44+
return firstA
45+
def closure(I,G,T,Nt):
46+
J = [p for p in I]
47+
first_set_list = first_set(G,T,Nt)
48+
while True:
49+
J1 = [x for x in J]
50+
for x in J1:
51+
handle = list(x[1])
52+
a = x[2]
53+
k = handle.index('.')
54+
if k+1!=len(handle):
55+
if handle[k+1] in Nt:
56+
for p in G:
57+
beta = ''.join(handle[m] for m in range(k+2,len(handle)))
58+
b1 = list(beta+a)[0]
59+
if p[0] == handle[k+1]:
60+
if b1 in T:
61+
new_p = (p[0],'.'+p[1],b1)
62+
if new_p not in J1: J1.append(new_p)
63+
elif b1 in Nt:
64+
for b in first_set_list[b1]:
65+
new_p = (p[0],'.'+p[1],b if b!='' else '$')
66+
if new_p not in J1: J1.append(new_p)
67+
flag = True
68+
for x in J1:
69+
if x not in J:
70+
flag = False
71+
J.append(x)
72+
if flag: break
73+
return J
74+
def goto(I,G,X,T,Nt):
75+
W = []
76+
for x in I:
77+
handle = list(x[1])
78+
k = handle.index('.')
79+
if k != len(handle)-1:
80+
if handle[k+1] == X:
81+
S1 = ''.join([handle[i] for i in range(k)])
82+
S2 = ''.join([handle[i] for i in range(k+2,len(handle))])
83+
W.append((x[0],S1+X+'.'+S2,x[2]))
84+
return closure(W,G,T,Nt)
85+
def items(G,T,Nt):
86+
C = [ closure([(G[0][0],'.'+G[0][1],'$')],G,T,Nt) ]
87+
action = {}
88+
goto_k = {}
89+
reduction_states = {}
90+
while True:
91+
C1 = [ I for I in C ]
92+
for I in C1:
93+
for X in T:
94+
goto_list = goto(I,G,X,T,Nt)
95+
if len(goto_list)!=0 and goto_list not in C1:
96+
C1.append(goto_list)
97+
if C1.index(I) not in action: action[C1.index(I)] = {}
98+
if X not in action[C1.index(I)]:
99+
action[C1.index(I)][X] = C1.index(goto_list)
100+
elif goto_list in C1:
101+
if C1.index(I) not in action: action[C1.index(I)] = {}
102+
if X not in action[C1.index(I)]:
103+
action[C1.index(I)][X] = C1.index(goto_list)
104+
for I in C1:
105+
for X in Nt:
106+
goto_list = goto(I,G,X,T,Nt)
107+
if len(goto_list)!=0 and goto_list not in C1:
108+
C1.append(goto_list)
109+
if C1.index(I) not in goto_k: goto_k[C1.index(I)] = {}
110+
if X not in goto_k[C1.index(I)]:
111+
goto_k[C1.index(I)][X] = C1.index(goto_list)
112+
elif goto_list in C1:
113+
if C1.index(I) not in goto_k: goto_k[C1.index(I)] = {}
114+
if X not in goto_k[C1.index(I)]:
115+
goto_k[C1.index(I)][X] = C1.index(goto_list)
116+
flag = True
117+
for x in C1:
118+
if x not in C:
119+
flag = False
120+
C.append(x)
121+
if flag: break
122+
for state in range(len(C)):
123+
reduction_states[state] = {}
124+
for production in C[state]:
125+
if production[1][len(production[1])-1] == '.':
126+
rhs = list(production[1])
127+
del rhs[-1]
128+
rhsStr = ''.join(i for i in rhs)
129+
Pp = (production[0],rhsStr)
130+
reduction_states[state][production[2]] = Pp
131+
accept_state = 0
132+
for x in reduction_states:
133+
if '$' in reduction_states[x]:
134+
if reduction_states[x]['$'] == G[0]:
135+
accept_state = x
136+
break
137+
return action,goto_k,reduction_states,accept_state
138+
def driver():
139+
fileHandle = open('grammar14.txt')
140+
G,T,Nt = import_grammar(fileHandle)
141+
print T,Nt
142+
action_list,goto_list,reduction_states,accept_state = items(G,T,Nt)
143+
print 'Action list'
144+
pprint(action_list)
145+
print 'Goto list'
146+
pprint(goto_list)
147+
print 'Reduction states'
148+
pprint(reduction_states)
149+
print 'Accept state',accept_state
150+
stack = [0]
151+
input_str = raw_input('Enter some string ')+'$'
152+
i,top = 0,0
153+
# LR(1) AUTOMATON PARSING
154+
while True:
155+
s = stack[top]
156+
try:
157+
print s,stack,input_str[i] if i != len(input_str) else 'Finish',
158+
if s == accept_state and input_str[i]=='$':
159+
print 'accept'
160+
break
161+
elif len(reduction_states[s]) != 0 and input_str[i] in reduction_states[s]:
162+
A,beta = reduction_states[s][input_str[i]]
163+
print 'reduce',A,'->',beta
164+
for j in range(len(beta)): del stack[top]
165+
t = stack[top]
166+
stack.insert(top,goto_list[t][A])
167+
else:
168+
a = input_str[i]
169+
stack.insert(top,action_list[s][a])
170+
print 'shift',action_list[s][a]
171+
i = i + 1
172+
except:
173+
print 'Syntax error'
174+
break
175+
driver()

0 commit comments

Comments
 (0)