forked from zameel7/CD-Lab
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfandf.c
74 lines (68 loc) · 1.63 KB
/
fandf.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
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
int n, m = 0, i = 0, j = 0;
char a[10][10], f[10];
void follow(char c);
void first(char c);
int main()
{
int z;
char c, ch;
printf("Enter the no of productions:\n");
scanf("%d", &n);
printf("Enter the productions:\n");
for (i = 0; i < n; i++)
scanf("%s%c", a[i], &ch);
do {
m = 0;
printf("\n\nEnter the elements whose first and follow is to be found:");
scanf("%c", &c);
first(c);
printf("First(%c)={", c);
for (i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
strcpy(f, " ");
m = 0;
follow(c);
printf("Follow(%c)={", c);
for (i = 0; i < m; i++)
printf("%c", f[i]);
printf("}\n");
printf("Continue(0/1)?");
scanf("%d%c", &z, &ch);
} while (z == 1);
return (0);
}
void first(char c) {
int k;
if (!isupper(c))
f[m++] = c;
else {
for (k = 0; k < n; k++) {
if (a[k][0] == c) {
if (a[k][2] == '$' || islower(a[k][2]))
f[m++] = a[k][2];
else
first(a[k][2]);
}
}
}
}
void follow(char c) {
if (a[0][0] == c)
f[m++] = '$';
for (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(a[i][j + 1]);
if (a[i][j + 1] == '\0' && c != a[i][0])
follow(a[i][0]);
}
}
}
}