3 /* sym - symbol table routines */
5 /* Copyright (c) 1990 The Regents of the University of California. */
6 /* All rights reserved. */
8 /* This code is derived from software contributed to Berkeley by */
11 /* The United States Government has rights in this work pursuant */
12 /* to contract no. DE-AC03-76SF00098 between the United States */
13 /* Department of Energy and the University of California. */
15 /* This file is part of flex. */
17 /* Redistribution and use in source and binary forms, with or without */
18 /* modification, are permitted provided that the following conditions */
21 /* 1. Redistributions of source code must retain the above copyright */
22 /* notice, this list of conditions and the following disclaimer. */
23 /* 2. Redistributions in binary form must reproduce the above copyright */
24 /* notice, this list of conditions and the following disclaimer in the */
25 /* documentation and/or other materials provided with the distribution. */
27 /* Neither the name of the University nor the names of its contributors */
28 /* may be used to endorse or promote products derived from this software */
29 /* without specific prior written permission. */
31 /* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR */
32 /* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED */
33 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR */
38 /* Variables for symbol tables:
39 * sctbl - start-condition symbol table
40 * ndtbl - name-definition symbol table
41 * ccltab - character class text symbol table
45 struct hash_entry
*prev
, *next
;
51 typedef struct hash_entry
**hash_table
;
53 #define NAME_TABLE_HASH_SIZE 101
54 #define START_COND_HASH_SIZE 101
55 #define CCL_HASH_SIZE 101
57 static struct hash_entry
*ndtbl
[NAME_TABLE_HASH_SIZE
];
58 static struct hash_entry
*sctbl
[START_COND_HASH_SIZE
];
59 static struct hash_entry
*ccltab
[CCL_HASH_SIZE
];
62 /* declare functions that have forward references */
64 static int addsym
PROTO ((register char[], char *, int, hash_table
, int));
65 static struct hash_entry
*findsym
PROTO ((register const char *sym
,
69 static int hashfunct
PROTO ((register const char *, int));
72 /* addsym - add symbol and definitions to symbol table
74 * -1 is returned if the symbol already exists, and the change not made.
77 static int addsym (sym
, str_def
, int_def
, table
, table_size
)
84 int hash_val
= hashfunct (sym
, table_size
);
85 register struct hash_entry
*sym_entry
= table
[hash_val
];
86 register struct hash_entry
*new_entry
;
87 register struct hash_entry
*successor
;
90 if (!strcmp (sym
, sym_entry
->name
)) { /* entry already exists */
94 sym_entry
= sym_entry
->next
;
97 /* create new entry */
98 new_entry
= (struct hash_entry
*)
99 flex_alloc (sizeof (struct hash_entry
));
101 if (new_entry
== NULL
)
102 flexfatal (_("symbol table memory allocation failed"));
104 if ((successor
= table
[hash_val
]) != 0) {
105 new_entry
->next
= successor
;
106 successor
->prev
= new_entry
;
109 new_entry
->next
= NULL
;
111 new_entry
->prev
= NULL
;
112 new_entry
->name
= sym
;
113 new_entry
->str_val
= str_def
;
114 new_entry
->int_val
= int_def
;
116 table
[hash_val
] = new_entry
;
122 /* cclinstal - save the text of a character class */
124 void cclinstal (ccltxt
, cclnum
)
128 /* We don't bother checking the return status because we are not
129 * called unless the symbol is new.
132 (void) addsym ((char *) copy_unsigned_string (ccltxt
),
133 (char *) 0, cclnum
, ccltab
, CCL_HASH_SIZE
);
137 /* ccllookup - lookup the number associated with character class text
139 * Returns 0 if there's no CCL associated with the text.
142 int ccllookup (ccltxt
)
145 return findsym ((char *) ccltxt
, ccltab
, CCL_HASH_SIZE
)->int_val
;
149 /* findsym - find symbol in symbol table */
151 static struct hash_entry
*findsym (sym
, table
, table_size
)
152 register const char *sym
;
156 static struct hash_entry empty_entry
= {
157 (struct hash_entry
*) 0, (struct hash_entry
*) 0,
158 (char *) 0, (char *) 0, 0,
160 register struct hash_entry
*sym_entry
=
162 table
[hashfunct (sym
, table_size
)];
165 if (!strcmp (sym
, sym_entry
->name
))
167 sym_entry
= sym_entry
->next
;
173 /* hashfunct - compute the hash value for "str" and hash size "hash_size" */
175 static int hashfunct (str
, hash_size
)
176 register const char *str
;
179 register int hashval
;
185 while (str
[locstr
]) {
186 hashval
= (hashval
<< 1) + (unsigned char) str
[locstr
++];
187 hashval
%= hash_size
;
194 /* ndinstal - install a name definition */
196 void ndinstal (name
, definition
)
201 if (addsym (copy_string (name
),
202 (char *) copy_unsigned_string (definition
), 0,
203 ndtbl
, NAME_TABLE_HASH_SIZE
))
204 synerr (_("name defined twice"));
208 /* ndlookup - lookup a name definition
210 * Returns a nil pointer if the name definition does not exist.
216 return (Char
*) findsym (nd
, ndtbl
, NAME_TABLE_HASH_SIZE
)->str_val
;
220 /* scextend - increase the maximum number of start conditions */
224 current_max_scs
+= MAX_SCS_INCREMENT
;
228 scset
= reallocate_integer_array (scset
, current_max_scs
);
229 scbol
= reallocate_integer_array (scbol
, current_max_scs
);
230 scxclu
= reallocate_integer_array (scxclu
, current_max_scs
);
231 sceof
= reallocate_integer_array (sceof
, current_max_scs
);
232 scname
= reallocate_char_ptr_array (scname
, current_max_scs
);
236 /* scinstal - make a start condition
239 * The start condition is "exclusive" if xcluflg is true.
242 void scinstal (str
, xcluflg
)
247 if (++lastsc
>= current_max_scs
)
250 scname
[lastsc
] = copy_string (str
);
252 if (addsym (scname
[lastsc
], (char *) 0, lastsc
,
253 sctbl
, START_COND_HASH_SIZE
))
254 format_pinpoint_message (_
255 ("start condition %s declared twice"),
258 scset
[lastsc
] = mkstate (SYM_EPSILON
);
259 scbol
[lastsc
] = mkstate (SYM_EPSILON
);
260 scxclu
[lastsc
] = xcluflg
;
261 sceof
[lastsc
] = false;
265 /* sclookup - lookup the number associated with a start condition
267 * Returns 0 if no such start condition.
273 return findsym (str
, sctbl
, START_COND_HASH_SIZE
)->int_val
;