-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors_ula.py
72 lines (56 loc) · 2.1 KB
/
errors_ula.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
import sys
import os
from parse_ula import parser
def is_identifier_expression (tupletree):
return tupletree[0] == "IdentifierExpression"
def is_assignment_statement (tupletree):
return tupletree[0] == "AssignStatement"
def get_identifier_data (tupletree):
raw_str = tupletree[1][0]
details = {}
details["name"] = raw_str[raw_str.find(",")+1: raw_str.rfind(",")]
details["lineno"] = raw_str[raw_str.rfind(",")+1:]
return details
symbol_tbl = []
def print_tree(tupletree, depth=0):
global symbol_tbl
global infilename
outfilename = os.path.splitext(infilename)[0] + ".err"
outfile = open(outfilename, "w")
if is_identifier_expression(tupletree):
id_data = get_identifier_data(tupletree)
if id_data["name"] not in symbol_tbl:
print("semantic error on line %s" % id_data["lineno"])
print("semantic error on line %s" % id_data["lineno"], file=outfile)
outfile.close();
exit()
elif is_assignment_statement(tupletree):
id_data = get_identifier_data(tupletree)
if id_data["name"] in symbol_tbl:
print("semantic error on line %s" % id_data["lineno"])
print("semantic error on line %s" % id_data["lineno"], file=outfile)
outfile.close();
exit()
else:
symbol_tbl.append(id_data["name"])
for item in tupletree[1]:
if isinstance(item, tuple):
print_tree( item, depth + 1)
infilename = ""
def main():
global infilename
if len(sys.argv) == 2:
infilename = sys.argv[1]
if os.path.isfile(infilename):
infile = open(infilename, "r")
outfilename = os.path.splitext(infilename)[0] + ".err"
sys.stdout = open(outfilename, "w")
syntree = parser.parse(infile.read())
sys.stdout = sys.__stdout__
print_tree(syntree)
else:
print("Invalid file.")
else:
print("Specify filename, e.g. error_ula.ply my_program.ula")
if __name__ == "__main__":
main()