-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·95 lines (70 loc) · 2.87 KB
/
main.py
File metadata and controls
executable file
·95 lines (70 loc) · 2.87 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
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import sys
import os
from lexical.ScanLex import scanlex
from lexical.structure.token.Token import token
from syntax.Syntax import syntax_process
from render.Render import tree_render
from semantic.Semantic import semantic_module
def printToken(token, option):
if "txt" == option:
print("<tokentype:", token.tokentype, "; tokenval:", token.tokenval,"; lexeme:", token.lexeme,"; line:", token.getNumberOfLine(),">")
elif "csv" == option:
print(token.tokentype,",",token.tokenval,",",token.lexeme,",",token.getNumberOfLine())
def printTokenList(option, tokenlist=[]):
for token in tokenlist:
printToken(token, option)
def main():
lex = scanlex(sys.argv[1])
analise = sys.argv[2]
#sm = semantic_module(syntax_tree)
#sm.walking_syntaxtree()
if analise == "lexical":
out_type = sys.argv[3]
if (out_type != "txt") and (out_type != "csv"):
print("Output file format is invalid. Please choose between txt or csv.")
else:
pass
#printTokenList(out_type, tokenlist)
elif analise == "syntax":
tokenlist = lex.getTokenListProcess()
syntax = syntax_process()
syntax_tree = syntax.exec(tokenlist)
if syntax_tree is not None:
try:
export = sys.argv[3]
render = tree_render()
out_name = sys.argv[4]
if export == "img":
render.exporToDotFile(syntax_tree, out_name)
render.compileDotFile(out_name)
print("it was exported to dot file and compiled to png image file format")
elif export == "txt":
render.exportToTxtFile(syntax_tree, out_name)
print("it was exported to txt file")
else:
print("invalid export option")
except IndexError:
if len(sys.argv) == 3:
render = tree_render()
render.renderTXT(syntax_tree)
else:
print("too few arguments.")
elif analise == "semantic":
tokenlist = lex.getTokenListProcess()
syntax = syntax_process()
syntax_tree = syntax.exec(tokenlist)
sm = semantic_module(syntax_tree)
if len(sys.argv) > 3:
if "--printTable" in sys.argv:
sm.printTable()
elif "--printTree" in sys.argv:
tree = sm.syntax_tree
render = tree_render()
filename = os.path.splitext(sys.argv[1])[0]
render.exporToDotFile(tree, filename)
render.compileDotFile(filename)
else:
print("Invalid analise option")
if __name__ == "__main__": main()