-
Notifications
You must be signed in to change notification settings - Fork 0
/
left factoring (seme).c
45 lines (42 loc) · 1.15 KB
/
left factoring (seme).c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<stdio.h>
#include<string.h>
int main()
{
char gram[20],part1[20],part2[20],part3[20],modifiedGram[20],newGram[20],tempGram[20];
int i,j=0,k=0,l=0,m=0,pos;
printf("Enter Production : A->");
gets(gram);
for(i=0;gram[i]!='|';i++,j++)
part1[j]=gram[i];
part1[j]='\0';
for(j=++i,i=0;gram[j]!='|';j++,i++)
part2[i]=gram[j];
part2[i]='\0';
for(i=++j,j=0;gram[i]!='\0';j++,i++)
part3[j]=gram[i];
part3[j]='\0';
for(i=0;i<strlen(part1)||i<strlen(part2);i++){
if(part1[i]==part2[i]){
modifiedGram[k]=part1[i];
k++;
pos=i+1;
}
}
for(i=pos,j=0;part1[i]!='\0';i++,j++,m++){
newGram[j]=part1[i];
}
newGram[j++]='|';
for(i=pos;part2[i]!='\0';i++,j++,m++){
newGram[j]=part2[i];
}
newGram[j++]='|';
for(i=pos;part3[i]!='\0';i++,j++,m++){
newGram[j]=part3[i];
}
modifiedGram[k]='A';
modifiedGram[++k]='\0';
newGram[j]='\0';
printf("\nGrammar Without Left Factoring : \n");
printf(" A->%s'",modifiedGram);
printf("\n A'->%s\n",newGram);
}