No empty .Rs/.Re
[netbsd-mini2440.git] / usr.bin / lex / sym.c
blob7c1c8be6e4914cc83372d72cdd9a20a5f59b1917
1 /* sym - symbol table routines */
3 /*-
4 * Copyright (c) 1990 The Regents of the University of California.
5 * All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Vern Paxson.
9 *
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.
29 /* $NetBSD: sym.c,v 1.10 1998/02/22 12:08:35 christos Exp $ */
31 #include "flexdef.h"
34 /* declare functions that have forward references */
36 int hashfunct PROTO((register char[], int));
39 struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
40 struct hash_entry *sctbl[START_COND_HASH_SIZE];
41 struct hash_entry *ccltab[CCL_HASH_SIZE];
43 /* addsym - add symbol and definitions to symbol table
45 * -1 is returned if the symbol already exists, and the change not made.
48 int addsym( sym, str_def, int_def, table, table_size )
49 register char sym[];
50 char *str_def;
51 int int_def;
52 hash_table table;
53 int table_size;
55 int hash_val = hashfunct( sym, table_size );
56 register struct hash_entry *sym_entry = table[hash_val];
57 register struct hash_entry *new_entry;
58 register struct hash_entry *successor;
60 while ( sym_entry )
62 if ( ! strcmp( sym, sym_entry->name ) )
63 { /* entry already exists */
64 return -1;
67 sym_entry = sym_entry->next;
70 /* create new entry */
71 new_entry = (struct hash_entry *)
72 flex_alloc( sizeof( struct hash_entry ) );
74 if ( new_entry == NULL )
75 flexfatal( _( "symbol table memory allocation failed" ) );
77 if ( (successor = table[hash_val]) != 0 )
79 new_entry->next = successor;
80 successor->prev = new_entry;
82 else
83 new_entry->next = NULL;
85 new_entry->prev = NULL;
86 new_entry->name = sym;
87 new_entry->str_val = str_def;
88 new_entry->int_val = int_def;
90 table[hash_val] = new_entry;
92 return 0;
96 /* cclinstal - save the text of a character class */
98 void cclinstal( ccltxt, cclnum )
99 Char ccltxt[];
100 int cclnum;
102 /* We don't bother checking the return status because we are not
103 * called unless the symbol is new.
106 (void) addsym( (char *) copy_unsigned_string( ccltxt ),
107 (char *) 0, cclnum,
108 ccltab, CCL_HASH_SIZE );
112 /* ccllookup - lookup the number associated with character class text
114 * Returns 0 if there's no CCL associated with the text.
117 int ccllookup( ccltxt )
118 Char ccltxt[];
120 return findsym( (char *) ccltxt, ccltab, CCL_HASH_SIZE )->int_val;
124 /* findsym - find symbol in symbol table */
126 struct hash_entry *findsym( sym, table, table_size )
127 register char sym[];
128 hash_table table;
129 int table_size;
131 static struct hash_entry empty_entry =
133 (struct hash_entry *) 0, (struct hash_entry *) 0,
134 (char *) 0, (char *) 0, 0,
136 register struct hash_entry *sym_entry =
137 table[hashfunct( sym, table_size )];
139 while ( sym_entry )
141 if ( ! strcmp( sym, sym_entry->name ) )
142 return sym_entry;
143 sym_entry = sym_entry->next;
146 return &empty_entry;
150 /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
152 int hashfunct( str, hash_size )
153 register char str[];
154 int hash_size;
156 register int hashval;
157 register int locstr;
159 hashval = 0;
160 locstr = 0;
162 while ( str[locstr] )
164 hashval = (hashval << 1) + (unsigned char) str[locstr++];
165 hashval %= hash_size;
168 return hashval;
172 /* ndinstal - install a name definition */
174 void ndinstal( name, definition )
175 char name[];
176 Char definition[];
178 if ( addsym( copy_string( name ),
179 (char *) copy_unsigned_string( definition ), 0,
180 ndtbl, NAME_TABLE_HASH_SIZE ) )
181 synerr( _( "name defined twice" ) );
185 /* ndlookup - lookup a name definition
187 * Returns a nil pointer if the name definition does not exist.
190 Char *ndlookup( nd )
191 char nd[];
193 return (Char *) findsym( nd, ndtbl, NAME_TABLE_HASH_SIZE )->str_val;
197 /* scextend - increase the maximum number of start conditions */
199 void scextend()
201 current_max_scs += MAX_SCS_INCREMENT;
203 ++num_reallocs;
205 scset = reallocate_integer_array( scset, current_max_scs );
206 scbol = reallocate_integer_array( scbol, current_max_scs );
207 scxclu = reallocate_integer_array( scxclu, current_max_scs );
208 sceof = reallocate_integer_array( sceof, current_max_scs );
209 scname = reallocate_char_ptr_array( scname, current_max_scs );
213 /* scinstal - make a start condition
215 * NOTE
216 * The start condition is "exclusive" if xcluflg is true.
219 void scinstal( str, xcluflg )
220 char str[];
221 int xcluflg;
223 /* Generate start condition definition, for use in BEGIN et al. */
224 action_define( str, lastsc );
226 if ( ++lastsc >= current_max_scs )
227 scextend();
229 scname[lastsc] = copy_string( str );
231 if ( addsym( scname[lastsc], (char *) 0, lastsc,
232 sctbl, START_COND_HASH_SIZE ) )
233 format_pinpoint_message(
234 _( "start condition %s declared twice" ),
235 str );
237 scset[lastsc] = mkstate( SYM_EPSILON );
238 scbol[lastsc] = mkstate( SYM_EPSILON );
239 scxclu[lastsc] = xcluflg;
240 sceof[lastsc] = false;
244 /* sclookup - lookup the number associated with a start condition
246 * Returns 0 if no such start condition.
249 int sclookup( str )
250 char str[];
252 return findsym( str, sctbl, START_COND_HASH_SIZE )->int_val;