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
42 /* commented out 2-29-90
43 static char * symtab[HASHSIZE] ;
49 char * symget(name
,newnode
,nodename
,nodenext
,symtab
,flag
)
51 char *(*newnode
)(), **(*nodename
)(), **(*nodenext
)() ;
53 int flag
; /* 1 is create if not there, 0 return NULL if not there */
62 index
= hash( name
) ;
69 while (*s
&& *t
&& *s
== *t
) {
84 *x
= (char *) malloc(strlen(name
)+1) ;
101 register int result
= 0 ;
102 register char * p
= name
;
105 result
= 3*result
+ (int)*p
++ ;
107 result
= result
% HASHSIZE
;
109 result
= result
+ HASHSIZE
;
114 /* added 2-19-90, attaches a new hash table to pointer */
120 *p
= (char **) calloc( HASHSIZE
, sizeof( char * ) ) ;
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
134 sym_traverse( ht
, nodenext
, f
)
136 char **(*nodenext
)() ;
141 for ( i
= 0 ; i
< HASHSIZE
; i
++ )
143 if ( ( p
= ht
[i
] ) != NULL
)
156 /**********************************************************************/
157 /**********************************************************************/
158 /**********************************************************************/
160 #ifdef COMMENTOUTSAMPLE
163 sample main program for symget() in the file symtab.c
171 struct symnode
*next
;
174 extern struct symnode
* symget() ;
179 struct symnode
* malloc() ;
180 return( malloc( sizeof( struct symnode
) ) ) ;
203 /**********************************************************************/
204 /**********************************************************************/
205 /**********************************************************************/