예제 #1
0
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)