def main(): global production_list, ntl, nt_list, tl, t_list firstfollow.main() print("\tFIRST AND FOLLOW OF NON-TERMINALS") for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) print(nt) print("\tFirst:\t", firstfollow.get_first(nt)) print("\tFollow:\t", firstfollow.get_follow(nt), "\n") augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] table = make_table(calc_states()) print("\tLR(0) TABLE\n") for i, j in table.items(): print(i, "\t", j) return
def main(inputFile): global production_list, ntl, nt_list, tl, t_list firstfollow.main(inputFile) print("\tFIRST AND FOLLOW OF NON-TERMINALS") for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) print(nt) print("\tFirst:\t", firstfollow.get_first(nt)) print("\tFollow:\t", firstfollow.get_follow(nt), "\n") augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] print(nt_list) print(t_list) j = calc_states() ctr = 0 for s in j: print("I{}:".format(ctr)) for i in s: print("\t", i) ctr += 1 table = make_table(j) print("\n\t---- LR(1) TABLE----") print() sr, rr = 0, 0 for i, j in table.items(): print(i, "\t", j) s, r = 0, 0 for p in j.values(): if p != 'accept' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 print("\n", sr, "s/r conflicts |", rr, "r/r conflicts") stringHandler(inputFile) return
def main(): global production_list, ntl, nt_list, tl, t_list firstfollow.main() for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] print(nt_list) print(t_list) j = calc_states() if (sys.argv[1] == 'i'): ctr = 0 st = "" for s in j: st += "Item{}:".format(ctr) for i in s: st += "\t" + i + "\n" ctr += 1 print("ST:") print(st) #st = "\n".join(st) #write_to_file = open("temp_clr_items.txt", 'w') #write_to_file.write(st) #write_to_file.close() elif (sys.argv[1] == 't'): table = make_table(j) sr, rr = 0, 0 for i, j in table.items(): s, r = 0, 0 for p in j.values(): if p != 'accept' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 print("\n", sr, "s/r conflicts |", rr, "r/r conflicts") else: print("Invalid command line argument!") return
def main(): global production_list, ntl, nt_list, tl, t_list firstfollow.main() # print("\tFIRST AND FOLLOW OF NON-TERMINALS") for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) # print(nt) # print("\tFirst:\t", firstfollow.get_first(nt)) # print("\tFollow:\t", firstfollow.get_follow(nt), "\n") augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] table = make_table(calc_states()) print("\n======== TABLE ===========\n") # for i, j in table.items(): # print(i, "\t", j) sys.stdout.write("\t{}\t{}\n".format('\t'.join(t_list), '\t'.join(nt_list))) for i, j in table.items(): sys.stdout.write("\n{}\t".format(i)) for sym in t_list + nt_list: if sym in table[i].keys(): if type(table[i][sym]) != type(set()): sys.stdout.write("{}\t".format(table[i][sym])) else: sys.stdout.write("{}\t".format(', '.join(table[i][sym]))) else: sys.stdout.write("\t") sys.stdout.write("\n") s, r = 0, 0 for p in j.values(): if p != 'accept' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 return
def start(self): pl = self.text.get("1.0", END).split("\n") + [''] print("pl ", pl) self.head.config(text="First and Follow of Non-Terminals") self.text.delete("1.0", END) self.master.geometry("700x700") self.cont.config(command=self.more) global nt_list, t_list firstfollow.production_list = firstfollow.main(pl) for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) self.text.insert(END, nt) self.text.insert( END, "\tFirst:\t{}\n".format(firstfollow.get_first(nt))) self.text.insert( END, "\tFollow:\t{}\n\n".format(firstfollow.get_follow(nt))) #self.text.config(state=DISABLED) augment_grammar() #just make sentences into a list nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] #self.text.insert(END, "{}\n".format(nt_list)) #self.text.insert(END, "{}\n".format(t_list)) self.text.see(END) self.text.config(state=DISABLED)
def main(): column_names = ["stack", "Input"] stack_df = pd.DataFrame(columns=column_names) global production_list, ntl, nt_list, tl, t_list user_gram = firstfollow.main() print("\tFIRST AND FOLLOW OF NON-TERMINALS") print("NTLIT IS", ntl) for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) st.write("For: ", nt) print(nt) st.write("First", firstfollow.get_first(nt)) st.write("Follow", firstfollow.get_follow(nt)) print("\tFirst:\t", firstfollow.get_first(nt)) print("\tFollow:\t", firstfollow.get_follow(nt), "\n") augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] print(nt_list) print(t_list) j = calc_states() ctr = 0 for s in j: st.write("Item", ctr) print("Item{}:".format(ctr)) for i in s: st.text(str(i)) print("\t", i) ctr += 1 table = make_table(j) # print("table is ", table) print( '_____________________________________________________________________' ) print("\n\tCLR(1) TABLE\n") sym_list = nt_list + t_list sr, rr = 0, 0 print( '_____________________________________________________________________' ) print('\t| ', '\t| '.join(sym_list), '\t\t|') print( '_____________________________________________________________________' ) listI = list() listJ = list() for i, j in table.items(): listI.append(i) to_append_j = (list( j.get(sym, ' ') if type(j.get(sym)) in ( str, None) else next(iter(j.get(sym, ' '))) for sym in sym_list)) listJ.append(to_append_j) print( i, "\t| ", '\t| '.join( list( j.get(sym, ' ') if type(j.get(sym)) in ( str, None) else next(iter(j.get(sym, ' '))) for sym in sym_list)), '\t\t|') s, r = 0, 0 for p in j.values(): if p != 'accept' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 nt_list.insert(0, "SN") df_columns = nt_list + t_list df = pd.DataFrame(columns=df_columns) for i in range(len(listJ)): listJ[i].insert(0, i) df.loc[len(df)] = listJ[i] st.title("CLR1 Table") st.dataframe(df) df.to_csv("check.csv") print( '_____________________________________________________________________' ) st.write(sr, "S/R conflicts") st.write(rr, "R/R Conficlts") print("\n", sr, "s/r conflicts |", rr, "r/r conflicts") print( '_____________________________________________________________________' ) print("Enter the string to be parsed") Input = user_gram + '$' print("printinf this", Input) st.write("Input string to be parsed is", Input) try: stack = ['0'] a = list(table.items()) print("productions\t:", production_list) import json jsonString = json.dumps(production_list) st.text("The Prodcution rule is: ") st.write(jsonString) # st.write(json.dumps(jsonString)) print('stack', "\t \t\t \t", 'Input') s = ''.join(str(v) for v in stack) new_stack = [] new_stack.append(s) stack_df.at[0, 'stack'] = (new_stack) # print("new stack is", new_stack) i = ''.join(str(v) for v in Input) new_input = [] new_input.append(i) stack_df.at[0, 'Input'] = (new_input) # print("new input is", new_input) print(*stack, "\t \t\t \t", *Input, sep="") i_counter = 1 j_counter = 1 while (len(Input) != 0): b = list(a[int(stack[-1])][1][Input[0]]) if (b[0][0] == "s"): #s=Input[0]+b[0][1:] stack.append(Input[0]) stack.append(b[0][1:]) Input = Input[1:] s = ''.join(str(v) for v in stack) new_stack = [] new_stack.append(s) stack_df.at[i_counter, 'stack'] = (new_stack) print("new stack is", new_stack) i = ''.join(str(v) for v in Input) new_input = [] new_input.append(i) stack_df.at[j_counter, 'Input'] = (new_input) print("new input is", new_input) print(*stack, "\t \t\t \t", *Input, sep="") i_counter = i_counter + 1 j_counter = j_counter + 1 elif (b[0][0] == "r"): s = int(b[0][1:]) #print(len(production_list),s) l = len(production_list[s]) - 3 #print(l) prod = production_list[s] l *= 2 l = len(stack) - l stack = stack[:l] s = a[int(stack[-1])][1][prod[0]] #print(s,b) stack += list(prod[0]) stack.append(s) s = ''.join(str(v) for v in stack) new_stack = [] new_stack.append(s) stack_df.at[i_counter, 'stack'] = (new_stack) print("new stack is", new_stack) i = ''.join(str(v) for v in Input) new_input = [] new_input.append(i) stack_df.at[i_counter, 'Input'] = (new_input) print("new input is", new_input) print(*stack, "\t \t\t \t", *Input, sep="") i_counter = i_counter + 1 j_counter = j_counter + 1 elif (b[0][0] == "a"): st.write("String Accepted.") print("\n\tString Accepted\n") break except: print('\n\tString INCORRECT for given Grammar!\n') st.write("String INCORRECT for given Grammar") st.title("Parsing Table for CLR Parser") stack_df.to_csv("stack.csv") st.dataframe(stack_df) return
def main(): global production_list, ntl, nt_list, tl, t_list firstfollow.main() print("\tFIRST AND FOLLOW OF NON-TERMINALS") for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) print(nt) print("\tFirst:\t", firstfollow.get_first(nt)) print("\tFollow:\t", firstfollow.get_follow(nt), "\n") augment_grammar() nt_list=list(ntl.keys()) t_list=list(tl.keys()) + ['$'] print(nt_list) print(t_list) j=calc_states() ctr=0 for s in j: print("Item{}:".format(ctr)) for i in s: print("\t", i) ctr+=1 table=make_table(j) print('_____________________________________________________________________') print("\n\tCLR(1) TABLE\n") sym_list = nt_list + t_list sr, rr=0, 0 print('_____________________________________________________________________') print('\t| ','\t| '.join(sym_list),'\t\t|') print('_____________________________________________________________________') for i, j in table.items(): print(i, "\t| ", '\t| '.join(list(j.get(sym,' ') if type(j.get(sym))in (str , None) else next(iter(j.get(sym,' '))) for sym in sym_list)),'\t\t|') s, r=0, 0 for p in j.values(): if p!='accept' and len(p)>1: p=list(p) if('r' in p[0]): r+=1 else: s+=1 if('r' in p[1]): r+=1 else: s+=1 if r>0 and s>0: sr+=1 elif r>0: rr+=1 print('_____________________________________________________________________') print("\n", sr, "s/r conflicts |", rr, "r/r conflicts") print('_____________________________________________________________________') print("Enter the string to be parsed") Input=input()+'$' try: stack=['0'] a=list(table.items()) '''print(a[int(stack[-1])][1][Input[0]]) b=list(a[int(stack[-1])][1][Input[0]]) print(b[0][0]) print(a[0][1]["S"])''' print("productions\t:",production_list) print('stack',"\t \t\t \t",'Input') print(*stack,"\t \t\t \t",*Input,sep="") while(len(Input)!=0): b=list(a[int(stack[-1])][1][Input[0]]) if(b[0][0]=="s" ): #s=Input[0]+b[0][1:] stack.append(Input[0]) stack.append(b[0][1:]) Input=Input[1:] print(*stack,"\t \t\t \t",*Input,sep="") elif(b[0][0]=="r" ): s=int(b[0][1:]) #print(len(production_list),s) l=len(production_list[s])-3 #print(l) prod=production_list[s] l*=2 l=len(stack)-l stack=stack[:l] s=a[int(stack[-1])][1][prod[0]] #print(s,b) stack+=list(prod[0]) stack.append(s) print(*stack,"\t \t\t \t",*Input,sep="") elif(b[0][0]=="a"): print("\n\tString Accepted\n") break except: print('\n\tString INCORRECT for given Grammar!\n') return
def main(): global production_list, ntl, nt_list, tl, t_list firstfollow.main() print("\tFIRST AND FOLLOW OF NON-TERMINALS") for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) print(nt) print("\tFirst:\t", firstfollow.get_first(nt)) print("\tFollow:\t", firstfollow.get_follow(nt), "\n") augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] print(nt_list) print(t_list) j = calc_stateslalr1() ctr = 0 for s in j: print("Item{}:".format(ctr)) for i in s: print("\t", i) ctr += 1 table = make_tablelalr1(j) print("table ") print("\n\tCLR(1) TABLE\n") sr, rr = 0, 0 for i, j in table.items(): print(i, "\t", j) s, r = 0, 0 for p in j.values(): if p != 'accept' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 print("\n", sr, "s/r conflicts |", rr, "r/r conflicts") # sys.stdout.write('.') sys.stdout.write("\t{}\t{}\n".format('\t'.join(t_list), '\t'.join(nt_list))) for i, j in table.items(): sys.stdout.write("\n{}\t".format(i)) for sym in t_list + nt_list: if sym in table[i].keys(): if type(table[i][sym]) != type(set()): sys.stdout.write("{}\t".format(table[i][sym])) else: sys.stdout.write("{}\t".format(', '.join(table[i][sym]))) else: sys.stdout.write("\t") sys.stdout.write("\n") s, r = 0, 0 for p in j.values(): if p != 'accept' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 return
def main(): global production_list, ntl, nt_list, tl, t_list # contentMain = [c[:-1] for c in content if c and ("\n" in c)] # print(content) firstfollow.main() augment_grammar() nt_list=list(ntl.keys()) t_list=list(tl.keys()) + ['$'] j=calc_states() ctr=0 # for s in j: # print("Item{}:".format(ctr)) # for i in s: # print("\t", i) # ctr+=1 table=make_table(j) print(" ======== table ======== \n ") sr, rr=0, 0 for i, j in table.items(): # print(i, "\t", j) s, r=0, 0 for p in j.values(): if p!='accept' and len(p)>1: p=list(p) if('r' in p[0]): r+=1 else: s+=1 if('r' in p[1]): r+=1 else: s+=1 if r>0 and s>0: sr+=1 elif r>0: rr+=1 # sys.stdout.write('.') sys.stdout.write("\t{}\t{}\n".format('\t'.join(t_list), '\t'.join(nt_list))) for i, j in table.items(): sys.stdout.write("\n{}\t".format(i)) for sym in t_list+nt_list: if sym in table[i].keys(): if type(table[i][sym])!=type(set()): sys.stdout.write("{}\t".format(table[i][sym])) else: sys.stdout.write("{}\t".format(', '.join(table[i][sym]))) else: sys.stdout.write("\t") sys.stdout.write("\n") s, r=0, 0 for p in j.values(): if p!='accept' and len(p)>1: p=list(p) if('r' in p[0]): r+=1 else: s+=1 if('r' in p[1]): r+=1 else: s+=1 if r>0 and s>0: sr+=1 elif r>0: rr+=1 print("\n\n\n", sr, "s/r conflicts |", rr, "r/r conflicts\n") return
def main(lip, nn, tables): if nn == 0: global production_list, ntl, nt_list, tl, t_list firstfollow.main(lip) li = [] li.append("\tFIRST AND FOLLOW OF NON-TERMINALS") for nt in ntl: firstfollow.compute_first(nt) firstfollow.compute_follow(nt) li.append(nt) s = str("\tFirst:\t") s = s + str(firstfollow.get_first(nt)) li.append(s) s = str("\tFollow:\t") s = s + str(firstfollow.get_follow(nt)) li.append(s) file = open( "C:\\Users\\himanshu\\PycharmProjects\\pythonProject\\first_follow.txt", 'w') for ele in li: file.write(ele + '\n') file.close() li = [] augment_grammar() nt_list = list(ntl.keys()) t_list = list(tl.keys()) + ['$'] print(nt_list) print(t_list) j = calc_states() ctr = 0 for s in j: s = str("Item{}:".format(ctr)) for i in s: s = s + str("\t") s = s + str(i) li.append(s) ctr += 1 file = open( "C:\\Users\\himanshu\\PycharmProjects\\pythonProject\\item.txt", 'w') for ele in li: file.write(ele + '\n') file.close() files = open( "C:\\Users\\himanshu\\PycharmProjects\\pythonProject\\table.txt", 'w') table = make_table(j) print( '+--------+-----------------------------------------------------+-------------------+', file=files) print( "| | GOTO | ACTION |", file=files) sym_list = nt_list + t_list sr, rr = 0, 0 print( '+--------+-----------------------------------------------------+-------------------+', file=files) print('|\t| ', '\t| '.join(sym_list), '\t\t |', file=files) for i, j in table.items(): print("|", i, "\t|", " \t| ".join( list( j.get(sym, ' ') if type(j.get(sym)) in ( str, None) else next(iter(j.get(sym, ' '))) for sym in sym_list)), "\t\t |", file=files) s, r = 0, 0 for p in j.values(): if p != 'ac' and len(p) > 1: p = list(p) if ('r' in p[0]): r += 1 else: s += 1 if ('r' in p[1]): r += 1 else: s += 1 if r > 0 and s > 0: sr += 1 elif r > 0: rr += 1 print( '+--------+-----------------------------------------------------+-------------------+', file=files) print(sr, "s/r conflicts |", rr, "r/r conflicts", file=files) print( '+--------+-----------------------------------------------------+-------------------+', file=files) files.close() return table files = open( "C:\\Users\\himanshu\\PycharmProjects\\pythonProject\\stack.txt", 'w') Input = nn + '$' try: stack = ['0'] a = list(tables.items()) '''print(a[int(stack[-1])][1][Input[0]]) b=list(a[int(stack[-1])][1][Input[0]]) print(b[0][0]) print(a[0][1]["S"])''' print("productions\t:", production_list) print('stack', "\t \t\t \t", 'Input', file=files) for i in stack: files.write(i + "\t\t\t\t" + Input + "\n") while (len(Input) != 0): b = list(a[int(stack[-1])][1][Input[0]]) if (b[0][0] == "s"): # s=Input[0]+b[0][1:] stack.append(Input[0]) stack.append(b[0][1:]) Input = Input[1:] for i in stack: files.write(i) files.write("\t\t\t\t" + Input + "\n") elif (b[0][0] == "r"): s = int(b[0][1:]) # print(len(production_list),s) l = len(production_list[s]) - 3 # print(l) prod = production_list[s] l *= 2 l = len(stack) - l stack = stack[:l] s = a[int(stack[-1])][1][prod[0]] # print(s,b) stack += list(prod[0]) stack.append(s) for i in stack: files.write(i) files.write("\t\t\t\t" + Input + "\n") elif (b[0][0] == "a"): print("\n\tString Accepted\n", file=files) break except: print('\n\tString INCORRECT for given Grammar!\n', file=files) files.close() return