-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsingtable.py
60 lines (43 loc) · 1.66 KB
/
parsingtable.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 collections import defaultdict
from first import first
from follow import follow, EPSILON
from production import decodeProductionList
def createParseTable(input):
start, productionList = decodeProductionList(input)
firstMap = first(productionList)
followMap = follow(productionList, firstMap, start)
table = __create(productionList, firstMap, followMap)
##filter the same production in each value
for key, value in table.items():
table[key] = list(set(value))
return table
def __create(productionList, first, follow):
ans = defaultdict(list)
for production in productionList:
nonterminal = production.nonterminal
for right in production.rightList:
if right == EPSILON:
print("here")
firstInRight = getFirstFromRight(right, first)
for s in firstInRight:
## ensure s is termina
if s != EPSILON:
ans[(nonterminal, s)].append(nonterminal + "->" + right)
if EPSILON in firstInRight:
nonterminalFollow = follow[nonterminal]
for s in nonterminalFollow:
ans[(nonterminal, s)].append(nonterminal + "->" + right)
if "$" in nonterminalFollow:
ans[(nonterminal, "$")].append(nonterminal + "->" + right)
## filter the same production
return ans
def getFirstFromRight(right, firstMap):
ans = []
subans = firstMap[right[0]]
ans.extend(subans)
i =1
while i < len(right) and EPSILON in subans:
subans = firstMap[right[i]]
i += 1
ans.extend(subans)
return list(set(ans))