-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrecursion.c
71 lines (62 loc) · 1.31 KB
/
lrecursion.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<stdio.h>
#include<string.h>
// Program in C to remove left recursion from given grammar
// Removing Left Recursion
/*Input: A->Axy|z
Output: A->zA'
A'->xyA'|e
*/
int main()
{
int i,j,n,k;
int lrec = 0;
char prod[100];
char newprod1[100]= "";
char newprod2[100]= "";
char alpha[100] = "";
char beta[100]= "";
char sts[20]="";
scanf("%s",prod);
sts[0] = prod[0]; // sts is Start Symbol
int size = (int)(strlen(prod));
for(i=0;i<size;i++)
{
if(prod[i] == '|')
{
k = i;
}
}
if(prod[3] == prod[0])
{
lrec = 1;
}
if(lrec == 1)
{
int c =0;
k = k-1;
for(i=4;i<=k;i++)
{
alpha[c] = prod[i];
c++;
}
c = 0;
for(i=k+2;i<size;i++)
{
beta[c] = prod[i];
c++;
}
strcat(newprod1,sts);
strcat(newprod1,"->");
strcat(newprod1,beta);
strcat(newprod1,sts);
strcat(newprod1,"'");
strcat(newprod2,sts);
strcat(newprod2,"'");
strcat(newprod2,"->");
strcat(newprod2,alpha);
strcat(newprod2,sts);
strcat(newprod2,"'");
strcat(newprod2,"|e");
printf("\n%s\n%s",newprod1,newprod2);
}
}