1 /* sym - symbol table routines */
4 * Copyright (c) 1990 The Regents of the University of California.
7 * This code is derived from software contributed to Berkeley by
10 * The United States Government has rights in this work pursuant
11 * to contract no. DE-AC03-76SF00098 between the United States
12 * Department of Energy and the University of California.
14 * Redistribution and use in source and binary forms are permitted provided
15 * that: (1) source distributions retain this entire copyright notice and
16 * comment, and (2) distributions including binaries display the following
17 * acknowledgement: ``This product includes software developed by the
18 * University of California, Berkeley and its contributors'' in the
19 * documentation or other materials provided with the distribution and in
20 * all advertising materials mentioning features or use of this software.
21 * Neither the name of the University nor the names of its contributors may
22 * be used to endorse or promote products derived from this software without
23 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
25 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
31 "@(#) $Id: sym.c,v 1.2 1994/06/24 10:57:28 ceriel Exp $ (LBL)";
37 /* declare functions that have forward references */
39 int hashfunct PROTO((register char[], int));
42 struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
43 struct hash_entry *sctbl[START_COND_HASH_SIZE];
44 struct hash_entry *ccltab[CCL_HASH_SIZE];
46 struct hash_entry *findsym();
49 /* addsym - add symbol and definitions to symbol table
52 * char sym[], *str_def;
56 * 0 / -1 = addsym( sym, def, int_def, table, table_size );
58 * -1 is returned if the symbol already exists, and the change not made.
61 int addsym( sym, str_def, int_def, table, table_size )
69 int hash_val = hashfunct( sym, table_size );
70 register struct hash_entry *sym_entry = table[hash_val];
71 register struct hash_entry *new_entry;
72 register struct hash_entry *successor;
76 if ( ! strcmp( sym, sym_entry->name ) )
77 { /* entry already exists */
81 sym_entry = sym_entry->next;
84 /* create new entry */
85 new_entry = (struct hash_entry *) malloc( sizeof( struct hash_entry ) );
87 if ( new_entry == NULL )
88 flexfatal( "symbol table memory allocation failed" );
90 if ( (successor = table[hash_val]) )
92 new_entry->next = successor;
93 successor->prev = new_entry;
96 new_entry->next = NULL;
98 new_entry->prev = NULL;
99 new_entry->name = sym;
100 new_entry->str_val = str_def;
101 new_entry->int_val = int_def;
103 table[hash_val] = new_entry;
109 /* cclinstal - save the text of a character class
114 * cclinstal( ccltxt, cclnum );
117 void cclinstal( ccltxt, cclnum )
122 /* we don't bother checking the return status because we are not called
123 * unless the symbol is new
125 Char *copy_unsigned_string();
127 (void) addsym( (char *) copy_unsigned_string( ccltxt ), (char *) 0, cclnum,
128 ccltab, CCL_HASH_SIZE );
132 /* ccllookup - lookup the number associated with character class text
136 * int ccllookup, cclval;
137 * cclval/0 = ccllookup( ccltxt );
140 int ccllookup( ccltxt )
144 return ( findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val );
148 /* findsym - find symbol in symbol table
154 * struct hash_entry *sym_entry, *findsym();
155 * sym_entry = findsym( sym, table, table_size );
158 struct hash_entry *findsym( sym, table, table_size )
164 register struct hash_entry *sym_entry = table[hashfunct( sym, table_size )];
165 static struct hash_entry empty_entry =
167 (struct hash_entry *) 0, (struct hash_entry *) 0, NULL, NULL, 0,
172 if ( ! strcmp( sym, sym_entry->name ) )
173 return ( sym_entry );
174 sym_entry = sym_entry->next;
177 return ( &empty_entry );
181 /* hashfunct - compute the hash value for "str" and hash size "hash_size"
185 * int hash_size, hash_val;
186 * hash_val = hashfunct( str, hash_size );
189 int hashfunct( str, hash_size )
194 register int hashval;
200 while ( str[locstr] )
201 hashval = ((hashval << 1) + str[locstr++]) % hash_size;
207 /* ndinstal - install a name definition
212 * ndinstal( nd, def );
215 void ndinstal( nd, def )
221 Char *copy_unsigned_string();
223 if ( addsym( copy_string( nd ), (char *) copy_unsigned_string( def ), 0,
224 ndtbl, NAME_TABLE_HASH_SIZE ) )
225 synerr( "name defined twice" );
229 /* ndlookup - lookup a name definition
234 * def/NULL = ndlookup( nd );
241 return ( (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val );
245 /* scinstal - make a start condition
250 * scinstal( str, xcluflg );
253 * the start condition is Exclusive if xcluflg is true
256 void scinstal( str, xcluflg )
263 /* bit of a hack. We know how the default start-condition is
264 * declared, and don't put out a define for it, because it
265 * would come out as "#define 0 1"
267 /* actually, this is no longer the case. The default start-condition
268 * is now called "INITIAL". But we keep the following for the sake
269 * of future robustness.
272 if ( strcmp( str, "0" ) )
273 printf( "#define %s %d\n", str, lastsc );
275 if ( ++lastsc >= current_max_scs )
277 current_max_scs += MAX_SCS_INCREMENT;
281 scset = reallocate_integer_array( scset, current_max_scs );
282 scbol = reallocate_integer_array( scbol, current_max_scs );
283 scxclu = reallocate_integer_array( scxclu, current_max_scs );
284 sceof = reallocate_integer_array( sceof, current_max_scs );
285 scname = reallocate_char_ptr_array( scname, current_max_scs );
286 actvsc = reallocate_integer_array( actvsc, current_max_scs );
289 scname[lastsc] = copy_string( str );
291 if ( addsym( scname[lastsc], (char *) 0, lastsc,
292 sctbl, START_COND_HASH_SIZE ) )
293 format_pinpoint_message( "start condition %s declared twice", str );
295 scset[lastsc] = mkstate( SYM_EPSILON );
296 scbol[lastsc] = mkstate( SYM_EPSILON );
297 scxclu[lastsc] = xcluflg;
298 sceof[lastsc] = false;
302 /* sclookup - lookup the number associated with a start condition
307 * scnum/0 = sclookup( str );
314 return ( findsym( str, sctbl, START_COND_HASH_SIZE )->int_val );