Programming Blog

This blog is about technical and programming questions and there solutions. I also cover programs that were asked in various interviews, it will help you to crack the coding round of various interviews

Saturday 15 April 2017

C program to remove Left Recursion form a grammer

#include<stdio.h>

#include<conio.h>
void main()
{
char a[10][100],b[10][100],c[1];
int i,n;
printf("Enter no of production\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
gets();
printf("Enter prodution %d -- ",(i+1));
gets(a[i]);
}
int flag=0,j=0;
if(a[0][3]==a[0][0])
{
for(i=0;a[0][i]!='\0';i++)
{
if(i<3)
b[0][j++]=a[0][i];
if(flag==1)
{
b[0][j++]=a[0][i];
}
if(a[0][i]=='|')
{
flag=1;
}

}
b[0][j++]=a[0][0];
b[0][j++]='\'';
b[0][j]='\0';
j=0;
for(i=0;a[0][i]!='|';i++)
{
if(i<3)
b[1][j++]=a[0][i];
if(i>3)
{
b[1][j++]=a[0][i];
}
}
b[1][j++]=a[0][0];
b[1][j++]='\'';
b[1][j++]='|';
b[1][j++]='&';
b[1][j++]='\0';
}
printf("\n%s\n%s",b[0],b[1]);
}

No comments:

Post a Comment