-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompiler.py
37 lines (28 loc) · 1.1 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
from antlr4 import InputStream, CommonTokenStream, ParseTreeWalker
from gen.jauanLexer import jauanLexer
from gen.jauanParser import jauanParser
from gen.MyListener import MyListener
from jasmin import execute, compile
from argparse import ArgumentParser
from antlr4.error.ErrorListener import ErrorListener
class MyLexerErrorListener(ErrorListener):
def syntaxError(self, recognizer, offendingSymbol, line, column, msg, e):
raise SystemExit(
Exception(f"Erro lexico: linha {line}:{column} {msg}"))
def executaCompilacao(expressao):
input_stream = InputStream(expressao)
lexer = jauanLexer(input_stream)
tokens = CommonTokenStream(lexer)
parser = jauanParser(tokens)
parser.removeErrorListeners()
parser.addErrorListener(MyLexerErrorListener())
tree = parser.prog()
walker = ParseTreeWalker()
l = MyListener()
walker.walk(l, tree)
compile('programaJasmin')
execute('programaJasmin')
if __name__ == '__main__':
with open('input.txt', 'r') as f:
expressao = f.read()
executaCompilacao(expressao)