Пример #1
0
 def semantic_action_24(self):
     left_offset = self.attribute_stack.get_top(4)["offset"]
     left_addr = self.attribute_stack.get_top(4)["addr"]
     E_addr = self.attribute_stack.get_top(2)["addr"]
     Left = self.attribute_stack.get_top(4)
     E = self.attribute_stack.get_top(2)
     if self.attribute_stack.get_top(4)["type"]=="struct":
         raise MyException("赋值语句类型错误")
     if not (Left["type"]=="boolean" and E["type"]=="boolean" or 
             Left["type"] in self.legal_type and E["type"] in self.legal_type):
             raise MyException("赋值类型不匹配")
     #类型转换
     left_type_prioity = self.legal_type[Left["type"]]
     E_type_prioity = self.legal_type[E["type"]]
     #如果Left的类型比E宽
     if left_type_prioity>E_type_prioity:
         temp = self.widen(E["addr"],E["type"],Left["type"])
         E["addr"] = temp
     elif left_type_prioity<E_type_prioity:
         raise MyException("赋值语句不能向宽类型转换")
     if left_offset == None:
         self.gencode("=",str(E_addr),None,str(left_addr))
     else:
         self.gencode("=",str(E_addr),None,str(left_addr)+"["+str(left_offset)+"]")
     return {}
Пример #2
0
 def semantic_action_35(self):
     Y_attribute = {}
     Y_attribute["addr"] = self.new_temp()
     Y1 = self.attribute_stack.get_top(3)
     F = self.attribute_stack.get_top(1)
     if F["type"] not in self.legal_type:
         raise MyException("算数类型不对")
     if Y1["type"] not in self.legal_type:
         raise MyException("算数类型不对")
     Y_attribute["type"] = self.max_type(Y1["type"],F["type"])
     a1 = self.widen(Y1["addr"],Y1["type"],Y_attribute["type"])
     a2 = self.widen(F["addr"],F["type"],Y_attribute["type"])
     self.gencode("/",a1,a2,Y_attribute["addr"])
     return Y_attribute
Пример #3
0
 def semantic_action_32(self):
     E_attribute = {}
     E_attribute["addr"] = self.new_temp()
     E1 = self.attribute_stack.get_top(3)
     Y = self.attribute_stack.get_top(1)
     if E1["type"] not in self.legal_type:
         raise MyException("算数类型不对")
     if Y["type"] not in self.legal_type:
         raise MyException("算数类型不对")
     E_attribute["type"] = self.max_type(E1["type"],Y["type"])
     a1 = self.widen(E1["addr"],E1["type"],E_attribute["type"])
     a2 = self.widen(Y["addr"],Y["type"],E_attribute["type"])
     self.gencode("-",a1,a2,E_attribute["addr"])
     return E_attribute
Пример #4
0
 def semantic_action_26(self):
     left_attribute = {}
     t = self.table_stack.get_top(1)
     id_addr = self.attribute_stack.get_top(1)["addr"]
     item = t.search(id_addr)
     if item==None:
         raise MyException(id_addr+"未定义") 
     if item.type=="fuction" or item.type == "array":
         raise MyException(id_addr+"类型不匹配")
     left_attribute["type"] = item.type
     left_attribute["addr"] = id_addr
     left_attribute["offset"] = None
     left_attribute["table"] = item.redundant_point
     return left_attribute
Пример #5
0
 def semantic_action_67(self):
     id = self.attribute_stack.get_top(1)
     t = self.table_stack.get_top(1)
     if t.search(id["addr"])==None:
         raise MyException(id["addr"]+"未定义") 
     self.param_queue.enqueue(id["addr"])
     return {}
Пример #6
0
 def semantic_action_28(self):
     left_attribute = {}
     id_name = self.attribute_stack.get_top(1)["addr"]
     Left1 = self.attribute_stack.get_top(3)
     t = Left1["table"]
     item = t.search(id_name)
     if item == None:
         raise MyException(id_name+"未定义")
     if Left1["type"] != "struct":
         raise MyException("不能对非结构体进行.操作")
     if item.type == "struct":
         left_attribute["table"] = item.redundant_point
     left_attribute["type"] = item.type
     left_attribute["addr"] = id_name +"."+self.attribute_stack.get_top(3)["addr"]
     left_attribute["offset"] = self.attribute_stack.get_top(3)["offset"]
     return left_attribute
Пример #7
0
 def max_type(self,type1,type2):
     legal_type = self.legal_type
     if type1 not in legal_type or type2 not in legal_type:
         raise MyException("算数运算类型不正确")
     type1_prioity = legal_type[type1]
     type2_prioity = legal_type[type2]
     if type1_prioity>type2_prioity:
         return type1
     else:
         return type2
Пример #8
0
 def semantic_action_5(self):
     Param = self.attribute_stack.get_top(6)
     offset = self.offset_stack.get_top()
     t = self.table_stack.get_top()
     t.set_offset(offset)
     name = self.attribute_stack.get_top(9)["addr"]
     X = self.attribute_stack.get_top(10)
     Z = self.attribute_stack.get_top(2)
     if t.duplicate_check(name):#重复定义,程序报错
         raise MyException(name+"重复定义")
     if X["type"] != Z["type"]:
         raise MyException("返回类型不匹配")
     t.return_type = X["type"]
     if t.father_table.duplicate_check(name):
         raise MyException("函数重复定义")
     t.father_table.add(symbol_item(name,"fuction",None,t))
     t.param_num = Param["param_num"]
     self.table_stack.pop(1)
     self.offset_stack.pop(1)
     return {}
Пример #9
0
 def semantic_action_29(self):
     L_attribute = {}
     t = self.table_stack.get_top(1)
     id_name = self.attribute_stack.get_top(4)["addr"]
     item = t.search(id_name)
     E = self.attribute_stack.get_top(2)
     if item == None:
         raise MyException(id_name+"未定义")
     if item.type != "array":
         raise MyException(id_name+"非数组变量不能有索引") 
     if E["type"] != "int":
         raise MyException("数组索引只能用整数")
     L_attribute["array"] = self.table_stack.get_top(1).search(id_name).redundant_point
     L_attribute["addr"] = self.attribute_stack.get_top(2)["addr"]
     L_attribute["ndim"] = 0
     if len(L_attribute["array"].limits) == 1:
         L_attribute["type"] = L_attribute["array"].type
     else:
         L_attribute["type"] = "array"
     return L_attribute
Пример #10
0
 def semantic_action_39(self):
     M_attribute = {}
     Left = self.attribute_stack.get_top(1)
     if Left["type"] not in {"char","int","float","boolean"}:
         raise MyException("类型错误")
     if Left["offset"] == None:
         M_attribute["addr"] = Left["addr"]
     else:
         M_attribute["addr"] = self.new_temp()
         self.gencode("=",Left["addr"] + "[" + Left["offset"] + "]",None,M_attribute["addr"])
     M_attribute["type"] = Left["type"]
     return M_attribute
Пример #11
0
 def semantic_action_27(self):
     left_attribute = {}
     left_attribute["addr"] = self.new_temp()
     left_attribute["offset"] = self.new_temp()
     l_array = self.attribute_stack.get_top(1)["array"]
     l_addr = self.attribute_stack.get_top(1)["addr"]
     L = self.attribute_stack.get_top(1)
     if L["type"]=="array":
         raise MyException("数组索引过少")
     left_attribute["type"] = L["type"]
     self.gencode("=",str(l_array.address),None,left_attribute["addr"])
     self.gencode("*",l_addr,self.width(l_array.type),left_attribute["offset"])
     return left_attribute
Пример #12
0
 def semantic_action_25(self):
     left = self.attribute_stack.get_top(4)
     B = self.attribute_stack.get_top(2)
     if left["type"]!="boolean":
         raise MyException("赋值语句类型错误")
     if left["offset"] == None:
         result = str(left["addr"])
     else:
         result= str(left["addr"])+"["+str(left["offset"])+"]"
     self.back_patch(B["truelist"],self.next_quad)
     self.back_patch(B["falselist"],self.next_quad+2)
     self.gencode("=","true",None,result)
     self.gencode("goto",None,None,self.next_quad+2)
     self.gencode("=","false",None,result)
     return {}
Пример #13
0
 def semantic_action_30(self):
     L_attribute = {}
     t = self.new_temp()
     m = self.attribute_stack.get_top(4)["ndim"]+1
     L1 = self.attribute_stack.get_top(4)
     E = self.attribute_stack.get_top(2)
     if L1["type"]!="array":
             raise MyException("数组索引过多")
     self.gencode("*",L1["addr"],L1["array"].limits[m],t)
     self.gencode("+",t,E["addr"],t)
     L_attribute["array"] = L1["array"]
     L_attribute["addr"] = t
     L_attribute["ndim"] = m
     if m==len(L1["array"].limits)-1:
         L_attribute["type"] = L1["array"].type
     else:
         L_attribute["type"] = "array"
     return L_attribute
Пример #14
0
    def semantic_action_10(self):
        t = self.table_stack.get_top()
        id_name = self.attribute_stack.get_top(2)["addr"]
        T_type = self.attribute_stack.get_top(3)["type"]
        T_width = self.attribute_stack.get_top(3)["width"]
        offset = self.offset_stack.get_top()

        if t.duplicate_check(id_name):
            raise MyException(id_name+"重复定义")
        else:
            if "array_dope_vector" in self.attribute_stack.get_top(3):#数组或者正常变量
                redundant_point = self.attribute_stack.get_top(3)["array_dope_vector"]
                if redundant_point != None:
                    redundant_point.address = offset
            else:#结构体
                redundant_point = self.attribute_stack.get_top(3)["table"]
            t.add(symbol_item(id_name,T_type,offset,redundant_point))
        self.offset_stack.pop(1)
        self.offset_stack.push(offset+T_width)
        return {}
Пример #15
0
 def semantic_action_64(self):
     t = self.table_stack.get_top(1)
     id = self.attribute_stack.get_top(5)
     Elist = self.attribute_stack.get_top(3)
     Left = self.attribute_stack.get_top(8)
     item = t.search(id["addr"])
     n=0
     if item==None:
         raise MyException(id["addr"]+"未定义")
     table = item.redundant_point
     if item.type!="fuction":
         raise MyException("不能对非函数变量进行函数调用")
     if table.param_num != len(Elist["type_array"]):
         raise MyException("参数个数不正确")
     for i in range(table.param_num):
         if table.table[i].type != Elist["type_array"][i]:
             raise MyException("第"+str(i+1)+"个参数类型不正确")
     if not (Left["type"]=="boolean" and table.return_type=="boolean" or 
             Left["type"] in self.legal_type and table.return_type in self.legal_type):
             raise MyException("赋值类型不匹配")
     #类型转换
     left_type_prioity = self.legal_type[Left["type"]]
     E_type_prioity = self.legal_type[table.return_type]
     #如果Left的类型比E宽
     if left_type_prioity>E_type_prioity:
         temp = self.widen(table.return_type,table.return_type,Left["type"])
         table.return_type = temp
     elif left_type_prioity<E_type_prioity:
         raise MyException("赋值语句不能向宽类型转换")
     while not self.param_queue.is_empty():
         p = self.param_queue.dequeue()
         self.gencode("param",None,None,p)
         n+=1
     self.gencode("call",id["addr"],n,None)
     self.gencode("=","F",None,Left["addr"])
     return {}