Thursday, 26 April 2018

WAP to remove left recursion from a given grammar (SPCC)

production=input("Enter the production:")
print(production)
product=production.split('->')
l=product[1].split('|')
ans=product[0]+'\''
anslist=[]
anslist2=['epsilon']
for e in l:
if(product[0] not in e):
anslist.append(e+ans)
else:
anslist2.append(e[1:]+ans)
answer=''
for e in anslist:
answer=answer+e+'|'
answer=answer[:-1]
if(answer==''):
answer=ans
print(product[0]+'->'+answer)
answer=''
for e in anslist2:
answer=answer+e+'|'
answer=answer[:-1]
print(ans+'->'+answer)

Output:

Enter the production:E->E+T|E*T|T
E->E+T|E*T|T
E=>TE'
E'=>epsilon|+TE'|*TE'

Enter the production:E->E+T|E*T|T|T2
E->E+T|E*T|T|T2
E=>TE'|T2E'
E'=>epsilon|+TE'|*TE'

No comments:

Post a Comment