Removing left factoring and left recursion
C Program To Remove Left Factoring
1: #include<stdio.h>
2: #include<string.h>
3: int main()
4: {
5: char gram[20],part1[20],part2[20],modifiedGram[20],newGram[20],tempGram[20];
6: int i,j=0,k=0,l=0,pos;
7: printf("Enter Production : A->");
8: gets(gram);
9: for(i=0;gram[i]!='|';i++,j++)
10: part1[j]=gram[i];
11: part1[j]='\0';
12: for(j=++i,i=0;gram[j]!='\0';j++,i++)
13: part2[i]=gram[j];
14: part2[i]='\0';
15: for(i=0;i<strlen(part1)||i<strlen(part2);i++)
16: {
17: if(part1[i]==part2[i])
18: {
19: modifiedGram[k]=part1[i];
20: k++;
21: pos=i+1;
22: }
23: }
24: for(i=pos,j=0;part1[i]!='\0';i++,j++){
25: newGram[j]=part1[i];
26: }
27: newGram[j++]='|';
28: for(i=pos;part2[i]!='\0';i++,j++){
29: newGram[j]=part2[i];
30: }
31: modifiedGram[k]='X';
32: modifiedGram[++k]='\0';
33: newGram[j]='\0';
34: printf("\n A->%s",modifiedGram);
35: printf("\n X->%s\n",newGram);
36: }
Remove Left Factoring
C Program To Remove Left Recusrion
#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");
}
Remove Left Recusrion
Output:
Enter the productions: E->E+E|T
The productions after eliminating Left Recursion are:
E->+EE'
E'->TE'
E->ε
No comments:
Post a Comment