[1] | 1 | /* symtab.c |
---|
| 2 | |
---|
| 3 | Symbol Table Handler -- Generic |
---|
| 4 | |
---|
| 5 | The routine symget() returns a pointer to a C structure matching a |
---|
| 6 | given lexeme. If the lexeme does not already exist in the symbol |
---|
| 7 | table, the routine will create a new symbol structure, store it, and |
---|
| 8 | then return a pointer to the newly created structure. |
---|
| 9 | |
---|
| 10 | It is up to the calling module to declare the symbol structure as |
---|
| 11 | well as several routines for manipulating the symbol structure. The |
---|
| 12 | routines are passed to symget as pointers. |
---|
| 13 | |
---|
| 14 | name type description |
---|
| 15 | |
---|
| 16 | newnode() *char returns a pointer to a symbol structure. |
---|
| 17 | |
---|
| 18 | nodename() **char retrieves the lexeme name from a symbol |
---|
| 19 | structure, returned as a pointer to a |
---|
| 20 | character array. |
---|
| 21 | |
---|
| 22 | nodenext() **char retrieves pointer to the next field of |
---|
| 23 | the symbol structure (the next field |
---|
| 24 | is itself a pointer to a symbol structure) |
---|
| 25 | |
---|
| 26 | For a sample main or calling program see the end of this file. |
---|
| 27 | |
---|
| 28 | **** |
---|
| 29 | REVISED 2-19-90. Added code to make hashtable interchangible. |
---|
| 30 | new routine: create_ht() creates new hashtable |
---|
| 31 | rev routine: symget() added parameter to pass hash table |
---|
| 32 | */ |
---|
| 33 | |
---|
| 34 | #include <stdio.h> |
---|
| 35 | #include <string.h> |
---|
| 36 | #ifndef _WIN32 |
---|
| 37 | # include <strings.h> |
---|
| 38 | #endif |
---|
| 39 | |
---|
| 40 | #define HASHSIZE 1024 |
---|
| 41 | |
---|
| 42 | /* commented out 2-29-90 |
---|
| 43 | static char * symtab[HASHSIZE] ; |
---|
| 44 | */ |
---|
| 45 | |
---|
| 46 | void * malloc() ; |
---|
| 47 | void * calloc() ; |
---|
| 48 | |
---|
| 49 | char * symget(name,newnode,nodename,nodenext,symtab,flag) |
---|
| 50 | char *name ; |
---|
| 51 | char *(*newnode)(), **(*nodename)(), **(*nodenext)() ; |
---|
| 52 | char *symtab[] ; |
---|
| 53 | int flag ; /* 1 is create if not there, 0 return NULL if not there */ |
---|
| 54 | { |
---|
| 55 | int index ; |
---|
| 56 | int found ; |
---|
| 57 | register char *s ; |
---|
| 58 | register char *t ; |
---|
| 59 | char **x ; |
---|
| 60 | char *p ; |
---|
| 61 | |
---|
| 62 | index = hash( name ) ; |
---|
| 63 | p = symtab[index] ; |
---|
| 64 | found = 0 ; |
---|
| 65 | |
---|
| 66 | while (p) { |
---|
| 67 | s = name ; |
---|
| 68 | t = *(*nodename)(p) ; |
---|
| 69 | while (*s && *t && *s == *t ) { |
---|
| 70 | s++ ; |
---|
| 71 | t++ ; |
---|
| 72 | } |
---|
| 73 | if (!*s && !*t) { |
---|
| 74 | found = 1 ; |
---|
| 75 | break ; |
---|
| 76 | } |
---|
| 77 | p = *(*nodenext)(p) ; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | if (!found ) { |
---|
| 81 | if (flag ) { |
---|
| 82 | p = (*newnode)() ; |
---|
| 83 | x = (*nodename)(p) ; |
---|
| 84 | *x = (char *) malloc(strlen(name)+1) ; |
---|
| 85 | strcpy(*x,name) ; |
---|
| 86 | x = (*nodenext)(p) ; |
---|
| 87 | *x = symtab[index] ; |
---|
| 88 | symtab[index] = p ; |
---|
| 89 | } else { |
---|
| 90 | return(NULL) ; |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | return(p) ; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | int |
---|
| 98 | hash(name) |
---|
| 99 | char * name ; |
---|
| 100 | { |
---|
| 101 | register int result = 0 ; |
---|
| 102 | register char * p = name ; |
---|
| 103 | |
---|
| 104 | while (*p) |
---|
| 105 | result = 3*result + (int)*p++ ; |
---|
| 106 | |
---|
| 107 | result = result % HASHSIZE ; |
---|
| 108 | while (result < 0) |
---|
| 109 | result = result + HASHSIZE ; |
---|
| 110 | return(result) ; |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | |
---|
| 114 | /* added 2-19-90, attaches a new hash table to pointer */ |
---|
| 115 | |
---|
| 116 | int |
---|
| 117 | create_ht( p ) |
---|
| 118 | char *** p ; |
---|
| 119 | { |
---|
| 120 | *p = (char **) calloc( HASHSIZE , sizeof( char * ) ) ; |
---|
| 121 | return(0) ; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | /* added 4-15-92. |
---|
| 126 | |
---|
| 127 | This is a generic routine that, given a hash table pointer, |
---|
| 128 | will traverse the hash table and apply a caller supplied |
---|
| 129 | function to each entry |
---|
| 130 | |
---|
| 131 | */ |
---|
| 132 | |
---|
| 133 | int |
---|
| 134 | sym_traverse( ht, nodenext, f ) |
---|
| 135 | char *ht[] ; |
---|
| 136 | char **(*nodenext)() ; |
---|
| 137 | void (*f)() ; |
---|
| 138 | { |
---|
| 139 | char * p, **x ; |
---|
| 140 | int i ; |
---|
| 141 | for ( i = 0 ; i < HASHSIZE ; i++ ) |
---|
| 142 | { |
---|
| 143 | if ( ( p = ht[i] ) != NULL ) |
---|
| 144 | { |
---|
| 145 | while ( p ) |
---|
| 146 | { |
---|
| 147 | (*f)(p) ; |
---|
| 148 | x = (*nodenext)(p) ; |
---|
| 149 | p = *x ; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | return(0) ; |
---|
| 154 | } |
---|
| 155 | |
---|
| 156 | /**********************************************************************/ |
---|
| 157 | /**********************************************************************/ |
---|
| 158 | /**********************************************************************/ |
---|
| 159 | |
---|
| 160 | #ifdef COMMENTOUTSAMPLE |
---|
| 161 | /* sample_main.c |
---|
| 162 | |
---|
| 163 | sample main program for symget() in the file symtab.c |
---|
| 164 | |
---|
| 165 | */ |
---|
| 166 | |
---|
| 167 | #include <stdio.h> |
---|
| 168 | |
---|
| 169 | struct symnode { |
---|
| 170 | char * name ; |
---|
| 171 | struct symnode *next ; |
---|
| 172 | } ; |
---|
| 173 | |
---|
| 174 | extern struct symnode * symget() ; |
---|
| 175 | |
---|
| 176 | struct symnode * |
---|
| 177 | newnode() |
---|
| 178 | { |
---|
| 179 | struct symnode * malloc() ; |
---|
| 180 | return( malloc( sizeof( struct symnode ) ) ) ; |
---|
| 181 | } |
---|
| 182 | |
---|
| 183 | char ** |
---|
| 184 | nodename(p) |
---|
| 185 | struct symnode *p ; |
---|
| 186 | { |
---|
| 187 | char ** x ; |
---|
| 188 | x = &(p->name) ; |
---|
| 189 | return( x ) ; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | struct symnode ** |
---|
| 193 | nodenext(p) |
---|
| 194 | struct symnode *p ; |
---|
| 195 | { |
---|
| 196 | struct symnode **x ; |
---|
| 197 | x = &(p->next) ; |
---|
| 198 | return( x ) ; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | #endif |
---|
| 202 | |
---|
| 203 | /**********************************************************************/ |
---|
| 204 | /**********************************************************************/ |
---|
| 205 | /**********************************************************************/ |
---|
| 206 | |
---|