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

Thursday, 16 November 2017

Left Recursion Grammer

#include<stdio.h>
#include<string.h>
 void leftree(char enter[20]);
char enter[5][2];
int x;
char p1[20],p2[20];
void main()
{
int s;
clrscr();
printf("enter number of productions\n");
scanf("%d",&x);
 if(x>0)
 {
   for(s=0;s<=x;s++)
    {
    printf("enter  productions\n");
    gets(enter[s]);
    leftree(enter[s]);
    }
 }
getch();
}
void leftree(char enter[20])
{  int i=0,k=0,j=0;
   char n1=enter[0];
   while(enter[i]!='>')
   {    i++;
  if(enter[i]==n1)
{
   i++;
   p1[0]=n1;
   p1[1]='_';
   p1[2]='>';
   j=3;
   p2[0]=n1;
   p2[1]='p';
   p2[1]='_';
   p2[3]='>';
   k=4;
   while(enter[i]!=1)
      {
      p2[k]=enter[i];
      k++;
      i++;
      }
   i++;
   p2[k]=n1;
   k++;
   p2[k++]='p';
   p2[k++]='1';
   p2[k++]='#';
   while(enter[i]!='\0')
      {
       p1[j++]=n1;
       p1[j]='p';
       puts(p1);
       puts(p2);
       }
  }
  else
    {
    printf("the entered grammer is not left recursive\n");
    }
    }
}






x

No comments:

Post a Comment