CDP 1

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 1

#include<stdio.

h>
#include<stdlib.h>
#include<string.h>

#define S 20

int main()
{
char P[S], A[S], B[S];
int non,i,j, index=3;

printf("Enter the Production as E->E|A: ");


scanf("%s", P);

non=P[0];
if(non==P[index]) //Checking if the Grammar is LEFT RECURSIVE
{
//Getting Alpha
for(i=++index,j=0;P[i]!='|';i++,j++){
A[j]=P[i];
//Checking if there is NO Vertical Bar (|)
if(P[i+1]==0){
printf("This Grammar CAN'T BE REDUCED.\n");
exit(0); //Exit the Program
}
}
A[j]='\0'; //String Ending NULL Character

if(P[++i]!=0) //Checking if there is Character after Vertical Bar (|)


{
//Getting Beta
for(j=i,i=0;P[j]!='\0';i++,j++){
B[i]=P[j];
}
B[i]='\0'; //String Ending NULL character

//Showing Output without LEFT RECURSION


printf("\nGrammar Without Left Recursion: \n\n");
printf(" %c->%s%c'\n", non,B,non);
printf(" %c'->%s%c'|#\n", non,A,non);
}
else
printf("This Grammar CAN'T be REDUCED.\n");
}
else
printf("\n This Grammar is not LEFT RECURSIVE.\n");
}

You might also like