def p_assignment(self, p): """assignment : lvalue ASSIGNMENT exp SEMICOLON""" # print("""assignment -> lvalue ASSIGNMENT exp SEMICOLON""") p[0] = Nonterminal() if p[3].func == 1: p[0].code = p[3].code + "\n" + p[1].get_value() + "=" + p[3].get_value() +";\n" print(p[0].code) self.check = True else: p[0].code = p[3].code + p[1].place + " = " + p[3].get_value() + ";\n" if self.check == False: self.FuncVARIABLES.append(p[1].get_value())
def p_assignment(self, p): """assignment : lvalue ASSIGNMENT exp SEMICOLON""" # print("""assignment -> lvalue ASSIGNMENT exp SEMICOLON""") p[0] = Nonterminal() if p[3].place != "": print(p[3].code) p[0].code = "L" + str(len( self.code_list)) + ": " + p[1].value + "=" + p[3].place + ";" p[0].address = len(self.code_list) self.code_list.append(p[0].code) p[0].code = p[3].code + p[0].code print(self.code_list) self.produce_output() elif p[3].value != "": p[0].code = "L" + str(len( self.code_list)) + ": " + p[1].value + "=" + p[3].value + ";" p[0].address = len(self.code_list) self.code_list.append(p[0].code) print(self.code_list) self.produce_output() else: true_label = len(self.code_list) false_label = true_label + 2 c = "L" + str(true_label) + ": " + p[1].value + "=" + "true" + ";" self.code_list.append(c) d = "L" + str(len( self.code_list)) + ": " + "goto " + str(false_label + 1) + ";" self.code_list.append(d) c = "L" + str( false_label) + ": " + p[1].value + "=" + "false" + ";" self.code_list.append(c) d = "L" + str(false_label + 1) + ": " self.code_list.append(d) for code in self.code_list: index = self.code_list.index(code) if "goto" in code and "_" in code: if "if" in code: new_code = code.replace("_", "L" + str(true_label)) self.code_list[index] = new_code else: new_code = code.replace("_", "L" + str(false_label)) self.code_list[index] = new_code print(self.code_list) self.produce_output()
def p_var_dec(self, p): """var_dec : var_type var_list SEMICOLON""" print("""var_dec -> var_type var_list SEMICOLON""") p[0] = Nonterminal() self.variable_decs += [p[1].rtype + " " + var + ";\n" for var in p[2].vars] self.VARIABLES += p[2].vars p[0].code = "\n" + p[2].code
def p_program(self, p): """program : macros classes""" print("""program -> macros classes""") p[0] = Nonterminal() p[0].sym_var = p[2].sym_var print("symbol table list :") print(self.symbol_table_list) include = "#include <stdio.h>" + "\n" include += "#include <stdio.h>" + "\n" if self.t_counter > -1: variables_declaration = "double " for i in range(0, self.t_counter + 1): if i == self.t_counter: variables_declaration += "TT" + str(i) + ";" else: variables_declaration += "TT" + str(i) + ", " else: variables_declaration = "" stack_components_declaration = "void* returnAddress;\ndouble * top = (double*) malloc(1000 * sizeof(double));\nvoid ** labelsTop = (void**) malloc(1000 * sizeof(void*));\ntop += 1000;\nlabelsTop += 1000;" goto_to_main = "goto _main;\n\n" p[0].code = include + "int main()\n{\n\n" + stack_components_declaration + "\n" + variables_declaration + "\n"+ self.variable_decs + goto_to_main + p[2].code + "\n\nend : return 0;\n}" file = open("final_result.c", "w") file.write(p[0].code) file.close()
def p_statement_1(self, p): """statement : assignment""" print("""statement -> assignment""") p[0] = Nonterminal() p[0].code = p[1].code self.check= False print(p[0].code)
def generate_boolean_code(self, p, code_list): p[0] = Nonterminal() next_quad = len(code_list) p[0].true_list = [next_quad] p[0].false_list = [next_quad + 1] if p[1].place != "" and p[3].place != "": code = "L" + str(next_quad) + ": " + "if " + p[1].place + p[2] + p[ 3].place + " goto _" + ";" elif p[1].place != "" and p[3].place == "": code = "L" + str(next_quad) + ": " + "if " + p[1].place + p[2] + p[ 3].value + " goto _" + ";" elif p[1].place == "" and p[3].place != "": code = "L" + str(next_quad) + ": " + "if " + p[1].value + p[2] + p[ 3].place + " goto _" + ";" else: code = "L" + str(next_quad) + ": " + "if " + p[1].value + p[2] + p[ 3].value + " goto _" + ";" code_list.append(code) code = "L" + str(next_quad + 1) + ": " + 'goto _' + ";" code_list.append(code) p[0].m = next_quad + 2 p[0].code = code_list p[0].type = "bool" print("comparison_operation -> exp " + p[2] + " exp") print(code_list)
def p_print(self, p): """print : PRINT LP STRING RP SEMICOLON""" p[0] = Nonterminal() print("""print -> PRINT LP STRING RP SEMICOLON""") p[0].code = p[1] + p[2] + p[3] + p[4] + p[5] self.code_list.append((p[0].code)) self.produce_output()
def p_actual_arguments_list_1(self, p): """actual_arguments_list : actual_arguments_list COMMA exp""" print("""actual_arguments_list -> actual_arguments_list COMMA exp""") p[0] = Nonterminal() p[0].code = p[1].code + p[3].code p[0].args = p[1].args p[0].args.append(p[3].get_value())
def p_var_list_item_1(self, p): """var_list_item : ID""" p[0] = Nonterminal() p[0].value = p[1] p[0].code = "double " + p[0].value + ";" self.code_list.append(p[0].code) print("""var_list_item -> ID""")
def generate_arithmetic_code(self, p, tmp, code_list): print( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^" ) p[0] = Nonterminal() p[0].place = tmp if len(p) == 4: print( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^4" ) arg1 = "" arg2 = "" if p[1].place == "": arg1 = p[1].value else: arg1 = p[1].place if p[3].place == "": arg2 = p[3].value else: arg2 = p[3].place p[0].code = "L" + str( len(code_list )) + ": " + p[0].place + "=" + arg1 + p[2] + arg2 + ";" print(p[0].code) code_list.append(p[0].code) print(code_list) elif len(p) == 3: print( "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^3" ) arg1 = "" arg2 = "" if p[2].place == "": arg1 = p[2].value else: arg1 = p[2].place p[0].code = "L" + str( len(code_list)) + ": " + p[0].place + "=" + p[1] + arg1 + ";" print(p[0].code) code_list.append(p[0].code) print(code_list)
def p_exp_12(self, p): """exp : function_call""" print("""exp -> function_call""") p[0] = Nonterminal() p[0] = p[1] p[0].code = p[1].code p[0].place = p[1].get_value() p[0].func = 1
def p_logical_operation_1(self, p): """logical_operation : exp OR exp""" print("""logical_operation -> exp OR exp""") p[0] = Nonterminal() false_label = self.new_label() self.code_generator.back_patch_false(p[1], false_label) p[0].code = p[1].code + false_label + ": // logical calculation (OR)\n" + p[3].code
def p_elseifs_2(self, p): """elseifs : elseif""" print("""elseifs -> elseif""") p[0] = Nonterminal() p[0].code = [] p[0].code.append(p[1].code) p[0].list = [] p[0].list.append(p[1].list)
def p_logical_operation(self, p): """logical_operation : exp AND exp""" print("""logical_operation -> exp AND exp""") p[0] = Nonterminal() true_label = self.new_label() self.code_generator.back_patch_true(p[1], true_label) p[0].code = p[1].code + true_label + ": // logical calculation (AND)\n" + p[3].code
def p_else_block_1(self, p): r"""else_block : ELSE block %prec PREC2""" print("""else_block : ELSE block %prec PREC2""") p[0] = Nonterminal() p[0].code = p[2].code + "\n" p[0].sym_var = p[2].sym_var self.add_ref(p[0].sym_var, self.new_scope_name()) self.symbol_table_list.append(p[0].sym_var)
def p_elseifs_1(self, p): r"""elseifs : elseifs elseif %prec PREC1""" print("""elseifs : elseifs elseif %prec PREC1""") p[0] = Nonterminal() elseif_label = self.new_label() self.code_generator.back_patch_false(p[1], elseif_label) p[0].code = p[1].code + elseif_label + ": //elseif \n" + p[2].code + "\n" p[0].sym_var = p[1].sym_var + p[2].sym_var
def p_logical_operation_1(self, p): """logical_operation : exp OR exp""" print("""logical_operation -> exp OR exp""") p[0] = Nonterminal() m = p[1].m self.backpatch(p[1].false_list, m) p[0].true_list = p[1].true_list + p[3].true_list p[0].false_list = p[3].false_list p[0].code = self.code_list print(self.code_list) self.produce_output()
def p_elseif(self, p): r"""elseif : ELSEIF LP exp RP block %prec PREC2""" print("""elseif : ELSEIF LP exp RP block %prec PREC2""") p[0] = Nonterminal() truelabel = self.new_label() self.code_generator.back_patch_true(p[3], truelabel) p[0].code = p[3].code + truelabel + ": //elseif expression\n" + p[5].code + "goto " + self.NEXT_LABEL + "; // next label\n\n" p[0].sym_var = p[5].sym_var self.add_ref(p[0].sym_var, self.new_scope_name()) self.symbol_table_list.append(p[0].sym_var)
def p_unary_operation_2(self, p): """unary_operation : NOT exp""" print("""unary_operation -> NOT exp""") p[0] = Nonterminal() p[0].place = self.new_temp() arg1 = p[1] arg2 = p[2].value p[0].code = "L" + str(len( self.code_list)) + ": " + p[0].place + "=" + arg1 + arg2 + ";" self.code_list.append(p[0].code) print(self.code_list) self.produce_output()
def p_func_body(self, p): """func_body : ID LP formal_arguments RP block""" print("""func_body -> ID LP formal_arguments RP block""") p[0] = Nonterminal() return_phrase = "goto end;\n\n" if p[1] == "_main" else CodeGenerator.popReturnAddr(self) + "goto *returnAddress; // return from function\n\n" p[0].code = "//function body ----------- \n" + p[1] + ": //function decleration\n\n" + p[3].code + "\n // function body:\n" + p[5].code + "\n// function ended\n" + return_phrase # self.variable_decs = "" print(p[0].code) p[0].func_name = p[1] p[0].sym_var = p[5].sym_var self.add_ref(p[0].sym_var, self.new_scope_name()) #save a scope self.symbol_table_list.append(p[0].sym_var)
def p_function_call_1(self, p): """function_call : lvalue1 function_call_body""" print("""function_call -> lvalue1 function_call_body""") p[0] = Nonterminal() label = self.new_label() p[0].args = p[2].args if self.t_counter > -1 : for i in range(0, self.t_counter): self.FuncVARIABLES.append("TT"+str(i)) p[0].code = CodeGenerator.storeVariables(self, self.FuncVARIABLES) + CodeGenerator.pushAddress(self,label) p[0].code += "// store arguments\n" + p[2].code + CodeGenerator.storeArgs(self,p[2].args)+ "\n goto " + p[1].get_value() + ";\n" p[0].place = self.new_temp() p[0].code += "// return label:\n" + label + ":\n" + "// load return value\n" + CodeGenerator.popVariable(self,p[0].place) +"\n //varaiables:" + CodeGenerator.loadVariables(self,self.FuncVARIABLES) print(p[0].code) print("-------------function call--------------")
def p_elseif(self, p): """elseif : ELSEIF LP exp RP block %prec ELSEIF""" print("""elseif -> ELSEIF LP exp RP block""") # print("true_list") # print(p[3].true_list) # print("false_list") # print(p[3].false_list) # print("code block") # print(p[5].code) p[0] = Nonterminal() p[0].code = p[5].code p[0].list = [] p[0].list.append(p[3].true_list) p[0].list.append(p[3].false_list)
def p_func_dec_2(self, p): """func_dec : STATIC VOID func_body""" print("""func_dec -> STATIC VOID func_body""") p[0] = Nonterminal() p[0].code = p[3].code p[0].sym_var = p[3].sym_var dic = {} dic["ref"] = "NONE" dic["name"] = p[3].func_name dic["type"] = "FUNCTION" dic["v_type"] = "NONE" dic["size"] = "NONE" dic["address"] = "NONE" dic["return_type"] = p[2] p[0].sym_var.append(dic)
def p_while(self, p): """while : WHILE LP exp RP block""" print("""while -> WHILE LP exp RP block""") p[0] = Nonterminal() p[0].sym_var = p[5].sym_var self.add_ref(p[0].sym_var, self.new_scope_name()) self.symbol_table_list.append(p[0].sym_var) begin = self.new_label() code_begin = self.new_label() after = self.new_label() self.code_generator.back_patch_false(p[3], after) self.code_generator.back_patch_true(p[3], code_begin) p[0].code = begin + ": // while begin\n\n" + p[3].code + code_begin + ": // while code begin\n" + p[5].code + "goto " + begin + "; //back to while begin\n\n" + after + ": // end of while\n\n" print("*******************************************************************************") print(p[0].code) print("*******************************************************************************")
def p_for(self, p): """for : FOR LP ID IN exp TO exp STEPS exp RP block""" print("""for -> FOR LP ID IN exp TO exp STEPS exp RP block""") p[0] = Nonterminal() p[0].sym_var = p[11].sym_var self.add_ref(p[0].sym_var, self.new_scope_name()) self.symbol_table_list.append(p[0].sym_var) begin = self.new_label() code_begin = self.new_label() after = self.new_label() initialization = "double " + p[3] + ";\n" + p[3] + " = " + p[5].get_value() + "; // FOR initialization\n" check_bundry = "if ( " + p[3] + " < " + p[7].get_value() + " ) goto " + code_begin + "; // FOR check\n" check_bundry += "goto " + after + ";\n\n" iteration = p[3] + " = " + p[3] + " + " + p[9].get_value() + "; // FOR iteration\n" p[0].code = "// FOR BEGIN\n\n" + p[5].code + p[9].code + initialization + begin + ": // for begin\n\n" + p[7].code + check_bundry + code_begin + ": // for code begin\n" + p[11].code + iteration + "goto " + begin + "; //back to for begin\n\n" + after + ": // end of for\n\n" print("*******************************************************************************") print(p[0].code) print("*******************************************************************************")
def p_statement_var_dec(self, p): """statement_var_dec : return_type var_list SEMICOLON""" print("""statement_var_dec -> return_type var_list SEMICOLON""") p[0] = Nonterminal() self.variable_decs += "\n".join([p[1].rtype + " " + var + ";\n" for var in p[2].vars]) self.VARIABLES += p[2].vars p[0].code = "\n" + p[2].code print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^") for i in range(0, len(p[2].vars)): dic = {} dic["ref"]="NONE" dic["name"] = p[2].vars[i] dic["type"] = "VARIABLE" dic["v_type"] = p[0].sym_rtype if dic["v_type"] == "int": dic["size"] = 4 elif dic["v_type"] == "real": dic["size"] = 8 elif dic["v_type"] == "bool": dic["size"] = 1 elif dic["v_type"] == "string": dic["size"] = 6 else: dic["size"] = 0 if i == 0 : dic["address"] = "NONE" else: dic["address"] = "NONE" dic["return_type"] = "NONE" p[0].sym_var.append(dic) print(p[0].sym_var) print("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^")
def p_exp_10(self, p): """exp : unary_operation""" print("""exp -> unary_operation""") p[0] = Nonterminal() p[0].code = p[1].code p[0].place = p[1].get_value()
def p_exp_9(self, p): """exp : bitwise_operation %prec BITOP""" print("""exp -> bitwise_operation""") p[0] = Nonterminal() p[0].code = "NOT YET BITWISE"
def p_exp_8(self, p): """exp : comparison_operation %prec COMOP""" print("""exp -> comparison_operation""") p[0] = Nonterminal() p[0].code = p[1].code
def p_exp_7(self, p): """exp : logical_operation""" print("""exp -> logical_operation""") p[0] = Nonterminal() p[0].code = p[1].code p[0].place = p[1].get_value()