-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
96 lines (87 loc) · 2 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
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
87
88
89
90
91
92
93
94
95
96
import sys
from starcc.lexer import Lexer
from starcc.parse import Parse
from starcc.check import Check
from starcc.passes import Passes
from starcc.assembly import Assembly
file_name = ""
source_stream = ""
# 语法分析
# -p
def parse():
# 词法分析
lexer = Lexer(source_stream)
lexer.main()
# for i in lexer.tokens:
# print("(%s, %s)" % (i.type,i.value))
# 语法分析
parse = Parse(lexer.tokens)
parse.main()
parse.drawTree(parse.grammar_tree,file_name)
# 词法分析
# -l
def lexer():
lexer = Lexer(source_stream)
lexer.main()
for i in lexer.tokens:
print("(%s, %s)" % (i.type,i.value))
# 中间代码生成器
# -r
def passes():
# 词法分析
lexer = Lexer(source_stream)
lexer.main()
# for i in lexer.tokens:
# print("(%s, %s)" % (i.type,i.value))
# 语法分析
parse = Parse(lexer.tokens)
parse.main()
# parse.drawTree(parse.grammar_tree,file_name)
# 符号检查
check = Check(parse)
check.main()
# parse.drawTree(check.parse.grammar_tree)
# 中间代码生成
passes = Passes(check)
passes.main()
passes.Fun_insn_print()
# parse.drawTree(passes.parse_tree)
# 目标代码生成
def assembly():
# 词法分析
lexer = Lexer(source_stream)
lexer.main()
# for i in lexer.tokens:
# print("(%s, %s)" % (i.type,i.value))
# 语法分析
parse = Parse(lexer.tokens)
parse.main()
# parse.drawTree(parse.grammar_tree)
# 符号检查
check = Check(parse)
check.main()
# parse.drawTree(check.parse.grammar_tree)
# 中间代码生成
passes = Passes(check)
passes.main()
# 生成汇编代码
assembly = Assembly(passes)
assembly.main(file_name)
if __name__ == '__main__':
for opt in sys.argv[1:]:
if opt == "-f":
file_name = sys.argv[sys.argv.index(opt)+1]
# print(file_name)
source_file = open(file_name,"r")
source_stream = source_file.read()
file_name = file_name.split(".c")[0]
# print(source_stream)
# print(len(source_stream))
elif opt == "-l":
lexer()
elif opt == "-p":
parse()
elif opt == "-r":
passes()
elif opt == "-s":
assembly()