0% found this document useful (0 votes)
113 views1 page

Left Recursion

This C program takes in a context-free grammar as input from the user, with productions separated by pipes. It then checks each production to see if it is left recursive by comparing the first non-terminal symbol to the symbol after the arrow. If it finds left recursion, it prints the grammar without left recursion by splitting the production into two - one with the leftmost symbol moving to the right side and a new non-terminal symbol, and another production defining the new non-terminal.

Uploaded by

axar kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
113 views1 page

Left Recursion

This C program takes in a context-free grammar as input from the user, with productions separated by pipes. It then checks each production to see if it is left recursive by comparing the first non-terminal symbol to the symbol after the arrow. If it finds left recursion, it prints the grammar without left recursion by splitting the production into two - one with the leftmost symbol moving to the right side and a new non-terminal symbol, and another production defining the new non-terminal.

Uploaded by

axar kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

#include<stdio.

h>
#include<string.h>
#define SIZE 10
int main () {
char non_terminal;
char beta,alpha;
int num;
int i;
char production[10][SIZE];
int index=3; /* starting of the string following "->" */
printf("Enter Number of Production : ");
scanf("%d",&num);
printf("Enter the grammar as E->E-A :\n");
for(i=0;i<num;i++){
scanf("%s",production[i]);
}

for(i=0;i<num;i++){
printf("\nGRAMMAR : : : %s",production[i]);
non_terminal=production[i][0];
if(non_terminal==production[i][index]) {
alpha=production[i][index+1];
printf(" is left recursive.\n");
while(production[i][index]!=0 && production[i][index]!='|') {
index++; }
if(production[i][index]!=0) {
beta=production[i][index+1];
printf("Grammar without left recursion:\n");
printf("%c->%c%c\'",non_terminal,beta,non_terminal);
printf("\n%c\'->%c%c%c\'|e\n",non_terminal,alpha,beta,non_terminal);
}
else
printf(" can't be reduced\n");
}
else
printf(" is not left recursive.\n");
index=3;
}
return 0;
}

You might also like