You are on page 1of 2

PROGRAM NO:-4

WRITE A PROGRAM TO REMOVE LEFT RECURTION FROM A


GRAMMAR
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
char a[15]={"A->Ab/ac"};
char choice, count;
char temp1[5]={" "};
char temp2[5]={" "};
clrscr();
cout<<"the grammer given is:"<<a<<"\n";
cout<<"would you like to enter another grammer(y/n)::";
cin>>choice;
if (choice=='y')
{
cout<<"enter the production:";
cin>>a;
}
cout<<"the new productions are;"<<a;
for(int i=3;i<strlen(a);i++)
{
if (a[i]=='/')
{
count=i;
break;
}
}
cout<<"\n**********************************";
cout<<"\n\n the final productions are;\n";
if(a[3]!=a[0]&&a[count+1]!=a[0])
cout<<a;
else
{
if(a[3]==a[0])
{
int j=0;

for(i=4;i<count;i++)
{
temp1[j]=a[i];
j++;
}
for(i=count+1;i<strlen(a);i++)
temp2[i-count-1]=a[i];
}
else
if(a[count+1]==a[0])
{
for(i=3;i<count;i++)
temp2[i-3]=a[i];
for(i=count+2;i<strlen(a);i++)
temp1[i-count-2]=a[i];
}
cout<<a[0]<<"->"<<temp2<<a[0]<<"'\n";
cout<<a[0]<<"'->"<<temp1<<a[0]<<"'/^";
}
getch();
}

You might also like