-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilationPrinciple3_5.py
More file actions
61 lines (51 loc) · 2.14 KB
/
CompilationPrinciple3_5.py
File metadata and controls
61 lines (51 loc) · 2.14 KB
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
from CompilationPrinciple3_4 import *
from prettytable import PrettyTable
def get_lr_table(item_sets, goto, grammar, nonterminals, terminals):
action_table = []
goto_table = []
for _ in range(len(item_sets)):
action_table.append({})
goto_table.append({})
for go in goto:
# go is a status shift in gotos
if go[2] in nonterminals:
goto_table[go[0]][go[2]] = go[1]
else:
action_table[go[0]][go[2]] = 's'+str(go[1])
for item_set in item_sets:
for item in item_set:
if item[1][-1] == '·':
item_index = item_sets.index(item_set)
production = item[0] + '→' + ''.join(item[1][:-1])
if production == grammar[0]:
action_table[item_index]['#'] = 'acc'
else:
r = 'r' + str(grammar.index(production))
for terminal in terminals:
action_table[item_index][terminal] = r
action_table[item_index]['#'] = r
break
return action_table, goto_table
if __name__ == '__main__':
terminals, nonterminals, productions, grammar = read_grammars()
item_sets, goto = get_lr0_item_sets_from_grammar(terminals, nonterminals, productions, show=False)
action_table, goto_table = get_lr_table(item_sets, goto, grammar, nonterminals, terminals)
# show tables
action_column = ['state'] + terminals + ['#']
goto_column = ['state'] + nonterminals[1:]
action_table_display = PrettyTable(action_column)
for i in range(len(action_table)):
row = [' ']*(len(action_column)-1)
for k, v in action_table[i].items():
row[action_column.index(k)-1] = v
row.insert(0, i)
action_table_display.add_row(row)
print(action_table_display)
goto_table_display = PrettyTable(goto_column)
for i in range(len(goto_table)):
row = [' ']*(len(goto_column)-1)
for k, v in goto_table[i].items():
row[goto_column.index(k)-1] = v
row.insert(0, i)
goto_table_display.add_row(row)
print(goto_table_display)