-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfollow.c
More file actions
114 lines (96 loc) · 2.95 KB
/
follow.c
File metadata and controls
114 lines (96 loc) · 2.95 KB
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
/*
*Batch Number 6
*Abhinav Bhatia (2011A7PS371P)
*Mukul Bhutani (2011A7PS343P)
*/
#include "follow.h"
#include "compiler.h"
#include "first.h"
#include "definitions.h"
LinkList findRulesInWhichItComes(Symbol* NT, Grammar grammar)
{
LinkList ans = linkList_createNew(compareByReference);
LinkListNode* itr;
for (itr = grammar.rules.head; itr != NULL; itr = itr->next)
{
Rule* r = (Rule*)itr->element;
if (linkList_find(r->rhsSymbols, NT))
{
ans = linkList_add(ans, r);
}
}
return ans;
}
//followCalled is an array which stores whether Follow of the corresponding symbol had already been called in past
HashSet _follow(Symbol* NT, Grammar g, int* followCalled)
{
followCalled[NT->symbolID] = 1;
HashSet ans = set_create(g.terminalsSet.hashTable.size, compareByReference, hashFunctionForSymbols);
LinkList goodRules = findRulesInWhichItComes(NT, g);
LinkListNode* ruleItr;
for (ruleItr = goodRules.head; ruleItr != NULL; ruleItr = ruleItr->next)
{
Rule* r = (Rule*)ruleItr->element;
LinkListNode* NTlocationInRHS = linkList_find(r->rhsSymbols, NT);
if (NTlocationInRHS->next == NULL)
{
if (!followCalled[r->lhs->symbolID])
{
HashSet followOfLHS = _follow(r->lhs, g, followCalled);
HashSet unionedAns = set_union(ans, followOfLHS);
ans = set_clear(ans);
followOfLHS = set_clear(followOfLHS);
ans = unionedAns;
}
}
else
{
LinkList trailingSymbols = linkList_createNew(compareByReference);
LinkListNode* itr;
for (itr = NTlocationInRHS->next; itr != NULL; itr = itr->next)
{
trailingSymbols = linkList_addToTail(trailingSymbols, itr->element);
}
HashSet firstOfTrailingSymbols = first(trailingSymbols, g);
if (set_contains(firstOfTrailingSymbols, g.epsilon))
{
firstOfTrailingSymbols = set_remove(firstOfTrailingSymbols, g.epsilon);
HashSet unionedAns = set_union(ans, firstOfTrailingSymbols);
ans = set_clear(ans);
firstOfTrailingSymbols = set_clear(firstOfTrailingSymbols);
ans = unionedAns;
if (!followCalled[r->lhs->symbolID])
{
HashSet followOfLHS = _follow(r->lhs, g, followCalled);
unionedAns = set_union(ans, followOfLHS);
ans = set_clear(ans);
followOfLHS = set_clear(followOfLHS);
ans = unionedAns;
}
}
else
{
HashSet unionedAns = set_union(ans, firstOfTrailingSymbols);
ans = set_clear(ans);
firstOfTrailingSymbols = set_clear(firstOfTrailingSymbols);
ans = unionedAns;
}
trailingSymbols = linkList_clear(trailingSymbols);
}
}
goodRules = linkList_clear(goodRules);
return ans;
}
HashSet follow(Symbol* nonTerminal, Grammar g)
{
//followCalled is an array which stores whether Follow of the corresponding symbol had already been called in past
int* followCalled = (int*)malloc(g.nonTerminalSet.hashTable.size* sizeof(int));
int i;
for (i = 0; i < g.nonTerminalSet.hashTable.size; i++)
{
followCalled[i] = 0;
}
HashSet ans = _follow(nonTerminal, g, followCalled);
free(followCalled);
return ans;
}