-
Notifications
You must be signed in to change notification settings - Fork 0
/
symtable.c
237 lines (227 loc) · 8.44 KB
/
symtable.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symtable.h"
int hash( char * c ){
int i = 0;
int sum = 0;
int len = strlen(c);
for (i=0 ; i<len; i++){
sum += (int)c[i];
}
return sum%TABLE_SIZE;
}
struct entry * lookUp( char *c ){
int index = hash(c);
struct entry * nav;
nav = &hashTable[index];
if ( nav->key != NULL ){
if ( strcmp(c,nav->name) == 0 ){
return nav;
}else{
nav = nav->next;
while ( nav != NULL ){
nav = nav->next;
if ( strcmp(c,nav->name) == 0 ){
return nav;
}
}
}
}
return NULL;
}
int getClassCount(char * str){
int i;
int count = 0;
struct entry * nav;
for ( i=0; i < TABLE_SIZE; i++){
if ( hashTable[i].key != NULL ){
if ( strcmp(str, hashTable[i].sclass) == 0 ){
count ++;
}
}
if ( hashTable[i].next != NULL){
nav = hashTable[i].next;
while( nav != NULL ){
if ( strcmp(str, hashTable[i].sclass) == 0 ){
count ++;
}
nav = nav->next;
}
}
}
return count;
}
void printTable(){
int i;
struct entry * nav;
for ( i=0; i < TABLE_SIZE; i++){
if ( hashTable[i].key != NULL ){
printf( "Key: %s | Name: %s | Class: %s | Value: %s \n", hashTable[i].key, hashTable[i].name, hashTable[i].sclass, hashTable[i].value);
}
if ( hashTable[i].next != NULL){
nav = hashTable[i].next;
while( nav != NULL ){
printf( "Key: %s | Name: %s | Class: %s | Value: %s \n", nav->key, nav->name, nav->sclass, nav->value);
nav = nav->next;
}
}
}
}
void insertEntry( char *c, char *type ){
struct entry * entryAddress;
struct entry * navAddress;
int index = hash(c); //Generating the hash
entryAddress = &hashTable[index];
if ( entryAddress->key != NULL ){ //Here it searches for a available spot in the table
if ( strcmp(entryAddress->name,c) == 0 ){
return; //repeated entry
}
navAddress = entryAddress;
entryAddress = entryAddress->next;
while ( entryAddress != NULL ){
if ( strcmp(entryAddress->name,c) == 0 ){
return; //repeated entry
}
navAddress = entryAddress;
entryAddress = entryAddress->next;
}
entryAddress = malloc( sizeof (struct entry) ); //entryAdress represents where the token will be inserted
navAddress->next = entryAddress; //Adding one more chain to the linked list
}
if ( strcmp(type,"symbol") == 0 ){
entryAddress->key = malloc( 16*sizeof(char) );
entryAddress->sclass = malloc( 16*sizeof(char) );
entryAddress->name = malloc( 16*sizeof(char) );
entryAddress->value = malloc( 16*sizeof(char) );
sprintf(entryAddress->key, "%d", index);
sprintf(entryAddress->sclass, "%s", "Symbol");
sprintf(entryAddress->name, "%s", c);
sprintf(entryAddress->value, "%s", "");
sprintf(token, "%s", c);
return;
}
if ( strcmp(type,"number") == 0 ){
entryAddress->key = malloc( 16*sizeof(char) );
entryAddress->sclass = malloc( 16*sizeof(char) );
entryAddress->name = malloc( 19*sizeof(char) );
entryAddress->value = malloc( 19*sizeof(char) );
sprintf(entryAddress->key, "%d", index);
sprintf(entryAddress->sclass, "%s", "Number");
sprintf(entryAddress->name, "%s", c);
sprintf(entryAddress->value, "%s", c);
sprintf(token, "%s", c);
return;
}
if ( strcmp(type,"keyword") == 0 ){
entryAddress->key = malloc( 16*sizeof(char) );
entryAddress->sclass = malloc( 16*sizeof(char) );
entryAddress->name = malloc( 32*sizeof(char) );
entryAddress->value = malloc( 16*sizeof(char) );
sprintf(entryAddress->key, "%d", index);
sprintf(entryAddress->sclass, "%s", "Keyword");
sprintf(entryAddress->name, "%s", c);
sprintf(entryAddress->value, "%s", "");
sprintf(token, "%s", c);
return;
}
if ( strcmp(type,"type") == 0 ){
entryAddress->key = malloc( 16*sizeof(char) );
entryAddress->sclass = malloc( 16*sizeof(char) );
entryAddress->name = malloc( 16*sizeof(char) );
entryAddress->value = malloc( 16*sizeof(char) );
sprintf(entryAddress->key, "%d", index);
sprintf(entryAddress->sclass, "%s", "Type");
sprintf(entryAddress->name, "%s", c);
sprintf(entryAddress->value, "%s", "");
sprintf(token, "%s", c);
return;
}
if ( strcmp(type,"variable") == 0 ){
entryAddress->key = malloc( 16*sizeof(char) );
entryAddress->sclass = malloc( 16*sizeof(char) );
entryAddress->name = malloc( 32*sizeof(char) );
entryAddress->value = malloc( 16*sizeof(char) );
sprintf(entryAddress->key, "%d", index);
sprintf(entryAddress->sclass, "%s", "Variable");
sprintf(entryAddress->name, "%s", c);
sprintf(entryAddress->value, "%s", "");
sprintf(token, "%s", c);
return;
}
if ( strcmp(type,"grammar") == 0 ){
entryAddress->key = malloc( 16*sizeof(char) );
entryAddress->sclass = malloc( 16*sizeof(char) );
entryAddress->name = malloc( 32*sizeof(char) );
entryAddress->value = malloc( 16*sizeof(char) );
sprintf(entryAddress->key, "%d", index);
sprintf(entryAddress->sclass, "%s", "Grammar");
sprintf(entryAddress->name, "%s", c);
sprintf(entryAddress->value, "%s", "");
sprintf(token, "%s", c);
return;
}
}
/*
void printTable(int format){
int temp = 0;
struct entry * entryNav; //Hashtable navigation pointer
switch (format){
case 1:
for ( temp=0; temp < TABLE_SIZE; temp++){
if ( hashTable[temp].key != NULL ){
fprintf( fpTable ,"Key: %s | Name: %s | Class: %s | Value: %s \n", hashTable[temp].key, hashTable[temp].name, hashTable[temp].sclass, hashTable[temp].value);
}
if ( hashTable[temp].next != NULL){
entryNav = hashTable[temp].next;
while( entryNav != NULL ){
fprintf( fpTable ,"Key: %s | Name: %s | Class: %s | Value: %s \n", entryNav->key, entryNav->name, entryNav->sclass, entryNav->value);
entryNav = entryNav->next;
}
}
}
break;
case 2:
fprintf( fpTable ,"Key,Name,Class,Value\n");
for ( temp=0; temp < TABLE_SIZE; temp++){
if ( hashTable[temp].key != NULL ){
fprintf( fpTable ,"%s,%s,%s,%s\n", hashTable[temp].key, hashTable[temp].name, hashTable[temp].sclass, hashTable[temp].value);
}
if ( hashTable[temp].next != NULL){
entryNav = hashTable[temp].next;
while( entryNav != NULL ){
fprintf( fpTable ,"%s,%s,%s,%s\n", entryNav->key, (char *)entryNav->name, entryNav->sclass, entryNav->value);
entryNav = entryNav->next;
}
}
}
break;
}
}
*/
void freeTable(){
struct entry * entryNav; //Hashtable navigation pointer
struct entry * entryNavPrev; //Hashtable delayed navigation pointer
int temp = 0;
for ( temp=0; temp < TABLE_SIZE; temp++){
if ( hashTable[temp].key != NULL ){
free(hashTable[temp].key);
free(hashTable[temp].name);
free(hashTable[temp].sclass);
free(hashTable[temp].value);
}
if ( hashTable[temp].next != NULL){
entryNav = hashTable[temp].next;
while( entryNav != NULL ){
char * freeString = (char *) entryNav->key;
free(entryNav->key);
free(entryNav->name);
free(entryNav->sclass);
free(entryNav->value);
entryNavPrev = entryNav;
entryNav = entryNav->next;
free(entryNavPrev);
}
}
}
}