-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymboltable.c
More file actions
124 lines (105 loc) · 2.5 KB
/
symboltable.c
File metadata and controls
124 lines (105 loc) · 2.5 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
113
114
115
116
117
118
119
120
121
122
123
124
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#include<math.h>
#include"header.h"
#define TABLE_SIZE 1024
symtab * hash_table[TABLE_SIZE];
extern int scope;
extern int STRUCT_DEC;
extern int linenumber;
int HASH(char * str){
int idx=0;
while(*str){
idx = idx << 1;
idx+=*str;
str++; }
return (idx & (TABLE_SIZE-1));
}
symtab * lookup(char *name){
int hash_key;
symtab* symptr;
if(!name)
return NULL;
hash_key=HASH(name);
symptr=hash_table[hash_key];
while(symptr){
if(!(strcmp(name,symptr->lexeme)))
return symptr;
symptr=symptr->front;
}
return NULL;
}
void insert(char *name,TYPE type,void * P,IS_TYPE_DEF TypeDef){
int hash_key;
symtab* ptr;
symtab* symptr=(symtab*)malloc(sizeof(symtab));
hash_key=HASH(name);
ptr=hash_table[hash_key];
if(ptr==NULL){
/*first entry for this hash_key*/
hash_table[hash_key]=symptr;
symptr->front=NULL;
symptr->back=symptr;
}
else{
symptr->front=ptr;
ptr->back=symptr;
symptr->back=symptr;
hash_table[hash_key]=symptr;
}
symptr->scope=scope;
symptr->type=type;
symptr->type_when_def=TypeDef;
symptr->lexeme=name;
symptr->line=linenumber;
switch (type){
case ARR_:
symptr->symtab_u.st_arr=P;
break;
case STR_:
if(STRUCT_DEC)
symptr->scope=-scope;
symptr->symtab_u.st_struct=P;
break;
case FUNC_:
symptr->symtab_u.st_func=P;
symptr->scope=0;
break;
case STR_VAR_:
symptr->symtab_u.type_name=P;
break;
case TYPEDEF_:
switch (TypeDef){
case TYPEDEF_ARR:
symptr->symtab_u.st_arr=P;
break;
case TYPEDEF_STR:
symptr->symtab_u.type_name=P;
break;
default:
break;
}
default:
break;
}
}
int delete_scope(int scp){
int i;
symtab * ptr,*ptr1,*ptr2;
for(i=0;i<TABLE_SIZE;i++){
ptr=hash_table[i];
if(ptr==NULL)
continue;
ptr1=ptr;
while((ptr)&&(abs(ptr->scope)==(scp)))
ptr=ptr->front;
if(ptr1==ptr)
continue;
hash_table[i]=ptr;
if(ptr)
ptr->back=ptr;
}
return 1;
}