-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
45 lines (32 loc) · 1.15 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
import sys
from antlr4 import *
from grammer.MinijavaLexer import MinijavaLexer
from grammer.MinijavaParser import MinijavaParser
from MinijavaPrintListener import MiniJavaPrintListener
from MinijavaErrorListener import MinijavaErrorListener
from MinijavaSymbolVisitor import MinijavaSymbolVisitor
def main(argv):
file_addr = argv[1]
file_name = file_addr.split('/')[-1]
name = file_name.split('.')[0]
file = open(file_addr)
text = file.read()
input = InputStream(text)
lexer = MinijavaLexer(input)
lexer.addErrorListener(MinijavaErrorListener())
stream = CommonTokenStream(lexer)
parser = MinijavaParser(stream)
tree = parser.goal()
visitor = MinijavaSymbolVisitor()
visitor.visitGoal(tree)
symbol_table = visitor.get_symbol_table()
print(symbol_table)
printer = MiniJavaPrintListener(symbol_table)
walker = ParseTreeWalker()
walker.walk(printer, tree)
codeStore = printer.get_bytecode()
for className, classCode in codeStore.items():
output = open('intermediateCode/' + className + '.j', 'w')
output.write(classCode)
if __name__ == '__main__':
main(sys.argv)