-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymbolTable.h
88 lines (80 loc) · 2.05 KB
/
symbolTable.h
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
int i;
struct template{
char name[50];
char token[50];
char type[50];
char scope[10];
char stack[10];
int dimension;
int level;
struct template *next;
}*table[100];
int hashFunction(char *str,char *scopeid){
int sum = 0;
for(i=0;str[i]!='\0';i++){
sum += str[i];
}
for(i=0;scopeid[i]!='\0';++i)
sum += scopeid[i];
return sum % 100;
}
struct template* searchIndex(int index,char* str){
struct template *head = table[index];
struct template *temp;
while(head != NULL){
if(head->next == NULL)
temp = head;
if(strcmp(head->name,str)==0)
return NULL;
else
head = head->next;
}
return temp;
}
struct template *makenode(char *name,char *token, char *type, char *scope, char *stack,int dim){
struct template *entry = (struct template*) malloc(sizeof(struct template));
strcpy(entry->name,name);
strcpy(entry->token,token);
strcpy(entry->scope,scope);
strcpy(entry->type,type);
strcpy(entry->stack,stack);
entry->level = strlen(stack);
entry->dimension = dim;
entry->next = NULL;
return entry;
}
void insertHash(char *str,char *token,char *type,char *scope, char *stack, int dim){
int index = hashFunction(str,stack);
if(table[index] == NULL){
table[index] = makenode(str,token,type,scope,stack,dim);
}
else{
struct template *temp = searchIndex(index,str);
if(temp != NULL){
temp->next = makenode(str,token,type,scope,stack,dim);
}
}
}
void display(){
struct template *ptr;
printf("(name,\ttoken,\t\ttype,\tscope,\tscope-id,\tlevel)\n\n");
for(i=0;i<100;++i){
if(table[i] != NULL){
ptr = table[i];
while(ptr != NULL){
if(ptr->dimension == 0)
printf("(%s,\t%s,\t%s,\t%s,\t%s,\t%d)\t", ptr->name,ptr->token,ptr->type,ptr->scope,ptr->stack,ptr->level);
else
printf("(%s,\t%s,\t%s,\t%s,\t%s,\t%d,\t%d)\t", ptr->name,ptr->token,ptr->type,ptr->scope,ptr->stack,ptr->level,ptr->dimension);
ptr = ptr->next;
}printf("\n");
}
}
}
// putsym(char *name,char *token,int type,int scope){
// if(type == 1)
// insertHash(name,token,"int","global");
// }