-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
54 lines (48 loc) · 1.98 KB
/
main.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
from scanner.scanner import scan, ScanError
from parser import lr1
from parser.symbol import Productions, Terminal
from parser.tools import NullableSet, First, Follow, LR1_parse_table
import pickle
import sys
def make_LR1_parse_table():
nullable_set = NullableSet(Productions)
first_set = First(Productions, Terminal, nullable_set)
productions, parse_tab = LR1_parse_table(
Productions, first_set, Terminal, nullable_set, start_symbol='<程序>')
pickle.dump((productions, dict(parse_tab)),
open('./data/LR1_parse_table.dat', 'wb'))
def main():
try:
fp = open('./data/LR1_parse_table.dat', 'rb')
except FileNotFoundError:
print(
"Please run \"python3 %s make-parse-table\" before you first use this program." % sys.argv[0])
return
show_tree_after_iteration = True if 'show-tree-animation' in sys.argv else False
show_simplify_tree = True if 'show-simplify-tree' in sys.argv else False
productions, parse_tab = pickle.load(fp)
try:
tokens, symbol_table, const_string_table = scan(sys.argv[1])
res = lr1.prase(tokens, parse_tab, productions, Terminal, symbol_table, const_string_table,
simpify=show_simplify_tree, show_tree_after_iteration=show_tree_after_iteration)
lr1.show_tree(res, symbol_table, const_string_table)
print('符号表:')
for s in symbol_table:
print('\t',s)
print('字符串表:')
for s in const_string_table:
print("\t'%s'"%s)
except ScanError as e:
e.show_error(sys.argv[1])
except lr1.ParseError as e:
e.show_error(sys.argv[1])
if __name__ == '__main__':
if len(sys.argv) < 2:
print(
'Usage: \tpython3 %s src_path [show-tree-animation] [show-simplify-tree]' % sys.argv[0])
print('\tpython3 %s make-parse-table' % sys.argv[0])
sys.exit()
if sys.argv[1] == 'make-parse-table':
make_LR1_parse_table()
else:
main()