-
Notifications
You must be signed in to change notification settings - Fork 0
/
Follow.c
129 lines (120 loc) · 3.21 KB
/
Follow.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Subject: Compiler , to find follow of given symbol from given grammar
#include<stdio.h>
#include<string.h>
int prodCount;
//.....productions array......non-terminal[]............terminal[].............
char prod[30][30];
char nt[10];
char t[10];
//...............................................
char frst[20]; // stores first set
int frc; // count firsts
char folw[20]; // stores follow
int fwc; // count follows
//..............................................
int firstForFollow(char c,int hop);
int prevr,prevc; // it will store row n col of previous called first()
void follow(char c);
int ntc;
//.................FOLLOW.............................
void follow(char c)
{
int i,j;
for(i=0;i<prodCount;i++)
{
for(j=3;prod[i][j]!='\0';j++)
{
if(prod[i][j] == c )
{
if(prod[i][j+1]!='\0') // if its not at last of production
{
int hop = 1;
while(1>0)
{
int nu = firstForFollow(prod[i][j+hop],0);
if(nu == 1)
hop = hop + 1;
else
break;
}
strcat(folw,frst);
}
else
{
follow(prod[i][0]); // if its last,then find follow of LHS
}
}
}
}
if(prod[0][0] == c)
{ // if it is also a start symbol
if(!strchr(frst,'$'))
{
strcat(folw,"$");
fwc ++;
}
}
}
int firstForFollow(char c,int hop)
{
int i,j;
int nu = 0; // nu = 1 when first contains #
int prodIndex = 0;
if(c != '#') // not a null
{
for(i=0;i<prodCount;i++)
{
if(prod[i][0] == c){ //checks if non-terminal symbol
prodIndex = i;
hop = 0;
firstForFollow(prod[prodIndex][3+hop],hop);
prevr= prodIndex;
prevc= 3 + hop;
}
else // if terminal
{
if(!strchr(frst,c) && !strchr(nt,c)) // if not already in set & not a Non-Terminal
{
char x[] = "" ;
x[0] = c;
strcat(frst,x);
}
}
}
}
else
{ // if it is null
nu = 1;
if(!strchr(frst,c))
{
//strcat(frst,c);
}
hop++;
prevc = prevc + hop;
char d = prod[prevr][prevc+3];
firstForFollow(d , hop);
}
return nu;
}
//..............................................
int main()
{
int i,j=0;
printf("Number of productions? ");
scanf("%d",&prodCount);
for(i=0;i<prodCount;i++)
{
printf("Enter prod %d: ",i);
scanf("%s",prod[i]);
nt[ntc] = prod[i][0];
ntc++;
}
char ip[1];
printf("Enter char symbol ? ");
scanf("%s",ip);
printf("\n follow of %c is : { ",ip[0]);
follow(ip[0]);
printf(" %s",folw);
printf(" }");
}
/*Main ends*/