-
Notifications
You must be signed in to change notification settings - Fork 0
/
FOLLOW_of_Grammar.cpp
95 lines (86 loc) · 2.21 KB
/
FOLLOW_of_Grammar.cpp
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
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int n, j;
char a[10][10], Result[10];
char subResult[20];
void follow(char *Result, char c);
void first(char *Result, char c);
void addToResultSet(char[], char);
int main()
{
int i;
char c, choice;
printf("Enter the number of productions: ");
scanf("%d", &n);
printf("Enter %d productions.\nProduction with multiple terms should be given as separate productions\n", n);
for (i = 0; i < n; i++)
scanf("%s", a[i]);
do
{
printf("Find FOLLOW of: ");
scanf(" %c", &c);
follow(Result, c);
printf("FOLLOW(%c) = { ", c);
for (i = 0; Result[i] != '\0'; i++)
printf("%c ", Result[i]);
printf(" }\n");
printf("Do you want to continue (y/n)?: ");
scanf(" %c", &choice);
} while (choice == 'y' || choice == 'Y');
return 0;
}
void follow(char *Result, char c)
{
int k;
subResult[0] = '\0';
Result[0] = '\0';
if (a[0][0] == c)
addToResultSet(Result, '$');
for (int i = 0; i < n; i++)
{
for (j = 2; j < strlen(a[i]); j++)
{
if (a[i][j] == c)
{
if (a[i][j + 1] != '\0')
first(subResult, a[i][j + 1]);
if (a[i][j + 1] == '\0' && c != a[i][0])
follow(subResult, a[i][0]);
for (k = 0; subResult[k] != '\0'; k++)
addToResultSet(Result, subResult[k]);
}
}
}
}
void first(char *R, char c)
{
int k, m;
if (!(isupper(c)) && c != '#')
addToResultSet(R, c);
for (k = 0; k < n; k++)
{
if (a[k][0] == c)
{
if (a[k][2] == '#' && c != a[k][0])
follow(R, a[k][0]);
else if (!(isupper(a[k][2])) && a[k][2] != '#')
addToResultSet(R, a[k][2]);
else
first(R, a[k][2]);
for (m = 0; R[m] != '\0'; m++)
addToResultSet(Result, R[m]);
}
}
}
void addToResultSet(char Result[], char val)
{
int k;
for (k = 0; Result[k] != '\0'; k++)
{
if (Result[k] == val)
return;
}
Result[k] = val;
Result[k + 1] = '\0';
}