-
Notifications
You must be signed in to change notification settings - Fork 1
/
ll1.py
252 lines (207 loc) · 7.41 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
#!/usr/bin/env python
import bnf_parser
from grammar import Grammar, Production
def _update_set(dst: set, to_add: set) -> bool:
old_len = len(dst)
dst.update(to_add)
return old_len != len(dst)
def construct_first(grammar: Grammar) -> list:
def construct_first_nterm(nterm: str) -> bool:
changed = False
for prod in grammar.prods[nterm]:
for sym in prod.syms:
if sym != '@' and tuple(first[sym]) != ('@',):
if _update_set(first[nterm], first[sym]):
changed = True
break
else:
if _update_set(first[nterm], {'@'}):
first[nterm].add('@')
changed = True
return changed
first = dict([(sym, set()) for sym in grammar.prods])
first['@'] = {'@'}
# FIRST(a) = {a}
for term in grammar.terms:
first[term] = {term}
changed = True
while changed:
changed = False
for nterm in grammar.prods:
if construct_first_nterm(nterm):
changed = True
return first
# returns {'@'} on empty syms
def get_first_from_syms(first: list, syms: list) -> set:
# First, assume that eps is already in FIRST
result = {'@'}
for sym in syms:
result.update(first[sym])
if '@' not in first[sym]:
# If the eps transition link stops here, remove eps
return result - {'@'}
# Eps transition link doesn't stop till end, keep it
return result
def construct_follow(grammar: Grammar, first: list) -> list:
def construct_follow_nterm(prod: Production) -> bool:
changed = False
i = 0
while i < len(prod.syms):
nterm = prod.syms[i]
# Only process nonterminals
if not grammar.is_nonterminal(nterm):
i += 1
continue
i += 1
remaining_syms = prod.syms[i:]
remaining_first = get_first_from_syms(first, remaining_syms)
if _update_set(follow[nterm], remaining_first - {'@'}):
changed = True
if '@' in remaining_first:
if _update_set(follow[nterm], follow[prod.nterm]):
changed = True
return changed
follow = dict([(sym, set()) for sym in grammar.prods])
follow[grammar.start] = {'$'}
changed = True
while changed:
changed = False
for prodlist in grammar.prods.values():
for prod in prodlist:
if construct_follow_nterm(prod):
changed = True
return follow
def construct_table(grammar: Grammar, first: list, follow: list) -> dict:
# table[nterm][term] = Production
table = dict([(sym, list()) for sym in grammar.prods])
for nterm, prodlist in grammar.prods.items():
for prod in prodlist:
first_set = get_first_from_syms(first, prod.syms)
for term in first_set:
if term == '@':
for term in follow[nterm]:
if term != '@':
table[nterm].append((term, prod))
else:
table[nterm].append((term, prod))
return table
def construct_conflicts(table: dict) -> list:
# result[index] = tuple(nonterm, term, list(Productions))
result = list()
for nterm, pairs in table.items():
terms = set([pair[0] for pair in pairs])
for term in terms:
prods = list(filter(lambda pair, term=term: pair[0] == term, pairs))
if len(prods) != 1:
result.append((nterm, term, [pair[1] for pair in prods]))
return result
def _str_first_or_follow(grammar: Grammar, first: list, title) -> str:
result = ' ' + title + ':'
for sym in grammar.prods:
result += '\n {}: {}'.format(sym, ' '.join(first[sym]))
return result
def str_follow(grammar: Grammar, follow: list) -> str:
return _str_first_or_follow(grammar, follow, 'FOLLOW')
def str_first(grammar: Grammar, first: list) -> str:
return _str_first_or_follow(grammar, first, 'FIRST')
def str_table(table: dict) -> str:
result = ' Table:'
for nterm, pairs in table.items():
result += '\n {}:'.format(nterm)
for term, prod in sorted(pairs, key=lambda x: x[0]):
result += '\n {}: {}'.format(term, prod)
return result
def str_conflicts(conflicts: list) -> str:
result = ''
for nterm, term, prods in conflicts:
result += '\n {}, {}:'.format(nterm, term)
for prod in prods:
result += '\n ' + str(prod)
if result:
return ' Conflicts:' + result
else:
return ' Conflicts: None'
def str_ll1(grammar: Grammar) -> str:
first = construct_first(grammar)
follow = construct_follow(grammar, first)
table = construct_table(grammar, first, follow)
conflicts = construct_conflicts(table)
result = 'LL(1):'
result += '\n' + str_first(grammar, first)
result += '\n' + str_follow(grammar, follow)
result += '\n' + str_table(table)
result += '\n' + str_conflicts(conflicts)
return result
def parse(grammar: Grammar, table: dict, syms: list, callback):
stack = [grammar.start]
pos = 0
callback('INIT', stack, pos, None)
while stack:
sym = '$'
if pos < len(syms):
sym = syms[pos]
top = stack.pop()
if grammar.is_terminal(top):
assert sym == top
pos += 1
if pos > len(syms):
pos = len(syms)
callback('MATCH', stack, pos, sym)
else:
prod = list(filter(lambda x: x[0] == sym, table[top]))[0]
prod = prod[1]
stack_syms = prod.syms[-1::-1]
if tuple(stack_syms) != ('@',):
stack.extend(stack_syms)
callback('OUTPUT', stack, pos, prod)
def str_parse(grammar: Grammar, table: dict, syms: list):
def callback(action, stack, pos, info):
str_action = ''
if action == 'MATCH':
str_action = 'match ' + info
elif action == 'OUTPUT':
str_action = 'output ' + str(info)
inputs.append(' '.join(syms[pos:]) + ' $')
stacks.append(' '.join(stack[-1::-1]) + ' $')
actions.append(str_action)
inputs = list()
stacks = list()
actions = list()
parse(grammar, table, syms, callback)
get_length = lambda arr: max([len(t) for t in arr])
inputs_length = get_length(inputs)
stacks_length = get_length(stacks)
result = ''
for i, input_val in enumerate(inputs):
result += ' {} | {} | {}\n'.format(
input_val.rjust(inputs_length), stacks[i].rjust(stacks_length),
actions[i])
return result
def _demo_construction(bnf):
grammar = bnf_parser.parse(bnf)
print(grammar)
print(str_ll1(grammar))
def _demo_parse(grammar: Grammar, syms: str):
first = construct_first(grammar)
follow = construct_follow(grammar, first)
table = construct_table(grammar, first, follow)
conflicts = construct_conflicts(table)
assert not conflicts
demo_parse(grammar, table, syms)
def main():
bnf = '''
E := T E'
E' := + T E' | @
T := F T'
T' := * F T' | @
F := ( E ) | id
'''
_demo_parse(bnf_parser.parse(bnf), 'id + id * id'.split())
bnf = '''
S := 'i' E 't' S S' | a
S' := 'e' S | @
E := b
'''
_demo_construction(bnf)
if __name__ == '__main__':
main()