This repository has been archived by the owner on Oct 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
7.FOLLOW.c
144 lines (122 loc) · 2.49 KB
/
7.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int nop, m = 0;
char prod[10][10], res[10];
void FIRST(char c);
void FOLLOW(char c);
void result(char c);
int main()
{
int i;
int choice;
char c;
printf("Enter the no. of productions : ");
scanf("%d", &nop);
puts("\nEnter the production string like \"E=E+T\" \nand epsilon as #\n");
for (i = 0; i < nop; ++i)
{
printf("Enter production Number %d : ", i + 1);
scanf("%s", prod[i]);
}
do
{
m = 0;
memset(res, '\0', sizeof(res));
printf("\nFind FOLLOW of --> ");
scanf(" %c", &c);
if (isupper(c))
FOLLOW(c);
else
{
printf("Not Possible");
return 0;
}
printf("Follow (%c) = { ", c);
for (i = 0; i < m; ++i)
printf("%c ", res[i]);
puts(" }");
printf("Do you want to continue(Press 1 to continue...) ? ");
scanf("%d", &choice);
} while (choice == 1);
return 0;
}
void FOLLOW(char c)
{
int i, j;
if (prod[0][0] == c)
result('$');
for (i = 0; i < nop; ++i)
{
for (j = 2; j <= strlen(prod[i]); ++j)
{
if (prod[i][j] == c)
{
if (prod[i][j + 1] != '\0')
FIRST(prod[i][j + 1]);
if (prod[i][j + 1] == '\0' && c != prod[i][0])
FOLLOW(prod[i][0]);
}
}
}
return;
}
void FIRST(char c)
{
int k;
if (!(isupper(c)))
result(c);
for (k = 0; k < nop; ++k)
{
if (prod[k][0] == c)
{
if (prod[k][2] == '#')
FOLLOW(prod[k][0]);
else if (prod[k][2] == c)
return;
else if (islower(prod[k][2]))
result(prod[k][2]);
else
FIRST(prod[k][2]);
}
}
return;
}
void result(char c)
{
int i;
for (i = 0; i <= m; ++i)
if (res[i] == c)
return;
res[m++] = c;
}
/*
Input/ Output :-
-------------
Enter the no. of productions : 8
Enter the production string like "E=E+T"
and epsilon as #
Enter production Number 1 : E=TX
Enter production Number 2 : X=+TX
Enter production Number 3 : X=#
Enter production Number 4 : T=FY
Enter production Number 5 : Y=*FY
Enter production Number 6 : Y=#
Enter production Number 7 : F=(E)
Enter production Number 8 : F=i
Find FOLLOW of --> E
Follow (E) = { $ ) }
Do you want to continue(Press 1 to continue...) ? 1
Find FOLLOW of --> X
Follow (X) = { $ ) }
Do you want to continue(Press 1 to continue...) ? 1
Find FOLLOW of --> T
Follow (T) = { + $ ) }
Do you want to continue(Press 1 to continue...) ? 1
Find FOLLOW of --> Y
Follow (Y) = { + $ ) }
Do you want to continue(Press 1 to continue...) ? 1
Find FOLLOW of --> F
Follow (F) = { * + $ ) }
Do you want to continue(Press 1 to continue...) ? 0
*/