-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolHash.c
122 lines (110 loc) · 3.42 KB
/
symbolHash.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
// Group Number: 31
// MADHUR PANWAR 2016B4A70933P
// TUSSANK GUPTA 2016B3A70528P
// SALMAAN SHAHID 2016B4A70580P
// APURV BAJAJ 2016B3A70549P
// HASAN NAQVI 2016B5A70452P
#include "symbolHash.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
symTableNode* makeSymTableNode(char* lexeme, union funcVar fvinfo){
symTableNode *newNode = (symTableNode*)(malloc(sizeof(symTableNode)));
strcpy(newNode->lexeme, lexeme);
newNode->info = fvinfo;
newNode->next = NULL;
return newNode;
}
//Add the given function/variable details to the symbol table in st and return the address to the entry's union
union funcVar *stAdd(char *lexeme, union funcVar fvinfo , symbolTable *st){
int slot = symbolTableHashFunction(lexeme);
symTableNode *hashNode = (st->tb)[slot];
symTableNode *infoNode = makeSymTableNode(lexeme, fvinfo);
if(hashNode != NULL){
while(hashNode->next != NULL){
hashNode = hashNode->next;
}
hashNode->next = infoNode;
}
else{
(st->tb)[slot] = infoNode;
}
return &(infoNode->info);
}
//Check if the given key is in the current symbol table or not
symTableNode *stSearchCurrent(char *lexeme, symbolTable *st){
// assuming st is not NULL
if(st == NULL){
return NULL;
}
int slot = symbolTableHashFunction(lexeme);
symTableNode *curr_node = (st->tb)[slot];
while(curr_node != NULL){
if(equals(curr_node->lexeme, lexeme)){
return curr_node;
}
curr_node = curr_node->next;
}
return NULL;
}
//Check if the given key is in the current scope or not
// (check current ST and all the parent STs. Don't search in function input and output params)
//take help of stSearchCurrent(...)
symTableNode *stSearch(char *lexeme, symbolTable *st){
// assuming st is not NULL
if(st == NULL){
return NULL;
}
symTableNode *curr = NULL;
while(st != NULL){
curr = stSearchCurrent(lexeme, st);
if(curr){
return curr;
}
st = st->parent;
}
return NULL;
}
//return the address of the data element after finding the required entry in the table
// return NULL if not found
symVarInfo *stGetVarInfoCurrent(char *lexeme, symbolTable *st){
symTableNode *symInfoNode = stSearchCurrent(lexeme, st);
if(symInfoNode){
return &(symInfoNode->info.var);
}
return NULL;
}
symFuncInfo *stGetFuncInfo(char *lexeme, symbolTable *funcTable){
// assumption: 'funcTable' is the funcTable declared globally in symbolTable.c
symTableNode *symInfoNode = stSearchCurrent(lexeme, funcTable);
if(symInfoNode){
return &(symInfoNode->info.func);
}
return NULL;
}
symVarInfo *stGetVarInfo(char *lexeme, symbolTable *st){
symTableNode *symInfoNode = stSearch(lexeme, st);
if(symInfoNode){
return &(symInfoNode->info.var);
}
return NULL;
}
//UTILISE THE SYMBOLHASHFUNCTION from the hash.h header
void initSymbolTable(symbolTable *st){
(st->parent) = NULL;
(st->headChild) = NULL;
(st->lastChild) = NULL;
(st->next) = NULL;
(st->startNode) = NULL;
(st->scopeSize) = 0;
strcpy(st->funcName,"<EMPTY>");
for(int i=0; i<SYMBOL_TABLE_SIZE; i++){
(st->tb)[i] = NULL;
}
}
//create new symbol table and initialize entries to NULL
symbolTable *createSymbolTable(){
symbolTable *st = (symbolTable*)(malloc(sizeof(symbolTable)));
initSymbolTable(st);
return st;
}