xref: /freebsd/contrib/flex/src/sym.c (revision 7e38239042df09edbbdc443ccb4825f9155c6bb7)
1*7e382390SJung-uk Kim /* sym - symbol table routines */
2*7e382390SJung-uk Kim 
3*7e382390SJung-uk Kim /*  Copyright (c) 1990 The Regents of the University of California. */
4*7e382390SJung-uk Kim /*  All rights reserved. */
5*7e382390SJung-uk Kim 
6*7e382390SJung-uk Kim /*  This code is derived from software contributed to Berkeley by */
7*7e382390SJung-uk Kim /*  Vern Paxson. */
8*7e382390SJung-uk Kim 
9*7e382390SJung-uk Kim /*  The United States Government has rights in this work pursuant */
10*7e382390SJung-uk Kim /*  to contract no. DE-AC03-76SF00098 between the United States */
11*7e382390SJung-uk Kim /*  Department of Energy and the University of California. */
12*7e382390SJung-uk Kim 
13*7e382390SJung-uk Kim /*  This file is part of flex. */
14*7e382390SJung-uk Kim 
15*7e382390SJung-uk Kim /*  Redistribution and use in source and binary forms, with or without */
16*7e382390SJung-uk Kim /*  modification, are permitted provided that the following conditions */
17*7e382390SJung-uk Kim /*  are met: */
18*7e382390SJung-uk Kim 
19*7e382390SJung-uk Kim /*  1. Redistributions of source code must retain the above copyright */
20*7e382390SJung-uk Kim /*     notice, this list of conditions and the following disclaimer. */
21*7e382390SJung-uk Kim /*  2. Redistributions in binary form must reproduce the above copyright */
22*7e382390SJung-uk Kim /*     notice, this list of conditions and the following disclaimer in the */
23*7e382390SJung-uk Kim /*     documentation and/or other materials provided with the distribution. */
24*7e382390SJung-uk Kim 
25*7e382390SJung-uk Kim /*  Neither the name of the University nor the names of its contributors */
26*7e382390SJung-uk Kim /*  may be used to endorse or promote products derived from this software */
27*7e382390SJung-uk Kim /*  without specific prior written permission. */
28*7e382390SJung-uk Kim 
29*7e382390SJung-uk Kim /*  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
30*7e382390SJung-uk Kim /*  IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
31*7e382390SJung-uk Kim /*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
32*7e382390SJung-uk Kim /*  PURPOSE. */
33*7e382390SJung-uk Kim 
34*7e382390SJung-uk Kim #include "flexdef.h"
35*7e382390SJung-uk Kim 
36*7e382390SJung-uk Kim /* Variables for symbol tables:
37*7e382390SJung-uk Kim  * sctbl - start-condition symbol table
38*7e382390SJung-uk Kim  * ndtbl - name-definition symbol table
39*7e382390SJung-uk Kim  * ccltab - character class text symbol table
40*7e382390SJung-uk Kim  */
41*7e382390SJung-uk Kim 
42*7e382390SJung-uk Kim struct hash_entry {
43*7e382390SJung-uk Kim 	struct hash_entry *prev, *next;
44*7e382390SJung-uk Kim 	char   *name;
45*7e382390SJung-uk Kim 	char   *str_val;
46*7e382390SJung-uk Kim 	int     int_val;
47*7e382390SJung-uk Kim };
48*7e382390SJung-uk Kim 
49*7e382390SJung-uk Kim typedef struct hash_entry **hash_table;
50*7e382390SJung-uk Kim 
51*7e382390SJung-uk Kim #define NAME_TABLE_HASH_SIZE 101
52*7e382390SJung-uk Kim #define START_COND_HASH_SIZE 101
53*7e382390SJung-uk Kim #define CCL_HASH_SIZE 101
54*7e382390SJung-uk Kim 
55*7e382390SJung-uk Kim static struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
56*7e382390SJung-uk Kim static struct hash_entry *sctbl[START_COND_HASH_SIZE];
57*7e382390SJung-uk Kim static struct hash_entry *ccltab[CCL_HASH_SIZE];
58*7e382390SJung-uk Kim 
59*7e382390SJung-uk Kim 
60*7e382390SJung-uk Kim /* declare functions that have forward references */
61*7e382390SJung-uk Kim 
62*7e382390SJung-uk Kim static int addsym(char[], char *, int, hash_table, int);
63*7e382390SJung-uk Kim static struct hash_entry *findsym (const char *sym, hash_table table,
64*7e382390SJung-uk Kim 				   int table_size);
65*7e382390SJung-uk Kim static int hashfunct(const char *, int);
66*7e382390SJung-uk Kim 
67*7e382390SJung-uk Kim 
68*7e382390SJung-uk Kim /* addsym - add symbol and definitions to symbol table
69*7e382390SJung-uk Kim  *
70*7e382390SJung-uk Kim  * -1 is returned if the symbol already exists, and the change not made.
71*7e382390SJung-uk Kim  */
72*7e382390SJung-uk Kim 
addsym(char sym[],char * str_def,int int_def,hash_table table,int table_size)73*7e382390SJung-uk Kim static int addsym (char sym[], char *str_def, int int_def, hash_table table, int table_size)
74*7e382390SJung-uk Kim {
75*7e382390SJung-uk Kim 	int    hash_val = hashfunct (sym, table_size);
76*7e382390SJung-uk Kim 	struct hash_entry *sym_entry = table[hash_val];
77*7e382390SJung-uk Kim 	struct hash_entry *new_entry;
78*7e382390SJung-uk Kim 	struct hash_entry *successor;
79*7e382390SJung-uk Kim 
80*7e382390SJung-uk Kim 	while (sym_entry) {
81*7e382390SJung-uk Kim 		if (!strcmp (sym, sym_entry->name)) {	/* entry already exists */
82*7e382390SJung-uk Kim 			return -1;
83*7e382390SJung-uk Kim 		}
84*7e382390SJung-uk Kim 
85*7e382390SJung-uk Kim 		sym_entry = sym_entry->next;
86*7e382390SJung-uk Kim 	}
87*7e382390SJung-uk Kim 
88*7e382390SJung-uk Kim 	/* create new entry */
89*7e382390SJung-uk Kim 	new_entry = malloc(sizeof(struct hash_entry));
90*7e382390SJung-uk Kim 
91*7e382390SJung-uk Kim 	if (new_entry == NULL)
92*7e382390SJung-uk Kim 		flexfatal (_("symbol table memory allocation failed"));
93*7e382390SJung-uk Kim 
94*7e382390SJung-uk Kim 	if ((successor = table[hash_val]) != 0) {
95*7e382390SJung-uk Kim 		new_entry->next = successor;
96*7e382390SJung-uk Kim 		successor->prev = new_entry;
97*7e382390SJung-uk Kim 	}
98*7e382390SJung-uk Kim 	else
99*7e382390SJung-uk Kim 		new_entry->next = NULL;
100*7e382390SJung-uk Kim 
101*7e382390SJung-uk Kim 	new_entry->prev = NULL;
102*7e382390SJung-uk Kim 	new_entry->name = sym;
103*7e382390SJung-uk Kim 	new_entry->str_val = str_def;
104*7e382390SJung-uk Kim 	new_entry->int_val = int_def;
105*7e382390SJung-uk Kim 
106*7e382390SJung-uk Kim 	table[hash_val] = new_entry;
107*7e382390SJung-uk Kim 
108*7e382390SJung-uk Kim 	return 0;
109*7e382390SJung-uk Kim }
110*7e382390SJung-uk Kim 
111*7e382390SJung-uk Kim 
112*7e382390SJung-uk Kim /* cclinstal - save the text of a character class */
113*7e382390SJung-uk Kim 
cclinstal(char ccltxt[],int cclnum)114*7e382390SJung-uk Kim void    cclinstal (char ccltxt[], int cclnum)
115*7e382390SJung-uk Kim {
116*7e382390SJung-uk Kim 	/* We don't bother checking the return status because we are not
117*7e382390SJung-uk Kim 	 * called unless the symbol is new.
118*7e382390SJung-uk Kim 	 */
119*7e382390SJung-uk Kim 
120*7e382390SJung-uk Kim 	(void) addsym (xstrdup(ccltxt),
121*7e382390SJung-uk Kim 		       (char *) 0, cclnum, ccltab, CCL_HASH_SIZE);
122*7e382390SJung-uk Kim }
123*7e382390SJung-uk Kim 
124*7e382390SJung-uk Kim 
125*7e382390SJung-uk Kim /* ccllookup - lookup the number associated with character class text
126*7e382390SJung-uk Kim  *
127*7e382390SJung-uk Kim  * Returns 0 if there's no CCL associated with the text.
128*7e382390SJung-uk Kim  */
129*7e382390SJung-uk Kim 
ccllookup(char ccltxt[])130*7e382390SJung-uk Kim int     ccllookup (char ccltxt[])
131*7e382390SJung-uk Kim {
132*7e382390SJung-uk Kim 	return findsym (ccltxt, ccltab, CCL_HASH_SIZE)->int_val;
133*7e382390SJung-uk Kim }
134*7e382390SJung-uk Kim 
135*7e382390SJung-uk Kim 
136*7e382390SJung-uk Kim /* findsym - find symbol in symbol table */
137*7e382390SJung-uk Kim 
findsym(const char * sym,hash_table table,int table_size)138*7e382390SJung-uk Kim static struct hash_entry *findsym (const char *sym, hash_table table, int table_size)
139*7e382390SJung-uk Kim {
140*7e382390SJung-uk Kim 	static struct hash_entry empty_entry = {
141*7e382390SJung-uk Kim 		NULL, NULL, NULL, NULL, 0,
142*7e382390SJung-uk Kim 	};
143*7e382390SJung-uk Kim 	struct hash_entry *sym_entry =
144*7e382390SJung-uk Kim 
145*7e382390SJung-uk Kim 		table[hashfunct (sym, table_size)];
146*7e382390SJung-uk Kim 
147*7e382390SJung-uk Kim 	while (sym_entry) {
148*7e382390SJung-uk Kim 		if (!strcmp (sym, sym_entry->name))
149*7e382390SJung-uk Kim 			return sym_entry;
150*7e382390SJung-uk Kim 		sym_entry = sym_entry->next;
151*7e382390SJung-uk Kim 	}
152*7e382390SJung-uk Kim 
153*7e382390SJung-uk Kim 	return &empty_entry;
154*7e382390SJung-uk Kim }
155*7e382390SJung-uk Kim 
156*7e382390SJung-uk Kim /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
157*7e382390SJung-uk Kim 
hashfunct(const char * str,int hash_size)158*7e382390SJung-uk Kim static int hashfunct (const char *str, int hash_size)
159*7e382390SJung-uk Kim {
160*7e382390SJung-uk Kim 	int hashval;
161*7e382390SJung-uk Kim 	int locstr;
162*7e382390SJung-uk Kim 
163*7e382390SJung-uk Kim 	hashval = 0;
164*7e382390SJung-uk Kim 	locstr = 0;
165*7e382390SJung-uk Kim 
166*7e382390SJung-uk Kim 	while (str[locstr]) {
167*7e382390SJung-uk Kim 		hashval = (hashval << 1) + (unsigned char) str[locstr++];
168*7e382390SJung-uk Kim 		hashval %= hash_size;
169*7e382390SJung-uk Kim 	}
170*7e382390SJung-uk Kim 
171*7e382390SJung-uk Kim 	return hashval;
172*7e382390SJung-uk Kim }
173*7e382390SJung-uk Kim 
174*7e382390SJung-uk Kim 
175*7e382390SJung-uk Kim /* ndinstal - install a name definition */
176*7e382390SJung-uk Kim 
ndinstal(const char * name,char definition[])177*7e382390SJung-uk Kim void    ndinstal (const char *name, char definition[])
178*7e382390SJung-uk Kim {
179*7e382390SJung-uk Kim 
180*7e382390SJung-uk Kim 	if (addsym (xstrdup(name),
181*7e382390SJung-uk Kim 		    xstrdup(definition), 0,
182*7e382390SJung-uk Kim 		    ndtbl, NAME_TABLE_HASH_SIZE))
183*7e382390SJung-uk Kim 			synerr (_("name defined twice"));
184*7e382390SJung-uk Kim }
185*7e382390SJung-uk Kim 
186*7e382390SJung-uk Kim 
187*7e382390SJung-uk Kim /* ndlookup - lookup a name definition
188*7e382390SJung-uk Kim  *
189*7e382390SJung-uk Kim  * Returns a nil pointer if the name definition does not exist.
190*7e382390SJung-uk Kim  */
191*7e382390SJung-uk Kim 
ndlookup(const char * nd)192*7e382390SJung-uk Kim char   *ndlookup (const char *nd)
193*7e382390SJung-uk Kim {
194*7e382390SJung-uk Kim 	return findsym (nd, ndtbl, NAME_TABLE_HASH_SIZE)->str_val;
195*7e382390SJung-uk Kim }
196*7e382390SJung-uk Kim 
197*7e382390SJung-uk Kim 
198*7e382390SJung-uk Kim /* scextend - increase the maximum number of start conditions */
199*7e382390SJung-uk Kim 
scextend(void)200*7e382390SJung-uk Kim void    scextend (void)
201*7e382390SJung-uk Kim {
202*7e382390SJung-uk Kim 	current_max_scs += MAX_SCS_INCREMENT;
203*7e382390SJung-uk Kim 
204*7e382390SJung-uk Kim 	++num_reallocs;
205*7e382390SJung-uk Kim 
206*7e382390SJung-uk Kim 	scset = reallocate_integer_array (scset, current_max_scs);
207*7e382390SJung-uk Kim 	scbol = reallocate_integer_array (scbol, current_max_scs);
208*7e382390SJung-uk Kim 	scxclu = reallocate_integer_array (scxclu, current_max_scs);
209*7e382390SJung-uk Kim 	sceof = reallocate_integer_array (sceof, current_max_scs);
210*7e382390SJung-uk Kim 	scname = reallocate_char_ptr_array (scname, current_max_scs);
211*7e382390SJung-uk Kim }
212*7e382390SJung-uk Kim 
213*7e382390SJung-uk Kim 
214*7e382390SJung-uk Kim /* scinstal - make a start condition
215*7e382390SJung-uk Kim  *
216*7e382390SJung-uk Kim  * NOTE
217*7e382390SJung-uk Kim  *    The start condition is "exclusive" if xcluflg is true.
218*7e382390SJung-uk Kim  */
219*7e382390SJung-uk Kim 
scinstal(const char * str,int xcluflg)220*7e382390SJung-uk Kim void    scinstal (const char *str, int xcluflg)
221*7e382390SJung-uk Kim {
222*7e382390SJung-uk Kim 
223*7e382390SJung-uk Kim 	if (++lastsc >= current_max_scs)
224*7e382390SJung-uk Kim 		scextend ();
225*7e382390SJung-uk Kim 
226*7e382390SJung-uk Kim 	scname[lastsc] = xstrdup(str);
227*7e382390SJung-uk Kim 
228*7e382390SJung-uk Kim 	if (addsym(scname[lastsc], NULL, lastsc,
229*7e382390SJung-uk Kim 		    sctbl, START_COND_HASH_SIZE))
230*7e382390SJung-uk Kim 			format_pinpoint_message (_
231*7e382390SJung-uk Kim 						 ("start condition %s declared twice"),
232*7e382390SJung-uk Kim str);
233*7e382390SJung-uk Kim 
234*7e382390SJung-uk Kim 	scset[lastsc] = mkstate (SYM_EPSILON);
235*7e382390SJung-uk Kim 	scbol[lastsc] = mkstate (SYM_EPSILON);
236*7e382390SJung-uk Kim 	scxclu[lastsc] = xcluflg;
237*7e382390SJung-uk Kim 	sceof[lastsc] = false;
238*7e382390SJung-uk Kim }
239*7e382390SJung-uk Kim 
240*7e382390SJung-uk Kim 
241*7e382390SJung-uk Kim /* sclookup - lookup the number associated with a start condition
242*7e382390SJung-uk Kim  *
243*7e382390SJung-uk Kim  * Returns 0 if no such start condition.
244*7e382390SJung-uk Kim  */
245*7e382390SJung-uk Kim 
sclookup(const char * str)246*7e382390SJung-uk Kim int     sclookup (const char *str)
247*7e382390SJung-uk Kim {
248*7e382390SJung-uk Kim 	return findsym (str, sctbl, START_COND_HASH_SIZE)->int_val;
249*7e382390SJung-uk Kim }
250