def p_paramdec_1(self, p): """paramdec : ID COLON type""" p[0] = NonTerminal() p[0].place = p[1].place p[0].code = p[1] + p[2] + p[3].value print(p[0].code) pass
def p_exp_idassign1(self, p): """exp : ID ASSIGN exp""" p[0] = NonTerminal() p[0].value = p[1] p[0].code = p[3].code + p[1] + " " + p[2] + " " + str( p[3].get_value()) + ";\n" pass
def p_program(self, p): """program : declist MAIN LRB RRB block""" p[0] = NonTerminal() p[0].code = "int " + p[2] + " " + p[3] + p[4] + "{\n" + p[1].code + p[ 5].code + "}" self.file.write(p[0].code) pass
def p_relopexp_extended6(self, p): """relopexp : relopexp GE exp""" p[0] = NonTerminal() p[0].isRel = True p[0].place = self.new_temp() p[0].trueVal = p[0].place + " = " + str(1) p[0].falseVal = p[0].place + " = " + str(0) p[0].lastExp = str(p[3].get_value()) first = self.new_label() sec = self.new_label() third = self.new_label() fourth = self.new_label() fifth = self.new_label() after = self.new_label() x = self.new_label() p[0].code = "int " + p[0].place + ";\n" p[0].code += p[1].code + p[3].code + "if (" + str( p[1].get_value()) + " " + p[2] + " " + str( p[3].get_value()) + "){\n\tgoto " + fourth + ";\n}\n" p[0].code += sec + ": ;\n" + p[0].falseVal + ";\n" p[0].code += third + ": ;\n" + "goto " + after + ";\n" p[0].code += fourth + ": ;\n" + p[0].trueVal + ";\n" # p[0].code += p[0].place + " = " + str(p[1].get_value()) + " && " + str(p[0].get_value()) + ";\n" p[0].code += after + ": ;\n" self.labelCount -= 1
def p_paramdecslist_extended(self, p): """paramdecslist : paramdecslist COMMA paramdec""" p[0] = NonTerminal() p[0].place = self.new_temp() p[0].code = p[1].code + p[2] + p[3].code print(p[0].code) pass
def p_iddec_idlsbrsb(self, p): """iddec : ID LSB exp RSB""" p[0] = NonTerminal() p[0].code = "int " + p[ 1] + " = " + "arr_pointer;\n" + "arr_pointer += " + str( p[3].get_value()) # self.arr_last += p[3].get_value() pass
def p_exp_expsub(self, p): """exp : SUB exp""" p[0] = NonTerminal() p[0].place = self.new_temp() p[0].code = "int " + p[0].place + ";\n" p[0].code += p[2].code + p[0].place + " = " + p[1] + str( p[2].get_value()) + ";\n" pass
def p_exp_mod(self, p): """exp : exp MOD exp""" p[0] = NonTerminal() p[0].place = self.new_temp() p[0].code = p[1].code + p[3].code + "int " + p[0].place + " = " p[0].code += str(p[1].get_value()) + " " + p[2] + " " + str( p[3].get_value()) + ";\n" pass
def p_stmt_if2(self, p): """stmt : IF LRB exp RRB stmt elseiflist ELSE stmt %prec IF""" p[0] = NonTerminal() # p[0].begin = self.new_label() pElse = self.new_label() p[0].after = self.new_label() if p[6].code != "": p[0].code = p[3].code + "if (" + str(p[3].get_value( )) + " == 0" + "){\ngoto " + pElse + ";\n}\n" + p[5].code p[0].code += "goto " + p[0].after + ";\n" p[0].code += pElse + ": ;\n" + p[6].code p[0].code += p[0].after + ": ;\n" else: p[0].code = p[3].code + "if (" + str(p[3].get_value( )) + " == 0" + "){\ngoto " + pElse + ";\n}\n" + p[5].code p[0].code += "goto " + p[0].after + ";\n" p[0].code += pElse + ": ;\n" + p[8].code p[0].code += p[0].after + ": ;\n"
def p_stmt_on(self, p): """stmt : ON LRB exp RRB LCB cases RCB SEMICOLON""" p[0] = NonTerminal() p[0].code = "" for key in self.value_label: p[0].code += "if (" + str(p[3].get_value()) + " == " + str( key) + "){\ngoto " + self.value_label[key] + ";\n}\n" p[0].code += p[6].code p[0].code += "Last: ;\n"
def p_case(self, p): """case : WHERE exp COLON stmtlist""" p[0] = NonTerminal() label = self.new_label() p[0].switchValue = str(p[2].get_value()) p[0].switchLabel = label self.value_label[str(p[2].get_value())] = label p[0].code = label + ": ;\n{" + p[4].code + "goto Last;\n}\n"
def p_exp_idassign2(self, p): """exp : ID LSB exp RSB ASSIGN exp""" p[0] = NonTerminal() # p[0].isArray = True p[0].value = "array[" + p[1] + " + " + str(p[3].get_value()) + "]" p[0].code = p[6].code + "array[" + p[1] + " + " + str( p[3].get_value()) + "] " + p[5] + " " + str( p[6].get_value()) + ";\n" pass
def p_exp_or(self, p): """exp : exp OR exp""" p[0] = NonTerminal() p[0].place = self.new_temp() p[0].code = "int " + p[0].place + ";\n" p[0].code += p[1].code + p[3].code + p[0].place + " = " + str( p[1].get_value()) + " || " + str(p[3].get_value()) + ";\n" pass
def p_exp_expnot(self, p): """exp : NOT exp""" p[0] = NonTerminal() p[0].place = self.new_temp() # p[0].true_list = p[2].false_list # p[0].false_list = p[2].true_list p[0].code = p[2].code + p[0].place + " = " + "!" + str( p[2].get_value()) + ";\n" pass
def p_stmt_while(self, p): """stmt : WHILE LRB exp RRB stmt""" p[0] = NonTerminal() p[0].begin = self.new_label() p[0].after = self.new_label() p[0].code = p[0].begin + ": ;\n" + p[3].code + "if (" + str( p[3].get_value()) + " == 0" + "){\ngoto " + p[0].after + ";\n}\n" p[0].code += p[5].code + "\n" if p[3].isRel: p[0].code += "goto " + "L" + str(self.labelCount - 6) + ";\n" else: p[0].code += "goto " + p[0].begin + ";\n" p[0].code += p[0].after + ": ;\n"
def p_stmt_for1(self, p): """stmt : FOR LRB exp SEMICOLON exp SEMICOLON exp RRB stmt""" p[0] = NonTerminal() p[0].begin = self.new_label() # pMiddle = self.new_label() p[0].after = self.new_label() # s = p[5].code.split("\n") # pAgain = s[1].split(":") p[0].code = p[3].code + p[0].begin + ": ;\n" + p[5].code + "if (" + str( p[5].get_value()) + " == 0" + "){\ngoto " + p[0].after + ";\n}\n" p[0].code += p[9].code p[0].code += p[7].code + "\ngoto " + p[0].begin + ";\n" p[0].code += p[0].after + ": ;\n"
def p_stmt_for2(self, p): """stmt : FOR LRB ID IN ID RRB stmt""" p[0] = NonTerminal() p[0].begin = self.new_label() # pMiddle = self.new_label() p[0].after = self.new_label() counter = self.new_temp() p[0].code = "int " + counter + " = 0;\n" p[0].code += p[0].begin + ": ;\n" + p[ 3] + " = array[" + counter + "];\nif (" + counter + " == arr_pointer){\ngoto " + p[ 0].after + ";\n}\n" # p[0].code += "goto " + p[0].after + ";\n" p[0].code += p[7].code + counter + " += 1;\ngoto " + "L" + str( self.labelCount - 2) + ";\n" p[0].code += p[0].after + ": ;\n"
def p_explist_extendec(self, p): """explist : explist COMMA exp""" p[0] = NonTerminal() p[0].code = p[1].code + p[2] + " " + p[3].code
def p_explist_exp(self, p): """explist : exp""" p[0] = NonTerminal() p[0].code = p[1].code pass
def p_iddec_idassignexp(self, p): """iddec : ID ASSIGN exp""" p[0] = NonTerminal() p[0].code = p[3].code + "int " + p[1] + " " + p[2] + " " + str( p[3].get_value()) pass
def p_exp_lrbexprrb(self, p): """exp : LRB exp RRB""" p[0] = NonTerminal() p[0].place = p[2].place p[0].code = p[2].code pass
def p_exp_idlsbrsb(self, p): """exp : ID LSB exp RSB""" p[0] = NonTerminal() p[0].value = "array[" + p[1] + " + " + str(p[3].get_value()) + "]" p[0].code = p[3].code pass
def p_exp_relop(self, p): """exp : relopexp %prec EXP""" p[0] = NonTerminal() p[0].code = p[1].code p[0].place = p[1].place
def p_iddec_id(self, p): """iddec : ID""" p[0] = NonTerminal() p[0].code = "int " + p[1] pass
def p_declist_epsilon(self, p): """declist : """ p[0] = NonTerminal() p[0].code = "" pass
def p_dec_vardec(self, p): """dec : vardec""" p[0] = NonTerminal() p[0].code = p[1].code pass
def p_idlist_iddec(self, p): """idlist : iddec""" p[0] = NonTerminal() p[0].code = p[1].code pass
def p_exp_idexplist(self, p): """exp : ID LRB explist RRB""" p[0] = NonTerminal() p[0].code = p[1] + p[2] + p[3].code + p[4] pass
def p_idlist_comma(self, p): """idlist : idlist COMMA iddec""" p[0] = NonTerminal() p[0].code = p[1].code + ";\n" + p[3].code pass
def p_dec_funcdec(self, p): """dec : funcdec""" p[0] = NonTerminal() p[0].code = p[1].code pass