Coding Horror, be scared


Program to find nfa from epsilon nfa

/** * Add your code here * Any language, just go for it. */ # -*- coding: utf-8 -*- print ("test") TEST2=1 top=-1 def disparray( a, n): for i in range (0,n): print (a[i]) print ("\n") def dispeclosure(n,eclosure): for i in range (0,n): print ("State:",i) for j in range (0,n): if (eclosure[i][j]!=-1): print (eclosure[i][j],",") print ("\n") def check(ar1,a,n): f=0 for i in range (0,n): if ar1[i]==a: f=1 return f def push(stack,n,item): if top>=40: print ("Stack OVF") else: top=top+1 stack[top]=item def pop( stack): if top <0: print ("underflow") else: a=top top=top-1 return stack[a] def displaynfa(nst1,nsym1,NFA_TABLE): nst=nst1 nsym=nsym1 for i in range (0,nst): print ("State",i) for j in range (0,nsym): print ("Symbol",j) for k in range (0,nst): if (NFA_TABLE[i][j][k]!=-1): print (NFA_TABLE[i][j][k]) else: break print ("\t \t ") print ("\n") def displaynfa2(nst1,nsym1,NFA_TABLE): nst=nst1 nsym=nsym1 for i in range (0,nst): print ("State",i) for j in range (1,nsym): print ("Symbol",j) for k in range (0,nst): if (NFA_TABLE[i][j][k]!=-1): print (NFA_TABLE[i][j][k]) else: break print ("\t \t ") print ("\n") def geteclosure(nst,nsym,NFA_TABLE,eclosure): stack[50] for k in range (0,nst): currstate11=k for i in range (0,nst): eclosure[i][0]=i currstate=currstate11 ei=1 while True: if top!=-1: currstate=pop(stack) for i in range (0,nst): temp=NFA_TABLE[currstate][0][i] if temp==-1: # break TEST2=0 else: be=check(eclosure[currstate11],temp,nst) if be ==0: eclosure[currstate11][ei]=temp ei=ei+1; push(stack,nst,temp) if top==-1: break Nstates=int(input("ENter no. of states \n")) Nsymbols=int(input("Enter no. of i/p incl e as sym0 \n")) NFA_TABLE=[[[0 for i in range(Nstates)] for j in range(Nsymbols)] for k in range(Nstates)] eclosure=[[0 for i in range(Nstates)] for j in range(Nstates)] for i in range (0,Nstates): for j in range (0,Nstates): eclosure[i][j]=-1 for i in range (0,Nstates): for j in range (0,Nsymbols): for k in range (0,Nstates): NFA_TABLE[i][j][k]=-1 stack=[40] for i in range (0,Nstates): top=-1 for j in range(0,Nsymbols): transcount=int(input('Enter no. of tr for state {} and symb {}'.format(i,j))) for k in range (0,Nstates): if k< transcount: tempstate=int(input("Enter states 1 by 1 for {}".format(j)) if tempstate > Nstates: print ("INV state name") k=k-1 else: NFA_TABLE[i][j][k]=tempstate else: NFA_TABLE[i][j][k]=-1 startstate=int(input("Enter startstate:")) fn=int(input("Enter no. of final states")) finalstates[Nstates] for i in range (0,fn): finalstates[i]=int(input("Enter final states 1by 1")) print ("\n") print ("Input enfa \n ") print ("\n") displaynfa(Nstates,Nsymbols,NFA_TABLE) print ("\n") print ("\n start state:",startstate) print ("\n") print ("Final states : ") disparray(finalstates,fn) print ("\n") geteclosure(Nstates,Nsymbols,NFA_TABLE,eclosure) print ("e closure of input \n") dispeclosure(Nstates,eclosure) addf=fn for i in range (0,Nstates): for j in range (0,Nstates): b=eclsoure[i][j] if b==-1: break if(check(finalstates,b,fn)==1): if check(finalstates,i,addf)==0: finalstates[add]=i addf=addf+1 for k in range (1,Nsymbols): for l in range (1,Nstates): c=NFA_TABLE[b][k][l] NFA_TABLE[b][0][l]=-1 if c==-1: break for p in range (0,Nstates): r=eclosure[c][p] if r==-1: break s=check(NFA_TABLE[i][k],r,Nstates) if s==0: for z in range (0,Nstates): if NFA_TABLE[i][k][z]==-1: NFA_TABLE[i][k][z]=r break print ("\n") print ("\n o/p is \n") displaynfa2(Nstates,Nsymbols,NFA_TABLE) print("\n") print("Start state:",startstate) print("\n") disparray(finalstates,addf)

Reactions 0 reactions

No reactions yet, be the first: comment or tweak the code

Comment on this code:

Please Login or Register to leave a comment.