Skip to content

Instantly share code, notes, and snippets.

@devpruthvi
Created October 31, 2015 14:15
Show Gist options
  • Select an option

  • Save devpruthvi/86557eaedb94e33b04b9 to your computer and use it in GitHub Desktop.

Select an option

Save devpruthvi/86557eaedb94e33b04b9 to your computer and use it in GitHub Desktop.
A program to remove left recursion in C with sscanf
#include<stdio.h>
#include<string.h>
void main() {
char input[100],l[50],r[50],temp[10],tempprod[20],productions[25][50];
int i=0,j=0,flag=0,consumed=0;
printf("Enter the productions: ");
scanf("%1s->%s",l,r);
printf("%s",r);
while(sscanf(r+consumed,"%[^|]s",temp) == 1 && consumed <= strlen(r)) {
if(temp[0] == l[0]) {
flag = 1;
sprintf(productions[i++],"%s->%s%s'\0",l,temp+1,l);
}
else
sprintf(productions[i++],"%s'->%s%s'\0",l,temp,l);
consumed += strlen(temp)+1;
}
if(flag == 1) {
sprintf(productions[i++],"%s->ε\0",l);
printf("The productions after eliminating Left Recursion are:\n");
for(j=0;j<i;j++)
printf("%s\n",productions[j]);
}
else
printf("The Given Grammar has no Left Recursion");
}
@mvrozanti
Copy link
Copy Markdown

Enter the productions: E->E+E|T
The productions after eliminating Left Recursion are:
E->+EE'
E'->TE'
E->ε

Testing with the above productions seem to have wrong output.

@syed-jamal
Copy link
Copy Markdown

This doesn't seem to handle indirect and hidden left recursion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment