-
Notifications
You must be signed in to change notification settings - Fork 8
/
slr_backend_only.py
185 lines (122 loc) · 3.55 KB
/
slr_backend_only.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
from collections import deque
from collections import OrderedDict
from pprint import pprint
import firstfollow
from firstfollow import production_list, nt_list as ntl, t_list as tl
nt_list, t_list=[], []
class State:
_id=0
def __init__(self, closure):
self.closure=closure
self.no=State._id
State._id+=1
def closure(items):
global production_list
while True:
flag=0
for i in items:
if i.index('.')==len(i)-1: continue
Y=i.split('->')[1].split('.')[1][0]
for prod in production_list:
head, body=prod.split('->')
if head!=Y: continue
newitem=Y+'->.'+body
if newitem not in items:
items.append(newitem)
flag=1
if flag==0: break
return items
def goto(items, symbol):
global production_list
initial=[]
for i in items:
if i.index('.')==len(i)-1: continue
head, body=i.split('->')
seen, unseen=body.split('.')
if unseen[0]==symbol and len(unseen) >= 1:
initial.append(head+'->'+seen+unseen[0]+'.'+unseen[1:])
return closure(initial)
def calc_states():
def contains(states, t):
for s in states:
if sorted(s)==sorted(t): return True
return False
global production_list, nt_list, t_list
head, body=production_list[0].split('->')
states=[closure([head+'->.'+body])]
while True:
flag=0
for s in states:
for e in nt_list+t_list:
t=goto(s, e)
if t == [] or contains(states, t): continue
states.append(t)
flag=1
if not flag: break
return states
def make_table(states):
global nt_list, t_list
def getstateno(closure):
for s in states:
if sorted(s.closure)==sorted(closure): return s.no
def getprodno(closure):
closure=''.join(closure).replace('.', '')
return production_list.index(closure)
SLR_Table=OrderedDict()
for i in range(len(states)):
states[i]=State(states[i])
for s in states:
SLR_Table[s.no]=OrderedDict()
for item in s.closure:
head, body=item.split('->')
if body=='.':
for term in t_list:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getprodno(item))}
else: SLR_Table[s.no][term] |= {'r'+str(getprodno(item))}
continue
nextsym=body.split('.')[1]
if nextsym=='':
if getprodno(item)==0:
SLR_Table[s.no]['$']='accept'
else:
for term in ntl[head].follow: #for term in t_list:
if term not in SLR_Table[s.no].keys():
SLR_Table[s.no][term]={'r'+str(getprodno(item))}
else: SLR_Table[s.no][term] |= {'r'+str(getprodno(item))}
continue
nextsym=nextsym[0]
t=goto(s.closure, nextsym)
if t != []:
if nextsym in t_list:
if nextsym not in SLR_Table[s.no].keys():
SLR_Table[s.no][nextsym]={'s'+str(getstateno(t))}
else: SLR_Table[s.no][nextsym] |= {'s'+str(getstateno(t))}
else: SLR_Table[s.no][nextsym] = str(getstateno(t))
return SLR_Table
def augment_grammar():
for i in range(ord('Z'), ord('A')-1, -1):
if chr(i) not in nt_list:
start_prod=production_list[0]
production_list.insert(0, chr(i)+'->'+start_prod.split('->')[0])
return
def main():
global production_list, ntl, nt_list, tl, t_list
firstfollow.main()
print("\tFIRST AND FOLLOW OF NON-TERMINALS")
for nt in ntl:
firstfollow.compute_first(nt)
firstfollow.compute_follow(nt)
print(nt)
print("\tFirst:\t", firstfollow.get_first(nt))
print("\tFollow:\t", firstfollow.get_follow(nt), "\n")
augment_grammar()
nt_list=list(ntl.keys())
t_list=list(tl.keys()) + ['$']
table=make_table(calc_states())
print("\tSLR(1) TABLE\n")
for i, j in table.items():
print(i, "\t", j)
return
if __name__=="__main__":
main()