3 Symbol Table Handler -- Generic
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.
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.
16 newnode() *char returns a pointer to a symbol structure.
18 nodename() **char retrieves the lexeme name from a symbol
19 structure, returned as a pointer to a
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)
26 For a sample main or calling program see the end of this file.
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
44 /* commented out 2-29-90
45 static char * symtab[HASHSIZE] ;
52 char * symget(char *name
,char *(*newnode
)(),char **(*nodename
)(char *),char **(*nodenext
)(char *), char *symtab
[], int flag
) /* flag = 1 is create if not there, 0 return NULL if not there */
61 index
= hash( name
) ;
68 while (*s
&& *t
&& *s
== *t
) {
83 *x
= (char *) malloc(strlen(name
)+1) ;
96 int hash(char * name
)
98 register int result
= 0 ;
99 register char * p
= name
;
102 result
= 3*result
+ (int)*p
++ ;
104 result
= result
% HASHSIZE
;
106 result
= result
+ HASHSIZE
;
111 /* added 2-19-90, attaches a new hash table to pointer */
113 int create_ht(char *** p
)
115 *p
= (char **) calloc( HASHSIZE
, sizeof( char * ) ) ;
122 This is a generic routine that, given a hash table pointer,
123 will traverse the hash table and apply a caller supplied
124 function to each entry
128 int sym_traverse( char *ht
[] , char **(*nodenext
)(char *), void (*f
)(char *) )
132 for ( i
= 0 ; i
< HASHSIZE
; i
++ )
134 if ( ( p
= ht
[i
] ) != NULL
)
147 /**********************************************************************/
148 /**********************************************************************/
149 /**********************************************************************/
151 #ifdef COMMENTOUTSAMPLE
154 sample main program for symget() in the file symtab.c
162 struct symnode
*next
;
165 extern struct symnode
* symget() ;
167 struct symnode
* newnode()
169 struct symnode
* malloc() ;
170 return( malloc( sizeof( struct symnode
) ) ) ;
173 char ** nodename(struct symnode
*p
)
180 struct symnode
** nodenext(struct symnode
*p
)
189 /**********************************************************************/
190 /**********************************************************************/
191 /**********************************************************************/