-
Notifications
You must be signed in to change notification settings - Fork 2
/
follow.py
60 lines (52 loc) · 1.88 KB
/
follow.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
from input import rules, diction, firsts, follows, nonterm_userdef, term_userdef, sample_input_string
from first import first
def follow(nt):
solset = set()
if nt == start_symbol:
solset.add('$')
for curNT in diction:
rhs = diction[curNT]
for subrule in rhs:
if nt in subrule:
while nt in subrule:
index_nt = subrule.index(nt)
subrule = subrule[index_nt + 1:]
if len(subrule) != 0:
res = first(subrule)
if '#' in res:
newList = []
res.remove('#')
ansNew = follow(curNT)
if ansNew != None:
if type(ansNew) is list:
newList = res + ansNew
else:
newList = res + [ansNew]
else:
newList = res
res = newList
else:
if nt != curNT:
res = follow(curNT)
if res is not None:
if type(res) is list:
for g in res:
solset.add(g)
else:
solset.add(res)
return list(solset)
def computeAllFollows():
for NT in diction:
solset = set()
sol = follow(NT)
if sol is not None:
for g in sol:
solset.add(g)
follows[NT] = solset
print("\nCalculated follows: ")
key_list = list(follows.keys())
index = 0
for gg in follows:
print(f"follow({key_list[index]})"
f" => {follows[gg]}")
index += 1