-
Notifications
You must be signed in to change notification settings - Fork 2
/
follow.c
172 lines (161 loc) · 3.78 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
BATCH NO. 27
Mayank Agarwal (2014A7PS111P)
Karan Deep Batra(2014A7PS160P)
*/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include "ntort.h"
#include "hashtable.h"
#include "grammar.h"
#include "first.h"
#include "follow.h"
#include "lexerDef.h"
int terminaladded[maxnonterminals+5][numberofterminals+5];
void calculateFollow(int ind)
{
if(followdone[ind] == 1)
return;
ntort* start = toppointers[ind].firstntort;
ntort* temp;
ntort* followhelper;
while(start != NULL)
{
temp = start;
ntort* last;
while(1)
{
if(strcmp(temp->next->str,"$") == 0)
{
//copy follow of temp->next->next to follow[ind]
if(ind == temp->next->next->val)
break;
if(!followdone[temp->next->next->val])
calculateFollow(temp->next->next->val);
followhelper = followSets[temp->next->next->val];
if(followSets[ind] == NULL)
{
followSets[ind] = makentortnode(0, followhelper->val, followhelper->str);
terminaladded[ind][followhelper->val] = 1;
last = followSets[ind];
followhelper = followhelper->next;
}
while(followhelper != NULL)
{
if(terminaladded[ind][followhelper->val] == 1)
{
followhelper = followhelper->next;
continue;
}
last->next = makentortnode(0, followhelper->val, followhelper->str);
terminaladded[ind][followhelper->val] = 1;
last = last->next;
followhelper = followhelper->next;
}
break;
}
else if(temp->next->nt == 0)
{
//append terminal to follow[ind]
if(followSets[ind] == NULL)
{
followSets[ind] = makentortnode(0, temp->next->val, temp->next->str);
terminaladded[ind][temp->next->val] = 1;
last = followSets[ind];
}
else
{
if(terminaladded[ind][temp->next->val] == 1)
break;
last->next = makentortnode(0, temp->next->val, temp->next->str);
terminaladded[ind][temp->next->val] = 1;
last = last->next;
}
break;
}
else
{
followhelper = firstSets[temp->next->val];
int containseps = 0;
if(followSets[ind] == NULL)
{
if(strcmp(followhelper->str, "eps") == 0)
containseps = 1;
else
{
followSets[ind] = makentortnode(0, followhelper->val, followhelper->str);
terminaladded[ind][followhelper->val] = 1;
last = followSets[ind];
}
followhelper = followhelper->next;
}
while(followhelper != NULL)
{
if(strcmp(followhelper->str, "eps") == 0)
containseps = 1;
else
{
if(terminaladded[ind][followhelper->val] == 1)
{
followhelper = followhelper->next;
continue;
}
last->next = makentortnode(0, followhelper->val, followhelper->str);
terminaladded[ind][followhelper->val] = 1;
last = last->next;
}
followhelper = followhelper->next;
}
if(containseps)
temp = temp->next;
else
break;
//iterate and copy without eps
//if eps present then see next
//if no eps then break;
}
}
start = start->down;
}
followdone[ind] = 1;
return;
}
void populateFollowSets(hashtable* table)
{
FILE* fp = fopen("nonterminals.txt", "rb");
char buff[100];
fscanf(fp, "%s", buff);
int ind = present(table, buff);
followSets[ind] = makentortnode(0, present(table,"$"), "$");
followdone[ind] = 1;
for(int i = 1;i < maxnonterminals; i++)
{
fscanf(fp, "%s", buff);
ind = present(table,buff);
calculateFollow(ind);
}
fclose(fp);
return;
}
void printFollowSets(hashtable* table)
{
FILE* fp = fopen("nonterminals.txt", "r");
char buff[100];
for(int i=0; i<maxnonterminals; i++)
{
fscanf(fp, "%s", buff);
int ind = present(table, buff);
printf("%s --> ", buff);
ntort* temp = followSets[ind];
while(temp != NULL)
{
printf(" %s ", temp->str);
temp = temp->next;
}
printf("\n");
}
fclose(fp);
return;
}