Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

executable file 61 lines (50 sloc) 1.73 kb
import sys
import os
from collections import defaultdict
import ply.lex as lex
import ply.yacc as yacc
import tokRules
from yaccConfig import *
from semanticAnalysis import analyse
from codeGenerator import CodeGenerator
import grammarExceptions as e
registers = ['r%d' %r for r in range(8,16)] + ["rbx", "rcx", "rdx", "rsi", "rdi"]
def run():
if len(sys.argv) > 1:
fName = sys.argv[1]
if os.path.getsize(fName):
maliceprogram = open(fName, 'r').read()
base, ext = os.path.splitext(os.path.basename(fName))
if ext == ".alice":
code = parse_code(maliceprogram)
writeASM( code, base )
else:
print "Error! Filetype is not of format .alice."
return 1
return 0
else:
print "Error! No given files."
return 1
def parse_code(code):
try:
lexer = lex.lex(module=tokRules)
parser = yacc.yacc()
result = parser.parse(code)
if result:
symbolTable = {}
flags = defaultdict(set)
analyse(symbolTable, result, flags)
cg = CodeGenerator(symbolTable, registers, flags)
code = cg.generate(result)
return code
except (e.SemanticException, e.NoMatchException, e.SyntaxException, e.LexicalException, e.DivisionByZeroException) as exception:
print exception.value
print "(Paragraph : %d Clause: %d)" %(exception.lineno, exception.clauseno)
sys.exit(1)
def writeASM( result, fileName ):
asmFile = open(fileName + ".asm", 'w')
for line in result:
asmFile.write(line + "\n")
asmFile.close()
if __name__ == '__main__':
run()
Jump to Line
Something went wrong with that request. Please try again.