-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
128 lines (107 loc) · 3.52 KB
/
compiler.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import scanner
from Parser import parser
from SemanticLevel import ErrorType
from SemanticLevel.SemanticRoutines import program_block
from Tools.Development import develop_mode
# amir mohammad mohammadi 97107126
# sajad faghfur maghreby 97106187
def file2str(filepath):
f = open(filepath, "r")
ls_text = f.readlines()
s = ""
for txt in ls_text:
s += txt
return s
def save_tuple2file_based_on_1element(file_name, tl):
f = open(file_name, mode="w")
this_el = -1
for t in tl:
# resolve \n error
if "\n" in t[1]:
t = (t[0] - 1, t[1].replace("\n", ""), t[2])
# t[0] = int(t[0]) - 1
# t[1].replace("\n", "")
if t[0] == this_el:
f.write("(" + t[1] + ", " + t[2] + ") ")
else:
if this_el != -1:
f.write("\n")
this_el = t[0]
f.write(str(this_el) + "." + " ")
# f.write('{0:4}'.format(str(this_el)+'.'))
f.write(("(" + t[1] + ", " + t[2] + ") "))
# f.write('(' + repr(t[1]) + ', ' + repr(t[2]) + ') ')
def empty_error_file(file_name):
f = open(file_name, mode="w")
f.write("There is no lexical error.")
def save_list2file(file_name, l):
f = open(file_name, mode="w")
for i, t in enumerate(l):
f.write("{0:4}".format(str(i + 1) + "."))
f.write(t + "\n")
def main():
# addr = './pa_1/PA1_testcases1.2/T01/input1.txt'
# addr = './pa_1/PA1_testcases1.2/test/input1.txt'
addr = "input.txt"
s = file2str(
addr,
)
scnr = scanner.scanner(s=s)
while True:
line, next_token_type, next_token = scnr.get_next_token()
ErrorType.gl_line_number = line
if next_token_type is None:
parser.get_next_token("$", line)
break
else:
parser.get_next_token(
(str(next_token_type), str(next_token)), line)
# print("{: >3}{: >20}{: >20}".format(*[line, next_token, next_token_type]))
# if scnr.errors.__len__() == 0:
# empty_error_file("lexical_errors.txt")
# else:
# save_tuple2file_based_on_1element("lexical_errors.txt", scnr.errors)
if develop_mode:
save_tuple2file_based_on_1element("tokens.txt", scnr.tokens)
save_list2file("symbol_table.txt", scnr.lexemes)
save_tree("parse_tree.txt")
save_syntax_errors("syntax_errors.txt")
save_semantic_errors("semantic_errors.txt")
parser.pp_list_of_tuples(program_block)
def save_tree(addr):
tree = parser.draw_tree()
# print(tree)
f = open(addr, "w", encoding="utf-8")
f.write(tree)
f.close()
def save_syntax_errors(addr):
errors = parser.get_pars_errors()
print("syntax errors:[")
if errors.__len__() == 0:
f = open(addr, "w", encoding="utf-8")
f.write("There is no syntax error.")
f.close()
else:
f = open("syntax_errors.txt", "w", encoding="utf-8")
for e in errors:
print(e+"\n")
f.write(e + "\n")
f.close()
print("]")
def save_semantic_errors(addr):
errors = ErrorType.semantic_errors
if errors.__len__() == 0:
f = open(addr, "w", encoding="utf-8")
f.write("The input program is semantically correct.")
f.close()
else:
f = open(addr, "w", encoding="utf-8")
for e in errors:
# e = e.replace("'", '')
f.write(e + "\n")
f.close()
if __name__ == "__main__":
# try:
main()
# except:
# print("an unexpected error occurred")