diff --git a/Grammer.py b/Grammer.py index cde9762..5e47786 100644 --- a/Grammer.py +++ b/Grammer.py @@ -232,9 +232,9 @@ ("H_PRIME",'('):["#callFUNC",'(',"Arguments",')',"#endCallFUNC"], ("Parameters",')'):['epsilon'], - ("Parameter",')'):['epsilon'], + ("Parameter",')'):["#initFuncParam"], ("Arguments",')'):['epsilon'], - ("Argument",')'):['epsilon'], + ("Argument",')'):["#pFuncArgs"], ("C_PRIME",')'):['epsilon'], ("D_PRIME",')'):['epsilon'], ("E_PRIME",')'):['epsilon'], @@ -251,7 +251,7 @@ ("G_PRIME",'+'):["#insIDadd"], ("H_PRIME",'+'):["#pCLS_ID"],#OK - ("Parameter",','):[',',"Type","Identifier","#insIDadd","Parameter"], + ("Parameter",','):["#initFuncParam",',',"Type","Identifier","Parameter"], ("Argument",','):[',',"GenExpression","Argument"], ("C_PRIME",','):['epsilon'], ("D_PRIME",','):['epsilon'], @@ -344,12 +344,12 @@ ("VarDeclarations",'boolean'):["VarDeclaration","VarDeclarations"], ("VarDeclaration",'boolean'):["Type","Identifier","#addIDToSymTable",';'],#OK ! - ("Parameters",'boolean'):["Type","Identifier","#insIDadd","Parameter"],#OK + ("Parameters",'boolean'):["Type","Identifier","Parameter"],#OK ("Type",'boolean'):["#BOOL",'boolean'], ("VarDeclarations", 'int'): ["VarDeclaration", "VarDeclarations"], ("VarDeclaration", 'int'): ["Type","Identifier","#addIDToSymTable", ';'],#OK ! - ("Parameters", 'int'): ["Type", "Identifier","#insIDadd", "Parameter"],#OK + ("Parameters", 'int'): ["Type", "Identifier", "Parameter"],#OK ("Type",'int'):["#INT",'int'], ("VarDeclarations",'if'):['epsilon'], diff --git a/LL1_Parser.py b/LL1_Parser.py index 5771bb5..571d89a 100644 --- a/LL1_Parser.py +++ b/LL1_Parser.py @@ -21,8 +21,8 @@ "#pBOOLconst", "#SAVE_PC","#genWhile","#endWhile", "#genFor","#endFor", - "#genFunc","#endFunc", - "#callFUNC","#endCallFUNC" + "#genFunc","#endFunc","#initFuncParam", + "#callFUNC","#endCallFUNC","#pFuncArgs" } @@ -119,10 +119,9 @@ class FUNCscop(): def __init__(self, name, CLS_scop_index): self.pointer = [] #To point to other scopes by there INDEX in CLSscop_list! (python has no pointers!!!) - self.id_elems = [] #The Identifiers + self.id_elems = [] #The Identifiers format: (name,Type,address) self.code_block = [] #Contains the output code :D - self.param_inputs_CNT = 0 - self.vars_CNT = 0 + self.param_id_elems = [] self.return_type = None self.name=name self.address=0 @@ -132,6 +131,20 @@ def __init__(self, name, CLS_scop_index): else: self.is_main = True + #Adding the argument IDs + def add_param_ID(self,type,id_name): + if (type == "int"): + tempAlloc= alloc_4byte() + self.param_id_elems.append((id_name, "INT", tempAlloc)) + self.id_elems.append((id_name, "INT", tempAlloc)) + elif (type == "boolean"): + tempAlloc= alloc_4byte() + self.param_id_elems.append((id_name, "BOOL", tempAlloc)) + self.id_elems.append((id_name, "BOOL", tempAlloc)) + else: # ERROR + print "Undefined Type " + type + + #Adding other IDs def add_ID(self,type,id_name):# INT|BOOLEAN if(type=="int"): self.id_elems.append((id_name,"INT",alloc_4byte())) @@ -154,14 +167,6 @@ def give_ID_elem(self,id_name):# Find an ID return output - # def give_FUNC_elem(self,func_name):# Find a FUNC - # output = None - # # We don't have a function in a function scope! - # for index in self.pointer: - # output = CLSscop_list[index].give_FUNC_elem(func_name) - # if(output != None): - # break - def token_to_terminal(token): if(token[0]=='Key'): @@ -199,6 +204,8 @@ def findaddr(self,input_ID,func_scop_index): PC=0 SS=[]# Semantic Stack + calling_func_idx=-1 + #These codes have to be called at the start of the program (start of main CLS) def gen_initial_code(self): self.PB.append(("ASSIGN",'#' + str(stack_memory_start),stack_pointer,None)) @@ -238,7 +245,6 @@ def codegen(self,action):# Generate the final code and save in PB[] self.SS.append(self.current_token[1]) elif(action=="#insIDadd"):# Put the address of an Id in the scope instead of there name if (self.in_func_scope): - print "FLAG4:",self.SS[-1] ID_tuple=FUNCscop_list[self.FUNscope_index].give_ID_elem(self.SS[-1]) else: ID_tuple=CLSscop_list[self.CLSscope_index].give_ID_elem(self.SS[-1]) @@ -248,8 +254,8 @@ def codegen(self,action):# Generate the final code and save in PB[] self.abort = True return self.SS.pop() - self.SS.append(ID_tuple[1])# Here we put the address - self.SS.append(ID_tuple[2])# Here we put the address + self.SS.append(ID_tuple[1])#Here we put the type + self.SS.append(ID_tuple[2])#Here we put the address elif(action=="#pconst"): self.SS.append("INT") self.SS.append("#" + str(self.current_token[1])) @@ -320,17 +326,16 @@ def codegen(self,action):# Generate the final code and save in PB[] CodeLength=len(self.PB) Exit_comm_index = CodeLength - 1 - start_list = [0] + for i in range(0,len(FUNCscop_list)): FUNCscop_list[i].address = CodeLength self.PB.extend(FUNCscop_list[i].code_block) CodeLength=len(self.PB) - start_list.append(CodeLength) print "Code length:",CodeLength i = 0 - while (i < start_list[1]): + while (i <= Exit_comm_index): if (isinstance(self.PB[i][1], tuple)): if (self.PB[i][1][0] == '^'): self.PB[i] = ( @@ -355,8 +360,13 @@ def codegen(self,action):# Generate the final code and save in PB[] self.PB[i][3]) i = i + 1 - for idx in range(1,len(FUNCscop_list)): - while(i