-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_table.py
86 lines (72 loc) · 2.67 KB
/
parsing_table.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
def build_parse_table(rules, tokens):
table = {}
first_sets = calculate_first_sets(rules, tokens)
follow_sets = calculate_follow_sets(rules, first_sets)
for rule in rules:
for prod in rules[rule]:
first_set = first_sets[prod[0]]
for ft in first_set - {'@@e'}:
table[(rule, ft)] = prod
if '@@e' in first_set:
for fl in follow_sets[rule]:
table[(rule, fl)] = prod
return table
def tested_update(dest_set, src_set):
result_set = dest_set.union(src_set)
has_changed = dest_set != result_set
dest_set.update(src_set)
return has_changed
def tested_add(dest_set, item):
if item in dest_set:
return False
else:
dest_set.add(item)
return True
def calculate_first_sets(rules, tokens):
first_sets = {}
for token in tokens:
if not token.startswith('@@'):
first_sets[token] = {token}
first_sets['@@e'] = {'@@e'}
for rule in rules:
first_sets[rule] = set()
changed = True
while changed:
changed = False
for rule in rules:
for production in rules[rule]:
add_e = True
for prod_symbol in production:
prod_first = first_sets[prod_symbol]
changed = changed or tested_update(
first_sets[rule], prod_first - {'@@e'})
if '@@e' not in prod_first:
add_e = False
break
if add_e:
changed = changed or tested_add(first_sets[rule], '@@e')
return first_sets
def calculate_follow_sets(rules, first_sets):
follow_sets = {}
for rule in rules:
if rule[0] == '#': # starting rule
follow_sets[rule] = {'@@eof'}
else:
follow_sets[rule] = set()
changed = True
while changed:
changed = False
for rule in rules:
for production in rules[rule]:
for i in range(len(production)-1):
if production[i][0] != '@':
first_set = first_sets[production[i+1]]
changed = changed or tested_update(
follow_sets[production[i]], first_set-{'@@e'})
if '@@e' in first_set:
changed = changed or tested_update(
follow_sets[production[i]], follow_sets[rule])
if production[-1][0] != '@':
changed = changed or tested_update(
follow_sets[production[-1]], follow_sets[rule])
return follow_sets