Skip to content

Commit

Permalink
Fixed all the issues that we had during the presentation
Browse files Browse the repository at this point in the history
  • Loading branch information
onajafi committed May 31, 2018
1 parent 549eddc commit 6e9cd82
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 73 deletions.
29 changes: 23 additions & 6 deletions LL1_Parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
CODE_GENERATE_DBG = True

USING_PanicMode = True

GIVE_INITIAL_VALS_TO_VARS = True

data_memory_iterator = 2000 #The start of the dynamic memory is here
Expand Down Expand Up @@ -208,6 +207,8 @@ def findaddr(self,input_ID,func_scop_index):

calling_func_idx=-1

func_input_arg_CNT = 0

#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))
Expand Down Expand Up @@ -250,7 +251,6 @@ def codegen(self,action):# Generate the final code and save in PB[]
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])

if(ID_tuple==None):#ERROR semantic
print "Undefined identifier: " + self.SS[-1] + "\nAborted parsing..."
self.abort = True
Expand All @@ -274,6 +274,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "can't assign two different type: BOOL and INT"
print "Aborted parsing..."
self.abort=True
return
elif(action=="#addIDToSymTable"):# Add an entry to the symbol table
if(self.in_func_scope):
FUNCscop_list[self.FUNscope_index].add_ID(self.SS[-2],self.SS[-1])
Expand Down Expand Up @@ -317,6 +318,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
else:# ERROR semantic
print "The extension " + self.SS[-1] + " was not found\nAborted parsing..."
self.abort = True
return

else:
CLSscop_list.append(CLSscop(self.SS[-2]))
Expand Down Expand Up @@ -407,9 +409,11 @@ def codegen(self,action):# Generate the final code and save in PB[]
+ " scope, named: " + self.SS[-1]
print "Aborted parsing..."
self.abort = True
return
else:#ERROR semantic
print "There is no defined class named " + self.SS[-2] + "\nAborted parsing..."
self.abort = True
return
elif(action=="#MULT"):
if(self.SS[-2] != "BOOL" and self.SS[-4] != "BOOL"):
temp_WORD = alloc_4byte()
Expand All @@ -425,6 +429,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "Can't multiply booleans !!!"
print "Aborted parsing..."
self.abort=True
return
elif(action=="#ADD"):
if (self.SS[-2] != "BOOL" and self.SS[-4] != "BOOL"):
temp_WORD = alloc_4byte()
Expand All @@ -440,6 +445,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "Can't do addition on booleans !!!"
print "Aborted parsing..."
self.abort = True
return
elif(action=="#SUB"):
if (self.SS[-2] != "BOOL" and self.SS[-4] != "BOOL"):
temp_WORD = alloc_4byte()
Expand All @@ -455,6 +461,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "Can't do subtraction on booleans !!!"
print "Aborted parsing..."
self.abort = True
return
elif(action=="#LT"):
if (self.SS[-2] != "BOOL" and self.SS[-4] != "BOOL"):
temp_WORD = alloc_4byte()
Expand All @@ -470,6 +477,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "Can't comapare booleans !!!"
print "Aborted parsing..."
self.abort = True
return
elif(action=="#EQ"):
temp_WORD = alloc_4byte()
self.PB.append(("EQ", self.SS[-3], self.SS[-1], temp_WORD))
Expand All @@ -495,6 +503,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "Can't use an integer in a logical operation"
print "Aborted parsing..."
self.abort = True
return
elif(action=="#genIf"):
self.SS.append(self.PC)# Saving the address space for JPF command
self.PB.append(None)# We will fill this in #genElse
Expand Down Expand Up @@ -566,19 +575,22 @@ def codegen(self,action):# Generate the final code and save in PB[]
# Cleaning the Code Block to put the new function codes in it; will be saved later in endFunc
self.PB = []
self.PC = 0
self.func_input_arg_CNT = 0
elif(action=="#initFuncParam"):
# generate the parameter reading code:
FUNCscop_list[self.FUNscope_index].add_param_ID(self.SS[-2],self.SS[-1])
self.gen_pop_stack(FUNCscop_list[self.FUNscope_index].give_ID_elem(self.SS[-1])[2])#Search by name, then genarate code to read from stack
self.SS.pop()
self.SS.pop()
self.func_input_arg_CNT = self.func_input_arg_CNT + 1

elif(action=="#endFunc"):
#Check if return type is same with the function output type:
if((self.SS[-2] in ("BOOL","INT")) and FUNCscop_list[self.FUNscope_index].return_type != self.SS[-2]):#ERROR semantic
print "In " + FUNCscop_list[self.FUNscope_index].name + " the return type is diffrent with the specified one"
print "Aborted parsing..."
self.abort = True
return
self.gen_push_stack(self.SS[-1])#Saving the return value of the function
self.SS.pop()#The return val address
self.SS.pop()#The return val type
Expand All @@ -604,13 +616,18 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "There is no function defenition in class " + self.SS[-2] + " named " + self.SS[-1]
print "Aborted parsing..."
self.abort = True
return
else: # ERROR semantic
print "There is no defined class called: " + self.SS[-2]
print "Aborted parsing..."
self.abort = True
return
elif(action == "#pFuncArgs"):
pass
self.func_input_arg_CNT = self.func_input_arg_CNT - 1
elif(action=="#endCallFUNC"):
if(self.func_input_arg_CNT != 0):
print "Wrong number of inputs for function ",FUNCscop_list[self.calling_func_idx].name
self.abort = True
# Saving the return place after calling the function
self.gen_push_stack(("^", self.PC + 2*len(FUNCscop_list[self.calling_func_idx].param_id_elems) + 3)) # '^' means to add the start of the current function block address to the next param

Expand All @@ -621,7 +638,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
print "Input argument for function " + FUNCscop_list[self.calling_func_idx].name + " is different"
print "Aborted parsing..."
self.abort = True
break
return
else: # Generate a code to put the value of the address in the stack
self.gen_push_stack(self.SS[-1])
self.SS.pop()
Expand All @@ -638,7 +655,7 @@ def codegen(self,action):# Generate the final code and save in PB[]
#First we will save the return value
temp_ret_val = alloc_4byte()
self.gen_pop_stack(temp_ret_val)
self.SS.append(None) # Don't know the return type
self.SS.append(FUNCscop_list[self.calling_func_idx].return_type) # Don't know the return type
self.SS.append(temp_ret_val)

#Second there is a an address which we don't need it anymore
Expand Down Expand Up @@ -703,7 +720,7 @@ def get_token(self,token):
print "Poping: " + self.stack.pop()
else:
break
else:# ERROR we the LL(1) table is empty :(
else:# ERROR the LL(1) table is empty :(
self.Error_CNT = self.Error_CNT + 1
print "\nERROR LL(1) table is empty..."
print "The table Entry is:", (self.stack[-1],input_term)
Expand Down
5 changes: 3 additions & 2 deletions TokenScanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ def getChar(self,inputChar):
if(self.we_already_have_a_char):# There is a char left for process!
self.we_already_have_a_char = False # We don't want to get in a loop!
self.getChar(self.lastChar)
if(inputChar=='$'):
return ("ERROR","Wronginput: "+inputChar)
# print "state is: " + str(self.state),self.tempStr
if(self.state==0):
if(inputChar=='/'):#Check for comments
Expand All @@ -83,8 +85,6 @@ def getChar(self,inputChar):
self.state = 4
elif (inputChar == "" or inputChar == None):
return ("STOP","END OF FILE")
else:
self.tempStr = ""

elif(self.state==1):#ID
if (inputChar == '/'): # Check for comments
Expand All @@ -97,6 +97,7 @@ def getChar(self,inputChar):
else:
return self.constructToken(inputChar)


elif(self.state==2):#Digit
if (inputChar == '/'): # Check for comments
self.last_state = self.state
Expand Down
56 changes: 36 additions & 20 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,51 @@

f = open("testFile.txt", "r") #opens file with name of "test.txt"

goodbye=False

while True:
while not goodbye:
outprint = TScan.getChar(f.read(1))
if outprint != None:
#print outprint
my_parser.get_token(outprint)
if(outprint[1]=='EOF' or outprint[0]=="STOP"):
break
if(outprint[0]=="ERROR"):
print "ERROR: " + outprint[1]
goodbye = True

f.close()
print "\nThe output will be like:"
for line,command in enumerate(my_parser.PB):
print line, command


output_file = open("output.txt","w")
for line,command in enumerate(my_parser.PB):
output_file.write(str(line) + "\t" + '(')
if (command[0] != None):
output_file.write(str(command[0])+ ', ')
if (command[1] != None):
output_file.write(str(command[1]) + ', ')
if (command[2] != None):
output_file.write(str(command[2]) + ', ')
if (command[3] != None):
output_file.write(str(command[3]))
output_file.write(')\n')

output_file.close()
if(not goodbye):
print "\nThe output will be like:"
for line,command in enumerate(my_parser.PB):
print line, command


output_file = open("output.txt","w")
for line,command in enumerate(my_parser.PB):
output_file.write(str(line) + "\t" + '(')
if (command[0] != None):
output_file.write(str(command[0])+ ', ')
if (command[1] != None):
output_file.write(str(command[1]) + ', ')
if (command[2] != None):
output_file.write(str(command[2]) + ', ')
if (command[3] != None):
output_file.write(str(command[3]))
output_file.write(')\n')

output_file.close()

print "Done"
if(my_parser.abort or my_parser.Error_CNT):
if(my_parser.abort):
print "But with error(s) and " + str(my_parser.Error_CNT) + " warning(s)"
else:
print str(my_parser.Error_CNT) + " warning(s)"
else:
print "With no errors :)"



# tempset = {("omg","yay"):["Hi","iiii"],"holymoly":"funny","idfdf":"dead end"}
#
Expand Down
64 changes: 19 additions & 45 deletions testFile.txt
Original file line number Diff line number Diff line change
@@ -1,59 +1,33 @@

class Math{
static int foo;
static int OMG;
static boolean d;

public static int Mult(int a,int b){
int temp;
int output;
output = 0;
for(temp = 0; temp < a ;temp += 1){
output = output + b;
}
return output;
}
}

class myClass extends Math{
class myClass{

static int foo;
static int temp;
public static int fibonacci(int input)
{
int i;
int t1;
int t2;
i = 0;
if(input < 1)
{
i = 1;
}
else
{
if(input == 1)
{
i = 1;
}
else
{
t1= myClass.fibonacci(input - 1);
t2 = myClass.fibonacci(input - 2);
i = t1 + t2;
}
}
return i;



public static int test1(int i){


return 10;
}

}

public class MainCls {
public static void main(){
//Here is how we call the function:
int i;
i = i +1000003;
System.out.println(myClass.fibonacci(10));
System.out.println(i);

int $a;
boolean b;
//b=myClass.isPrime(31);

//Here is how we call & show the function:
//System.out.println(myClass.fibonacci(10));

//The output of isPrime
//System.out.println(b);
a=myClass.test1(9);
}
}

Expand Down

0 comments on commit 6e9cd82

Please sign in to comment.