def p_pn_quadruples_checkfunclength(p): ''' pn_quadruples_checkfunclength : empty ''' #Print("PN --- CIF 1 addgotof ") Quadruples.checkfunclenght(aux_parameter, p.lineno(-1)) aux_parameter.reset()
def p_pn_quadruples_addfuncion(p): ''' pn_quadruples_addfuncion : empty ''' #print("PN --- 1 addvariable " + p[-1]) vardato = tabla_varibles.get_variable(aux_dato.name, 'global', p.lineno(-1)) Quadruples.create_return_temporal(vardato)
def p_pn_quadruples_getarrlen(p): ''' pn_quadruples_getarrlen : empty ''' #print("PN --- 1 addconstantstring " + str(p[-1])) aux_dato.name = p[-2] vardato = tabla_varibles.get_variable(aux_dato.name, aux_tabla.name, p.lineno(-1)) Quadruples.get_variable_arr_len(vardato, p.lineno(-1))
def p_pn_quadruples_getfuncid(p): ''' pn_quadruples_getfuncid : empty ''' #Print("PN --- 1 addvariable " + p[-1]) funcid = p[-2] tabla_varibles.get_function_info(funcid, aux_parameter, p.lineno(-1)) Quadruples.addfuncid(funcid)
def p_pn_quadruples_checkreturn(p): ''' pn_quadruples_checkreturn : empty ''' vardato = tabla_varibles.get_variable(aux_tabla.name, 'return', p.lineno(-1)) Quadruples.PilaDato.append(vardato) Quadruples.POper.append('return') Quadruples.check_top_poper('return', p.lineno(-1))
def p_pn_quadruples_insert_str(p): ''' pn_quadruples_insert_str : empty ''' #print("PN --- 1 addconstantstring " + str(p[-1])) aux_dato.name = p[-2] vardato = tabla_varibles.get_variable(aux_dato.name, aux_tabla.name, p.lineno(-1)) Quadruples.pn_quadruples_insert_str(vardato)
def p_pn_quadruples_addvariablearr(p): ''' pn_quadruples_addvariablearr : empty ''' #print("PN --- 1 addvariablearr " + p[-1]) aux_dato.name = p[-4] index = p[-2] vardato = tabla_varibles.get_variable(aux_dato.name, aux_tabla.name, p.lineno(-1)) #print(vardato) Quadruples.get_variable_arr(vardato, index, p.lineno(-1))
def p_pn_quadruples_addvariablearrvar(p): ''' pn_quadruples_addvariablearrvar : empty ''' #print("PN --- 1 addvariablearr " + p[-1]) aux_dato.name = p[-4] index = tabla_varibles.get_variable(p[-2], aux_tabla.name, p.lineno(-1)) vardato = tabla_varibles.get_variable(aux_dato.name, aux_tabla.name, p.lineno(-1)) direccion = "("+str(index['direccion'])+")" Quadruples.get_variable_arr(vardato, direccion, p.lineno(-1))
def p_muere(p): ''' muere : empty ''' #tabla_varibles.get_constant_table() print("Quadruplos aqui") conta = 0 with open('JSON/datasss.json', 'w') as outfile: json.dump(tabla_varibles.diccionario, outfile) for q in Quadruples.PQuad: print(conta,q.operator,q.left_operand,q.right_operand,q.result) conta += 1 Quadruples.print_quad()
def p_pn_quadruples_checkfuncid(p): ''' pn_quadruples_checkfuncid : empty ''' #Print("PN --- CIF 1 addgotof ") right_operand = Quadruples.PilaDato.pop() left_operand = Quadruples.PilaDato.pop() tabla_varibles.check_param_arr(right_operand,left_operand,aux_parameter.name, p.lineno(-1)) inicio = tabla_varibles.diccionario['dirFunc'][aux_parameter.name]['inicio'] #print(inicio) Quadruples.check_param_arr_quads(left_operand,inicio) Quadruples.PilaDato.append(left_operand) Quadruples.PilaDato.append(right_operand) Quadruples.check_top_poper('parameter', p.lineno(-1))
def p_pn_st_functionid(p): ''' pn_st_functionid : empty ''' aux_tabla.name = p[-1] aux_tabla.posicion_inical = Quadruples.get_position() tabla_varibles.create_table_function(aux_tabla, p.lineno(-1))
def p_pn_quadruples_checkequals(p): ''' pn_quadruples_checkequals : empty ''' #print("PN Quadruples --- 12 checkequals") Quadruples.check_top_poper('equal', p.lineno(-1))
def p_pn_quadruples_addgoto(p): ''' pn_quadruples_addgoto : empty ''' #Print("PN --- CIF 3 addgoto ") Quadruples.addgoto()
def p_pn_quadruples_addfuncid(p): ''' pn_quadruples_addfuncid : empty ''' #Print("PN --- CIF 1 addgotof ") Quadruples.addparams(aux_parameter, p.lineno(-1))
def p_pn_quadruples_checklogical(p): ''' pn_quadruples_checklogical : empty ''' #Print("PN Quadruples --- 11 checklogical") Quadruples.check_top_poper('logical', p.lineno(-1))
def p_pn_quadruples_checkprint(p): ''' pn_quadruples_checkprint : empty ''' #Print("PN --- CIF 1 addgotof ") Quadruples.check_top_poper('print', p.lineno(-1))
def p_pn_quadruples_checkrelational(p): ''' pn_quadruples_checkrelational : empty ''' #Print("PN Quadruples --- 9 checkrelational") Quadruples.check_top_poper('relational', p.lineno(-1))
def p_funciones_especiales_statement(p): ''' funciones_especiales_statement : expresion funciones_especiales_statement_loop ''' Quadruples.pop_poper_funciones_especiales(aux_special_func.name, p.lineno(-1))
def p_funciones_especiales_end(p): ''' funciones_especiales_end : empty ''' aux_special_func.reset() Quadruples.pop_poper_funciones_especiales_end()
def p_pn_quadruples_resettemporales(p): ''' pn_quadruples_resettemporales : empty ''' #print("PN Quadruples --- 12 checkequals") Quadruples.reset_temporales()
from antlr4 import * from PpLexer import PpLexer from PpParser import PpParser from PpListener import PpListener from SymbolsTable import SymbolsTable from SemanticCube import SemanticCube from Quadruples import Quadruples from ObjGenerator import ObjGenerator if len(sys.argv) < 2: print("You must provide the file you want to compile") exit() try: pp_file = FileStream(sys.argv[1]) except Exception: print(f"Could not read {sys.argv[1]}") exit() lexer = PpLexer(pp_file) stream = CommonTokenStream(lexer) parser = PpParser(stream) tree = parser.r() if parser.getNumberOfSyntaxErrors() > 0: print("Compilation ended due to syntax errors") exit() listener = PpListener(SymbolsTable(), SemanticCube(), Quadruples(isFunc=False), ObjGenerator(), Path(sys.argv[1]).stem) walker = ParseTreeWalker() walker.walk(listener, tree)
def p_pn_quadruples_checksumres(p): ''' pn_quadruples_checksumres : empty ''' #Print("PN Quadruples--- 4 checksumres") Quadruples.check_top_poper('sumres', p.lineno(-1))
def p_pn_quadruples_endproc(p): ''' pn_quadruples_endproc : empty ''' #Print("PN --- CIF 1 addgotof ") Quadruples.endproc()
def p_pn_quadruples_checkmuldiv(p): ''' pn_quadruples_checkmuldiv : empty ''' #Print("PN Quadruples --- 3 checkmuldiv") Quadruples.check_top_poper('muldiv', p.lineno(-1))
def p_pn_quadruples_gotomain(p): ''' pn_quadruples_gotomain : empty ''' #Print("PN --- CIF 1 addgotof ") Quadruples.gotomain()
def p_pn_quadruples_closegotoup(p): ''' pn_quadruples_closegotoup : empty ''' #Print("PN --- CIF 2 closegotodown ") Quadruples.closegotoup()
''' VIP Parser Authors: David Souza & Eduardo de la Garza ''' import ply.yacc as yacc from scanner import tokens from Quadruples import Quadruples from constants import Operator, operators Q = Quadruples() def p_program(p): '''program : vars functions main | vars main | functions main | main''' def p_functions(p): '''functions : functions function | function''' def p_function(p): 'function : function_header function_body n_end_function' def p_main(p): 'main : FUNCTION MAIN L_PARENS R_PARENS n_start_main function_body n_end_main'
def p_pn_quadruples_addgotov(p): ''' pn_quadruples_addgotov : empty ''' #Print("PN --- CIF 1 addgotof ") Quadruples.addgotov()
from Operand import Operand from VirtualAddresses import VirtualAdresses from Tools import determineTypeAddressTable from Tools import deleteAddressesOfFunc #python variables funcDirec = FuncDirec() funcName = "" funcType = "" varType = "" varName = "" cteValue = "" cteType = "" operandsList = [] operatorsList = [] quadruples = Quadruples() errorQueue = [] gotoList = [] paramsCallCounter = 0 funcCall = "" pendingReturnOperand = "" hasReturn = False mustBeVoidCall = False idForArray = "" #Variables globales dirAddresses = { "globalInt": VirtualAdresses(15000, 15099, "globalInt"), "globalFloat": VirtualAdresses(15100, 15199, "globalFloat"), "globalChar": VirtualAdresses(15200, 15299, "globalChar"), #Variables locales
def p_pn_quadruples_checkgotomain(p): ''' pn_quadruples_checkgotomain : empty ''' #print("PN Quadruples --- 12 checkequals") Quadruples.check_gotomain()