forked from muhamza/Jalebi-Compiler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
symbolTable.c
249 lines (223 loc) · 5.65 KB
/
symbolTable.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
#include <stdbool.h>
int scopeOut = -1;
int count = 0;
int stack[20];
int stackPointer = 0;
struct SymbolNode
{
char ident[50];
char datatype[50];
int lineno;
int scope;
int initialized;
struct SymbolNode *next;
};
struct Symbol
{
struct SymbolNode *head;
int symbolCount;
int closed;
int parentPointer;
};
struct Symbol symbolTable[100];
struct SymbolNode * CreateNode(char *ident, char *datatype, int lineno, int scope)
{
struct SymbolNode *node;
node = (struct SymbolNode *) malloc(sizeof(struct SymbolNode));
strcpy(node->ident, ident);
strcpy(node->datatype, datatype);
node->lineno = lineno;
node->scope = scope;
node->initialized = 0;
node->next = NULL;
return node;
}
void InsertSymbolTable(char *ident, char *datatype, int lineno, int scope){
int symbolValue = scope;
if(scope>=0){
struct SymbolNode *node = CreateNode(ident, datatype, lineno, scopeOut);
if (symbolTable[symbolValue].head == NULL){
symbolTable[symbolValue].head = node;
symbolTable[symbolValue].symbolCount = 1;
return;
}
struct SymbolNode *temp;
temp = symbolTable[symbolValue].head;
while (temp->next != NULL){
temp = temp->next;
}
temp->next = node;
symbolTable[symbolValue].symbolCount++;
}
return;
}
bool FindVariableSymbolTable(char *ident){
struct SymbolNode *temp;
for (int i = 0; i <= count; i++){
if (symbolTable[i].symbolCount == 0)
continue;
temp = symbolTable[i].head;
if (!temp)
continue;
while (temp != NULL){
if (strcmp(temp->ident, ident)==0){
return true;
}
temp = temp->next;
}
}
return false;
}
bool IsVariableInitialized(char *ident){
struct SymbolNode *temp;
for (int i = 0; i <= count; i++){
if (symbolTable[i].symbolCount == 0)
continue;
temp = symbolTable[i].head;
if (!temp)
continue;
while (temp != NULL){
if (strcmp(temp->ident, ident)==0 && temp->initialized==1){
return true;
}
temp = temp->next;
}
}
return false;
}
bool IsVariableInteger(char *ident){
struct SymbolNode *temp;
for (int i = 0; i <= count; i++){
if (symbolTable[i].symbolCount == 0)
continue;
temp = symbolTable[i].head;
if (!temp)
continue;
while (temp != NULL){
if (strcmp(temp->ident, ident)==0 && strcmp(temp->datatype, "hindsa")==0){
return true;
}
temp = temp->next;
}
}
return false;
}
void InitializeVariable(char* token){
struct SymbolNode *temp;
for (int i = 0; i <= count; i++){
if (symbolTable[i].symbolCount == 0)
continue;
temp = symbolTable[i].head;
if (!temp)
continue;
while (temp != NULL){
if (strcmp(temp->ident, token)==0){
temp->initialized = 1;
return;
}
temp = temp->next;
}
}
}
void RemoveSymbolTable(char *ident, int scope){
int symbolValue = scope;
if (symbolValue < 0){
return;
}
if (symbolTable[symbolValue].head == NULL){
return;
}
struct SymbolNode *temp, *temp2;
temp = symbolTable[symbolValue].head;
while (temp != NULL){
if (strcmp(temp->ident, ident) == 0){
printf("%s\n","deleted");
if(symbolTable[symbolValue].head == temp){
symbolTable[symbolValue].head = temp->next;
free(temp);
}
else if(temp == NULL){
temp2->next = NULL;
free(temp);
}
else{
temp2->next = temp->next;
free(temp);
}
break;
}
temp2 = temp;
temp = temp->next;
}
}
void PrintSymbolTable()
{
struct SymbolNode *temp;
printf("\n-----------------------------------------Symbol Table------------------------------------------------------------------------------------------\n");
printf("-----------------------------------------------------------------------------------------------------------------------------------------------");
printf("\n|\tP.Scope \t|\tToken \t|\tToken Type \t|\tLine no. \t|\tScope \t|\tInitialized? \n");
printf("-----------------------------------------------------------------------------------------------------------------------------------------------\n");
for (int i = 0; i <= count; i++){
if (symbolTable[i].symbolCount == 0)
continue;
printf("%d ", symbolTable[i].parentPointer);
temp = symbolTable[i].head;
if (!temp)
continue;
while (temp != NULL){
printf("\t%s\t", temp->ident);
printf("\t%s\t", temp->datatype);
printf("\t%d\t", temp->lineno);
printf("\t%d\t", temp->scope);
printf("\t%d\n", temp->initialized);
temp = temp->next;
}
}
printf("-----------------------------------------------------------------------------------------------------------------------------------------------\n");
return;
}
void InitializeCloseScope(int scope){
if (scope>=0){
symbolTable[scope].closed = 0;
symbolTable[scope].parentPointer = -1;
}
}
void CloseScope(int scope){
if (scope>=0){
symbolTable[scope].closed = 1;
}
else{
printf("Error: %s\n", "Scope cannot be less than 0.");
}
}
void AddScopeList(){
int scope = SecondStack();
if (scopeOut>0){
symbolTable[scopeOut].parentPointer = scope;
}
}
void PushStack(int scope){
stack[stackPointer] = scope;
stackPointer = stackPointer + 1;
}
int PopStack(){
stackPointer = stackPointer - 1;
if (stackPointer >=0){
return stack[stackPointer];
}
else{
return 0;
}
}
int HeadStack(){
return stack[stackPointer];
}
int SecondStack(){
return stack[stackPointer-2];
}
void PrintStack(){
for (int i=0; i<stackPointer; i++){
printf("%d ", stack[i]);
}
printf("\n");
}