-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtab.c
102 lines (81 loc) · 2.57 KB
/
symtab.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
/**********************************************
CS515 Compilers
Project1
Spring 2016
**********************************************/
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include "symtab.h"
#define HASH_TABLE_SIZE 347
/* --- STATIC VARIABLES AND FUNCTIONS --- */
static
SymTabEntry **HashTable;
static
int hash(char *name) {
int i;
int hashValue = 1;
for (i=0; i < strlen(name); i++) {
hashValue = (hashValue * name[i]) % HASH_TABLE_SIZE;
}
return hashValue;
}
void
InitSymbolTable() {
int i;
int dummy;
HashTable = (SymTabEntry **) malloc (sizeof(SymTabEntry *) * HASH_TABLE_SIZE);
for (i=0; i < HASH_TABLE_SIZE; i++)
HashTable[i] = NULL;
}
/* Returns pointer to symbol table entry, if entry is found */
/* Otherwise, NULL is returned */
SymTabEntry *
lookup(char *name) {
int currentIndex;
int visitedSlots = 0;
currentIndex = hash(name);
while (HashTable[currentIndex] != NULL && visitedSlots < HASH_TABLE_SIZE) {
if (!strcmp(HashTable[currentIndex]->name, name) )
return HashTable[currentIndex];
currentIndex = (currentIndex + 1) % HASH_TABLE_SIZE;
visitedSlots++;
}
return NULL;
}
void
insert(char *name,int offset,int rowDim,int colDim) {
int currentIndex;
int visitedSlots = 0;
currentIndex = hash(name);
while (HashTable[currentIndex] != NULL && visitedSlots < HASH_TABLE_SIZE) {
if (!strcmp(HashTable[currentIndex]->name, name) )
printf("*** WARNING *** in function \"insert\": %s has already an entry\n", name);
currentIndex = (currentIndex + 1) % HASH_TABLE_SIZE;
visitedSlots++;
}
if (visitedSlots == HASH_TABLE_SIZE) {
printf("*** ERROR *** in function \"insert\": No more space for entry %s\n", name);
return;
}
HashTable[currentIndex] = (SymTabEntry *) malloc (sizeof(SymTabEntry));
HashTable[currentIndex]->name = (char *) malloc (strlen(name)+1);
strcpy(HashTable[currentIndex]->name, name);
HashTable[currentIndex]->offset = offset; //Offset of the identifier is stored;
HashTable[currentIndex]->rowDim = rowDim; //rowDimension of the Array;
HashTable[currentIndex]->colDim = colDim; //colDimension of the Array;
}
void
PrintSymbolTable() {
int i;
printf("\n --- Symbol Table ---------------\n\n");
for (i=0; i < HASH_TABLE_SIZE; i++) {
if (HashTable[i] != NULL) {
printf("\"%s\"", HashTable[i]->name);
printf("\t\"%d\" ",HashTable[i]->offset);
printf("\t%d",HashTable[i]->rowDim);
printf("\t%d\n",HashTable[i]->colDim);
}
}
printf("\n --------------------------------\n\n");
}