1*0a6a1f1dSLionel Sambuc /* $NetBSD: sym.c,v 1.3 2014/10/30 18:44:05 christos Exp $ */
2357f1050SThomas Veerman
3357f1050SThomas Veerman /* sym - symbol table routines */
4357f1050SThomas Veerman
5357f1050SThomas Veerman /* Copyright (c) 1990 The Regents of the University of California. */
6357f1050SThomas Veerman /* All rights reserved. */
7357f1050SThomas Veerman
8357f1050SThomas Veerman /* This code is derived from software contributed to Berkeley by */
9357f1050SThomas Veerman /* Vern Paxson. */
10357f1050SThomas Veerman
11357f1050SThomas Veerman /* The United States Government has rights in this work pursuant */
12357f1050SThomas Veerman /* to contract no. DE-AC03-76SF00098 between the United States */
13357f1050SThomas Veerman /* Department of Energy and the University of California. */
14357f1050SThomas Veerman
15357f1050SThomas Veerman /* This file is part of flex. */
16357f1050SThomas Veerman
17357f1050SThomas Veerman /* Redistribution and use in source and binary forms, with or without */
18357f1050SThomas Veerman /* modification, are permitted provided that the following conditions */
19357f1050SThomas Veerman /* are met: */
20357f1050SThomas Veerman
21357f1050SThomas Veerman /* 1. Redistributions of source code must retain the above copyright */
22357f1050SThomas Veerman /* notice, this list of conditions and the following disclaimer. */
23357f1050SThomas Veerman /* 2. Redistributions in binary form must reproduce the above copyright */
24357f1050SThomas Veerman /* notice, this list of conditions and the following disclaimer in the */
25357f1050SThomas Veerman /* documentation and/or other materials provided with the distribution. */
26357f1050SThomas Veerman
27357f1050SThomas Veerman /* Neither the name of the University nor the names of its contributors */
28357f1050SThomas Veerman /* may be used to endorse or promote products derived from this software */
29357f1050SThomas Veerman /* without specific prior written permission. */
30357f1050SThomas Veerman
31357f1050SThomas Veerman /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32357f1050SThomas Veerman /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33357f1050SThomas Veerman /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
34357f1050SThomas Veerman /* PURPOSE. */
35357f1050SThomas Veerman #include "flexdef.h"
36*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: sym.c,v 1.3 2014/10/30 18:44:05 christos Exp $");
37*0a6a1f1dSLionel Sambuc
38357f1050SThomas Veerman
39357f1050SThomas Veerman /* Variables for symbol tables:
40357f1050SThomas Veerman * sctbl - start-condition symbol table
41357f1050SThomas Veerman * ndtbl - name-definition symbol table
42357f1050SThomas Veerman * ccltab - character class text symbol table
43357f1050SThomas Veerman */
44357f1050SThomas Veerman
45357f1050SThomas Veerman struct hash_entry {
46357f1050SThomas Veerman struct hash_entry *prev, *next;
47357f1050SThomas Veerman char *name;
48357f1050SThomas Veerman char *str_val;
49357f1050SThomas Veerman int int_val;
50357f1050SThomas Veerman };
51357f1050SThomas Veerman
52357f1050SThomas Veerman typedef struct hash_entry **hash_table;
53357f1050SThomas Veerman
54357f1050SThomas Veerman #define NAME_TABLE_HASH_SIZE 101
55357f1050SThomas Veerman #define START_COND_HASH_SIZE 101
56357f1050SThomas Veerman #define CCL_HASH_SIZE 101
57357f1050SThomas Veerman
58357f1050SThomas Veerman static struct hash_entry *ndtbl[NAME_TABLE_HASH_SIZE];
59357f1050SThomas Veerman static struct hash_entry *sctbl[START_COND_HASH_SIZE];
60357f1050SThomas Veerman static struct hash_entry *ccltab[CCL_HASH_SIZE];
61357f1050SThomas Veerman
62357f1050SThomas Veerman
63357f1050SThomas Veerman /* declare functions that have forward references */
64357f1050SThomas Veerman
65357f1050SThomas Veerman static int addsym PROTO ((register char[], char *, int, hash_table, int));
66357f1050SThomas Veerman static struct hash_entry *findsym PROTO ((register const char *sym,
67357f1050SThomas Veerman hash_table table,
68357f1050SThomas Veerman
69357f1050SThomas Veerman int table_size));
70357f1050SThomas Veerman static int hashfunct PROTO ((register const char *, int));
71357f1050SThomas Veerman
72357f1050SThomas Veerman
73357f1050SThomas Veerman /* addsym - add symbol and definitions to symbol table
74357f1050SThomas Veerman *
75357f1050SThomas Veerman * -1 is returned if the symbol already exists, and the change not made.
76357f1050SThomas Veerman */
77357f1050SThomas Veerman
addsym(sym,str_def,int_def,table,table_size)78357f1050SThomas Veerman static int addsym (sym, str_def, int_def, table, table_size)
79357f1050SThomas Veerman register char sym[];
80357f1050SThomas Veerman char *str_def;
81357f1050SThomas Veerman int int_def;
82357f1050SThomas Veerman hash_table table;
83357f1050SThomas Veerman int table_size;
84357f1050SThomas Veerman {
85357f1050SThomas Veerman int hash_val = hashfunct (sym, table_size);
86357f1050SThomas Veerman register struct hash_entry *sym_entry = table[hash_val];
87357f1050SThomas Veerman register struct hash_entry *new_entry;
88357f1050SThomas Veerman register struct hash_entry *successor;
89357f1050SThomas Veerman
90357f1050SThomas Veerman while (sym_entry) {
91357f1050SThomas Veerman if (!strcmp (sym, sym_entry->name)) { /* entry already exists */
92357f1050SThomas Veerman return -1;
93357f1050SThomas Veerman }
94357f1050SThomas Veerman
95357f1050SThomas Veerman sym_entry = sym_entry->next;
96357f1050SThomas Veerman }
97357f1050SThomas Veerman
98357f1050SThomas Veerman /* create new entry */
99357f1050SThomas Veerman new_entry = (struct hash_entry *)
100357f1050SThomas Veerman flex_alloc (sizeof (struct hash_entry));
101357f1050SThomas Veerman
102357f1050SThomas Veerman if (new_entry == NULL)
103357f1050SThomas Veerman flexfatal (_("symbol table memory allocation failed"));
104357f1050SThomas Veerman
105357f1050SThomas Veerman if ((successor = table[hash_val]) != 0) {
106357f1050SThomas Veerman new_entry->next = successor;
107357f1050SThomas Veerman successor->prev = new_entry;
108357f1050SThomas Veerman }
109357f1050SThomas Veerman else
110357f1050SThomas Veerman new_entry->next = NULL;
111357f1050SThomas Veerman
112357f1050SThomas Veerman new_entry->prev = NULL;
113357f1050SThomas Veerman new_entry->name = sym;
114357f1050SThomas Veerman new_entry->str_val = str_def;
115357f1050SThomas Veerman new_entry->int_val = int_def;
116357f1050SThomas Veerman
117357f1050SThomas Veerman table[hash_val] = new_entry;
118357f1050SThomas Veerman
119357f1050SThomas Veerman return 0;
120357f1050SThomas Veerman }
121357f1050SThomas Veerman
122357f1050SThomas Veerman
123357f1050SThomas Veerman /* cclinstal - save the text of a character class */
124357f1050SThomas Veerman
cclinstal(ccltxt,cclnum)125357f1050SThomas Veerman void cclinstal (ccltxt, cclnum)
126357f1050SThomas Veerman Char ccltxt[];
127357f1050SThomas Veerman int cclnum;
128357f1050SThomas Veerman {
129357f1050SThomas Veerman /* We don't bother checking the return status because we are not
130357f1050SThomas Veerman * called unless the symbol is new.
131357f1050SThomas Veerman */
132357f1050SThomas Veerman
133357f1050SThomas Veerman (void) addsym ((char *) copy_unsigned_string (ccltxt),
134357f1050SThomas Veerman (char *) 0, cclnum, ccltab, CCL_HASH_SIZE);
135357f1050SThomas Veerman }
136357f1050SThomas Veerman
137357f1050SThomas Veerman
138357f1050SThomas Veerman /* ccllookup - lookup the number associated with character class text
139357f1050SThomas Veerman *
140357f1050SThomas Veerman * Returns 0 if there's no CCL associated with the text.
141357f1050SThomas Veerman */
142357f1050SThomas Veerman
ccllookup(ccltxt)143357f1050SThomas Veerman int ccllookup (ccltxt)
144357f1050SThomas Veerman Char ccltxt[];
145357f1050SThomas Veerman {
146357f1050SThomas Veerman return findsym ((char *) ccltxt, ccltab, CCL_HASH_SIZE)->int_val;
147357f1050SThomas Veerman }
148357f1050SThomas Veerman
149357f1050SThomas Veerman
150357f1050SThomas Veerman /* findsym - find symbol in symbol table */
151357f1050SThomas Veerman
findsym(sym,table,table_size)152357f1050SThomas Veerman static struct hash_entry *findsym (sym, table, table_size)
153357f1050SThomas Veerman register const char *sym;
154357f1050SThomas Veerman hash_table table;
155357f1050SThomas Veerman int table_size;
156357f1050SThomas Veerman {
157357f1050SThomas Veerman static struct hash_entry empty_entry = {
158357f1050SThomas Veerman (struct hash_entry *) 0, (struct hash_entry *) 0,
159357f1050SThomas Veerman (char *) 0, (char *) 0, 0,
160357f1050SThomas Veerman };
161357f1050SThomas Veerman register struct hash_entry *sym_entry =
162357f1050SThomas Veerman
163357f1050SThomas Veerman table[hashfunct (sym, table_size)];
164357f1050SThomas Veerman
165357f1050SThomas Veerman while (sym_entry) {
166357f1050SThomas Veerman if (!strcmp (sym, sym_entry->name))
167357f1050SThomas Veerman return sym_entry;
168357f1050SThomas Veerman sym_entry = sym_entry->next;
169357f1050SThomas Veerman }
170357f1050SThomas Veerman
171357f1050SThomas Veerman return &empty_entry;
172357f1050SThomas Veerman }
173357f1050SThomas Veerman
174357f1050SThomas Veerman /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
175357f1050SThomas Veerman
hashfunct(str,hash_size)176357f1050SThomas Veerman static int hashfunct (str, hash_size)
177357f1050SThomas Veerman register const char *str;
178357f1050SThomas Veerman int hash_size;
179357f1050SThomas Veerman {
180357f1050SThomas Veerman register int hashval;
181357f1050SThomas Veerman register int locstr;
182357f1050SThomas Veerman
183357f1050SThomas Veerman hashval = 0;
184357f1050SThomas Veerman locstr = 0;
185357f1050SThomas Veerman
186357f1050SThomas Veerman while (str[locstr]) {
187357f1050SThomas Veerman hashval = (hashval << 1) + (unsigned char) str[locstr++];
188357f1050SThomas Veerman hashval %= hash_size;
189357f1050SThomas Veerman }
190357f1050SThomas Veerman
191357f1050SThomas Veerman return hashval;
192357f1050SThomas Veerman }
193357f1050SThomas Veerman
194357f1050SThomas Veerman
195357f1050SThomas Veerman /* ndinstal - install a name definition */
196357f1050SThomas Veerman
ndinstal(name,definition)197357f1050SThomas Veerman void ndinstal (name, definition)
198357f1050SThomas Veerman const char *name;
199357f1050SThomas Veerman Char definition[];
200357f1050SThomas Veerman {
201357f1050SThomas Veerman
202357f1050SThomas Veerman if (addsym (copy_string (name),
203357f1050SThomas Veerman (char *) copy_unsigned_string (definition), 0,
204357f1050SThomas Veerman ndtbl, NAME_TABLE_HASH_SIZE))
205357f1050SThomas Veerman synerr (_("name defined twice"));
206357f1050SThomas Veerman }
207357f1050SThomas Veerman
208357f1050SThomas Veerman
209357f1050SThomas Veerman /* ndlookup - lookup a name definition
210357f1050SThomas Veerman *
211357f1050SThomas Veerman * Returns a nil pointer if the name definition does not exist.
212357f1050SThomas Veerman */
213357f1050SThomas Veerman
ndlookup(nd)214357f1050SThomas Veerman Char *ndlookup (nd)
215357f1050SThomas Veerman const char *nd;
216357f1050SThomas Veerman {
217357f1050SThomas Veerman return (Char *) findsym (nd, ndtbl, NAME_TABLE_HASH_SIZE)->str_val;
218357f1050SThomas Veerman }
219357f1050SThomas Veerman
220357f1050SThomas Veerman
221357f1050SThomas Veerman /* scextend - increase the maximum number of start conditions */
222357f1050SThomas Veerman
scextend()223357f1050SThomas Veerman void scextend ()
224357f1050SThomas Veerman {
225357f1050SThomas Veerman current_max_scs += MAX_SCS_INCREMENT;
226357f1050SThomas Veerman
227357f1050SThomas Veerman ++num_reallocs;
228357f1050SThomas Veerman
229357f1050SThomas Veerman scset = reallocate_integer_array (scset, current_max_scs);
230357f1050SThomas Veerman scbol = reallocate_integer_array (scbol, current_max_scs);
231357f1050SThomas Veerman scxclu = reallocate_integer_array (scxclu, current_max_scs);
232357f1050SThomas Veerman sceof = reallocate_integer_array (sceof, current_max_scs);
233357f1050SThomas Veerman scname = reallocate_char_ptr_array (scname, current_max_scs);
234357f1050SThomas Veerman }
235357f1050SThomas Veerman
236357f1050SThomas Veerman
237357f1050SThomas Veerman /* scinstal - make a start condition
238357f1050SThomas Veerman *
239357f1050SThomas Veerman * NOTE
240357f1050SThomas Veerman * The start condition is "exclusive" if xcluflg is true.
241357f1050SThomas Veerman */
242357f1050SThomas Veerman
scinstal(str,xcluflg)243357f1050SThomas Veerman void scinstal (str, xcluflg)
244357f1050SThomas Veerman const char *str;
245357f1050SThomas Veerman int xcluflg;
246357f1050SThomas Veerman {
247357f1050SThomas Veerman
248357f1050SThomas Veerman if (++lastsc >= current_max_scs)
249357f1050SThomas Veerman scextend ();
250357f1050SThomas Veerman
251357f1050SThomas Veerman scname[lastsc] = copy_string (str);
252357f1050SThomas Veerman
253357f1050SThomas Veerman if (addsym (scname[lastsc], (char *) 0, lastsc,
254357f1050SThomas Veerman sctbl, START_COND_HASH_SIZE))
255357f1050SThomas Veerman format_pinpoint_message (_
256357f1050SThomas Veerman ("start condition %s declared twice"),
257357f1050SThomas Veerman str);
258357f1050SThomas Veerman
259357f1050SThomas Veerman scset[lastsc] = mkstate (SYM_EPSILON);
260357f1050SThomas Veerman scbol[lastsc] = mkstate (SYM_EPSILON);
261357f1050SThomas Veerman scxclu[lastsc] = xcluflg;
262357f1050SThomas Veerman sceof[lastsc] = false;
263357f1050SThomas Veerman }
264357f1050SThomas Veerman
265357f1050SThomas Veerman
266357f1050SThomas Veerman /* sclookup - lookup the number associated with a start condition
267357f1050SThomas Veerman *
268357f1050SThomas Veerman * Returns 0 if no such start condition.
269357f1050SThomas Veerman */
270357f1050SThomas Veerman
sclookup(str)271357f1050SThomas Veerman int sclookup (str)
272357f1050SThomas Veerman const char *str;
273357f1050SThomas Veerman {
274357f1050SThomas Veerman return findsym (str, sctbl, START_COND_HASH_SIZE)->int_val;
275357f1050SThomas Veerman }
276