-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslr_parser.py
327 lines (231 loc) · 11.2 KB
/
slr_parser.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#!/usr/bin/env python3
from graphviz import Digraph
from grammer import Grammar
import argparse
from first_follow import first_follow
class SLRParser:
def __init__(self, G):
self.G_prime = Grammar(f"{G.start}' -> {G.start}\n{G.grammar_str}")
self.max_G_prime_len = len(max(self.G_prime.grammar, key=len))
self.G_indexed = []
for head, bodies in self.G_prime.grammar.items():
for body in bodies:
self.G_indexed.append([head, body])
self.first, self.follow = first_follow(self.G_prime)
self.C = self.items(self.G_prime)
self.action = list(self.G_prime.terminals) + ['$']
self.goto = list(self.G_prime.nonterminals - {self.G_prime.start})
self.parse_table_symbols = self.action + self.goto
self.parse_table = self.construct_table()
def CLOSURE(self, I, verbose=False):
J = I
if(verbose):
print("THIS IS J")
print(J)
print("------------------------")
while True:
item_len = len(J)
for head, bodies in J.copy().items():
for body in bodies.copy():
if '.' in body[:-1]:
symbol_after_dot = body[body.index('.') + 1]
if symbol_after_dot in self.G_prime.nonterminals:
for G_body in self.G_prime.grammar[symbol_after_dot]:
J.setdefault(symbol_after_dot, set()).add(
('.',) if G_body == ('^',) else ('.',) + G_body)
if item_len == len(J):
return J
def GOTO(self, I, X):
goto = {}
for head, bodies in I.items():
for body in bodies:
if '.' in body[:-1]:
dot_pos = body.index('.')
if body[dot_pos + 1] == X:
replaced_dot_body = body[:dot_pos] + (X, '.') + body[dot_pos + 2:]
for C_head, C_bodies in self.CLOSURE({head: {replaced_dot_body}}).items():
goto.setdefault(C_head, set()).update(C_bodies)
return goto
def items(self, G_prime):
C = [self.CLOSURE({G_prime.start: {('.', G_prime.start[:-1])}}, verbose=True)]
while True:
item_len = len(C)
for I in C.copy():
for X in G_prime.symbols:
goto = self.GOTO(I, X)
print("GOTO ", goto)
if goto and goto not in C:
C.append(goto)
if item_len == len(C):
return C
def construct_table(self):
parse_table = {r: {c: '' for c in self.parse_table_symbols} for r in range(len(self.C))}
for i, I in enumerate(self.C):
for head, bodies in I.items():
for body in bodies:
if '.' in body[:-1]: # CASE 2 a
symbol_after_dot = body[body.index('.') + 1]
if symbol_after_dot in self.G_prime.terminals:
s = f's{self.C.index(self.GOTO(I, symbol_after_dot))}'
if s not in parse_table[i][symbol_after_dot]:
if 'r' in parse_table[i][symbol_after_dot]:
parse_table[i][symbol_after_dot] += '/'
parse_table[i][symbol_after_dot] += s
elif body[-1] == '.' and head != self.G_prime.start: # CASE 2 b
for j, (G_head, G_body) in enumerate(self.G_indexed):
if G_head == head and (G_body == body[:-1] or G_body == ('^',) and body == ('.',)):
for f in self.follow[head]:
if parse_table[i][f]:
parse_table[i][f] += '/'
parse_table[i][f] += f'r{j}'
break
else: # CASE 2 c
parse_table[i]['$'] = 'acc'
for A in self.G_prime.nonterminals: # CASE 3
j = self.GOTO(I, A)
if j in self.C:
parse_table[i][A] = self.C.index(j)
return parse_table
def print_info(self):
def fprint(text, variable):
print(f'{text:>12}: {", ".join(variable)}')
def print_line():
print(f'+{("-" * width + "+") * (len(list(self.G_prime.symbols) + ["$"]))}')
def symbols_width(symbols):
return (width + 1) * len(symbols) - 1
print('AUGMENTED GRAMMAR:')
for i, (head, body) in enumerate(self.G_indexed):
print(f'{i:>{len(str(len(self.G_indexed) - 1))}}: {head:>{self.max_G_prime_len}} -> {" ".join(body)}')
print()
fprint('TERMINALS', self.G_prime.terminals)
fprint('NONTERMINALS', self.G_prime.nonterminals)
fprint('SYMBOLS', self.G_prime.symbols)
print('\nFIRST:')
for head in self.G_prime.grammar:
print(f'{head:>{self.max_G_prime_len}} = {{ {", ".join(self.first[head])} }}')
print('\nFOLLOW:')
for head in self.G_prime.grammar:
print(f'{head:>{self.max_G_prime_len}} = {{ {", ".join(self.follow[head])} }}')
width = max(len(c) for c in {'ACTION'} | self.G_prime.symbols) + 2
for r in range(len(self.C)):
max_len = max(len(str(c)) for c in self.parse_table[r].values())
if width < max_len + 2:
width = max_len + 2
print('\nPARSING TABLE:')
print(f'+{"-" * width}+{"-" * symbols_width(self.action)}+{"-" * symbols_width(self.goto)}+')
print(f'|{"":{width}}|{"ACTION":^{symbols_width(self.action)}}|{"GOTO":^{symbols_width(self.goto)}}|')
print(f'|{"STATE":^{width}}+{("-" * width + "+") * len(self.parse_table_symbols)}')
print(f'|{"":^{width}}|', end=' ')
for symbol in self.parse_table_symbols:
print(f'{symbol:^{width - 1}}|', end=' ')
print()
print_line()
for r in range(len(self.C)):
print(f'|{r:^{width}}|', end=' ')
for c in self.parse_table_symbols:
print(f'{self.parse_table[r][c]:^{width - 1}}|', end=' ')
print()
print_line()
print()
def generate_automaton(self):
automaton = Digraph('automaton', node_attr={'shape': 'record'})
for i, I in enumerate(self.C):
I_html = f'<<I>I</I><SUB>{i}</SUB><BR/>'
for head, bodies in I.items():
for body in bodies:
I_html += f'<I>{head:>{self.max_G_prime_len}}</I> →'
for symbol in body:
if symbol in self.G_prime.nonterminals:
I_html += f' <I>{symbol}</I>'
elif symbol in self.G_prime.terminals:
I_html += f' <B>{symbol}</B>'
else:
I_html += f' {symbol}'
I_html += '<BR ALIGN="LEFT"/>'
automaton.node(f'I{i}', f'{I_html}>')
for r in range(len(self.C)):
for c in self.parse_table_symbols:
if isinstance(self.parse_table[r][c], int):
automaton.edge(f'I{r}', f'I{self.parse_table[r][c]}', label=f'<<I>{c}</I>>')
elif 's' in self.parse_table[r][c]:
i = self.parse_table[r][c][self.parse_table[r][c].index('s') + 1:]
if '/' in i:
i = i[:i.index('/')]
automaton.edge(f'I{r}', f'I{i}', label=f'<<B>{c}</B>>' if c in self.G_prime.terminals else c)
elif self.parse_table[r][c] == 'acc':
automaton.node('acc', '<<B>accept</B>>', shape='none')
automaton.edge(f'I{r}', 'acc', label='$')
automaton.view()
def LR_parser(self, w):
buffer = f'{w} $'.split()
pointer = 0
a = buffer[pointer]
stack = ['0']
symbols = ['']
results = {'step': [''], 'stack': ['STACK'] + stack, 'symbols': ['SYMBOLS'] + symbols, 'input': ['INPUT'],
'action': ['ACTION']}
step = 0
while True:
s = int(stack[-1])
step += 1
results['step'].append(f'({step})')
results['input'].append(' '.join(buffer[pointer:]))
if a not in self.parse_table[s]:
results['action'].append(f'ERROR: unrecognized symbol {a}')
break
elif not self.parse_table[s][a]:
results['action'].append('ERROR: input cannot be parsed by given grammar')
break
elif '/' in self.parse_table[s][a]:
action = 'reduce' if self.parse_table[s][a].count('r') > 1 else 'shift'
results['action'].append(f'ERROR: {action}-reduce conflict at state {s}, symbol {a}')
break
elif self.parse_table[s][a].startswith('s'):
results['action'].append('shift')
stack.append(self.parse_table[s][a][1:])
symbols.append(a)
results['stack'].append(' '.join(stack))
results['symbols'].append(' '.join(symbols))
pointer += 1
a = buffer[pointer]
elif self.parse_table[s][a].startswith('r'):
head, body = self.G_indexed[int(self.parse_table[s][a][1:])]
results['action'].append(f'reduce by {head} -> {" ".join(body)}')
if body != ('^',):
stack = stack[:-len(body)]
symbols = symbols[:-len(body)]
stack.append(str(self.parse_table[int(stack[-1])][head]))
symbols.append(head)
results['stack'].append(' '.join(stack))
results['symbols'].append(' '.join(symbols))
elif self.parse_table[s][a] == 'acc':
results['action'].append('accept')
break
return results
def print_LR_parser(self, results):
def print_line():
print(f'{"".join(["+" + ("-" * (max_len + 2)) for max_len in max_lens.values()])}+')
max_lens = {key: max(len(value) for value in results[key]) for key in results}
justs = {'step': '>', 'stack': '', 'symbols': '', 'input': '>', 'action': ''}
print_line()
print(''.join(
[f'| {history[0]:^{max_len}} ' for history, max_len in zip(results.values(), max_lens.values())]) + '|')
print_line()
for i, step in enumerate(results['step'][:-1], 1):
print(''.join([f'| {history[i]:{just}{max_len}} ' for history, just, max_len in
zip(results.values(), justs.values(), max_lens.values())]) + '|')
print_line()
def main():
file_name = "grammar.txt"
f = open(file_name, "r")
tokens = "id*id+ id"
G = Grammar(f.read())
# print(G.symbols)
# print(G.terminals)
slr_parser = SLRParser(G)
slr_parser.print_info()
results = slr_parser.LR_parser(tokens)
slr_parser.print_LR_parser(results)
slr_parser.generate_automaton()
if __name__ == "__main__":
main()