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
40 /* commented out 2-29-90
41 static char * symtab[HASHSIZE] ;
47 char * symget(name
,newnode
,nodename
,nodenext
,symtab
,flag
)
49 char *(*newnode
)(), **(*nodename
)(), **(*nodenext
)() ;
51 int flag
; /* 1 is create if not there, 0 return NULL if not there */
60 index
= hash( name
) ;
67 while (*s
&& *t
&& *s
== *t
) {
82 *x
= (char *) malloc(strlen(name
)+1) ;
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 */
117 *p
= (char **) calloc( HASHSIZE
, sizeof( char * ) ) ;
124 This is a generic routine that, given a hash table pointer,
125 will traverse the hash table and apply a caller supplied
126 function to each entry
131 sym_traverse( ht
, nodenext
, f
)
133 char **(*nodenext
)() ;
138 for ( i
= 0 ; i
< HASHSIZE
; i
++ )
140 if ( ( p
= ht
[i
] ) != NULL
)
153 /**********************************************************************/
154 /**********************************************************************/
155 /**********************************************************************/
157 #ifdef COMMENTOUTSAMPLE
160 sample main program for symget() in the file symtab.c
168 struct symnode
*next
;
171 extern struct symnode
* symget() ;
176 struct symnode
* malloc() ;
177 return( malloc( sizeof( struct symnode
) ) ) ;
200 /**********************************************************************/
201 /**********************************************************************/
202 /**********************************************************************/