-
Notifications
You must be signed in to change notification settings - Fork 0
/
ComputationOfFirst.c
113 lines (98 loc) · 2.85 KB
/
ComputationOfFirst.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
#include <ctype.h>
#include <stdio.h>
#include <string.h>
void findfirst(char, int, int);
int count, n = 0;
char calc_first[10][100];
char production[10][10];
char first[10];
int k;
int e;
int main(int argc, char** argv) {
int jm = 0;
int i;
char c;
count = 8;
// The Input grammar
strcpy(production[0], "E=TA");
strcpy(production[1], "A=+TA");
strcpy(production[2], "A=#");
strcpy(production[3], "T=FB");
strcpy(production[4], "B=*FB");
strcpy(production[5], "B=#");
strcpy(production[6], "F=(E)");
strcpy(production[7], "F=i");
int kay;
char done[count];
int ptr = -1;
// Initializing the calc_first array
for (k = 0; k < count; k++) {
for (kay = 0; kay < 100; kay++) {
calc_first[k][kay] = '!';
}
}
int point1 = 0, point2, xxx;
for (k = 0; k < count; k++) {
c = production[k][0];
point2 = 0;
xxx = 0;
// Checking if First of c has already been calculated
for (kay = 0; kay <= ptr; kay++)
if (c == done[kay])
xxx = 1;
if (xxx == 1)
continue;
// Function call
findfirst(c, 0, 0);
ptr += 1;
// Adding c to the calculated list
done[ptr] = c;
printf("\n First(%c) = { ", c);
calc_first[point1][point2++] = c;
// Printing the First Sets of the grammar
for (i = 0 + jm; i < n; i++) {
int lark = 0, chk = 0;
for (lark = 0; lark < point2; lark++) {
if (first[i] == calc_first[point1][lark]) {
chk = 1;
break;
}
}
if (chk == 0) {
printf("%c, ", first[i]);
calc_first[point1][point2++] = first[i];
}
}
printf("}\n");
jm = n;
point1++;
}
}
void findfirst(char c, int q1, int q2) {
int j;
// The case where we encounter a Terminal
if (!(isupper(c))) {
first[n++] = c;
}
for (j = 0; j < count; j++) {
if (production[j][0] == c) {
if (production[j][2] == '#') {
if (production[q1][q2] == '\0')
first[n++] = '#';
else if (production[q1][q2] != '\0' && (q1 != 0 || q2 != 0)) {
// Recursion to calculate First of New Non-Terminal we encounter after epsilon
findfirst(production[q1][q2], q1, (q2 + 1));
}
else
first[n++] = '#';
}
else if (!isupper(production[j][2])) {
first[n++] = production[j][2];
}
else {
// Recursion to calculate First of New Non-Terminal we encounter at the beginning
findfirst(production[j][2], j, 3);
}
}
}
}