]> git.saurik.com Git - bison.git/blame - src/symtab.c
Remove `%thong' support as it is undocumented, unused, duplicates
[bison.git] / src / symtab.c
CommitLineData
40675e7c 1/* Symbol table manager for Bison,
72a23c97 2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
40675e7c 3
95e36146 4 This file is part of Bison, the GNU Compiler Compiler.
40675e7c 5
95e36146
AD
6 Bison is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
40675e7c 10
95e36146
AD
11 Bison is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
40675e7c 15
95e36146
AD
16 You should have received a copy of the GNU General Public License
17 along with Bison; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
40675e7c
DM
20
21
40675e7c 22#include "system.h"
72a23c97 23#include "hash.h"
2f1afb73 24#include "complain.h"
40675e7c
DM
25#include "symtab.h"
26#include "gram.h"
27
2f1afb73
AD
28/*------------------------.
29| Distinguished symbols. |
30`------------------------*/
31
32symbol_t *errtoken = NULL;
33symbol_t *undeftoken = NULL;
34symbol_t *eoftoken = NULL;
35symbol_t *axiom = NULL;
36symbol_t *startsymbol = NULL;
37
72a23c97
AD
38/*---------------------------------.
39| Create a new symbol, named TAG. |
40`---------------------------------*/
1e9798d5 41
db8837cb
AD
42static symbol_t *
43symbol_new (const char *tag)
1e9798d5 44{
db8837cb 45 symbol_t *res = XMALLOC (symbol_t, 1);
1e9798d5 46
1e9798d5
AD
47 res->tag = xstrdup (tag);
48 res->type_name = NULL;
5fbb0954 49 res->number = NUMBER_UNDEFINED;
1e9798d5
AD
50 res->prec = 0;
51 res->assoc = right_assoc;
b87f8b21 52 res->user_token_number = USER_NUMBER_UNDEFINED;
1e9798d5
AD
53 res->alias = NULL;
54 res->class = unknown_sym;
55
56 nsyms++;
57
58 return res;
59}
60
40675e7c 61
3ae2b51f
AD
62/*-----------------------------------------.
63| Set the TYPE_NAME associated to SYMBOL. |
64`-----------------------------------------*/
65
66void
67symbol_type_set (symbol_t *symbol, char *type_name)
68{
69 if (symbol->type_name)
70 complain (_("type redeclaration for %s"), symbol->tag);
71 symbol->type_name = type_name;
72}
73
74
75/*------------------------------------------.
76| Set the PRECEDENCE associated to SYMBOL. |
77`------------------------------------------*/
78
79void
80symbol_precedence_set (symbol_t *symbol,
81 int prec, associativity assoc)
82{
83 if (symbol->prec != 0)
84 complain (_("redefining precedence of %s"), symbol->tag);
85 symbol->prec = prec;
86 symbol->assoc = assoc;
87}
88
89
72a23c97
AD
90/*------------.
91| Free THIS. |
92`------------*/
93
94static void
db8837cb 95symbol_free (symbol_t *this)
40675e7c 96{
72a23c97
AD
97#if 0
98 /* This causes crashes because one string can appear more
99 than once. */
100 XFREE (this->type_name);
101#endif
102 XFREE (this->tag);
103 XFREE (this);
104}
105
106
2f1afb73
AD
107/*-----------------------------------------------------------.
108| If THIS is not defined, report an error, and consider it a |
109| nonterminal. |
110`-----------------------------------------------------------*/
111
112static bool
113symbol_check_defined (symbol_t *this)
114{
115 if (this->class == unknown_sym)
116 {
117 complain
118 (_("symbol %s is used, but is not defined as a token and has no rules"),
119 this->tag);
120 this->class = nterm_sym;
121 this->number = nvars++;
122 }
123
124 return TRUE;
125}
126
127
128/*-------------------------------------------------------------------.
129| Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
130| with TYPENAME. |
131`-------------------------------------------------------------------*/
132
133void
134symbol_make_alias (symbol_t *symbol, symbol_t *symval, char *typename)
135{
136 if (symval->alias)
137 warn (_("symbol `%s' used more than once as a literal string"),
138 symval->tag);
139 else if (symbol->alias)
140 warn (_("symbol `%s' given more than one literal string"),
141 symbol->tag);
142 else
143 {
144 symval->class = token_sym;
145 symval->type_name = typename;
146 symval->user_token_number = symbol->user_token_number;
147 symbol->user_token_number = USER_NUMBER_ALIAS;
148 symval->alias = symbol;
149 symbol->alias = symval;
150 /* symbol and symval combined are only one symbol */
151 nsyms--;
152 ntokens--;
153 assert (ntokens == symbol->number || ntokens == symval->number);
154 symbol->number = symval->number =
155 (symval->number < symbol->number) ? symval->number : symbol->number;
156 }
157}
158
159
160/*---------------------------------------------------------.
161| Check that THIS, and its alias, have same precedence and |
162| associativity. |
163`---------------------------------------------------------*/
164
165static bool
166symbol_check_alias_consistence (symbol_t *this)
167{
168 /* Check only those who _are_ the aliases. */
169 if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
170 {
171 if (this->prec != this->alias->prec)
172 {
173 if (this->prec != 0 && this->alias->prec != 0)
174 complain (_("conflicting precedences for %s and %s"),
175 this->tag, this->alias->tag);
176 if (this->prec != 0)
177 this->alias->prec = this->prec;
178 else
179 this->prec = this->alias->prec;
180 }
181
182 if (this->assoc != this->alias->assoc)
183 {
184 if (this->assoc != 0 && this->alias->assoc != 0)
185 complain (_("conflicting assoc values for %s and %s"),
186 this->tag, this->alias->tag);
187 if (this->assoc != 0)
188 this->alias->assoc = this->assoc;
189 else
190 this->assoc = this->alias->assoc;
191 }
192 }
193 return TRUE;
194}
195
196
197/*-------------------------------------------------------------------.
198| Assign a symbol number, and write the definition of the token name |
199| into FDEFINES. Put in SYMBOLS. |
200`-------------------------------------------------------------------*/
201
202static bool
203symbol_pack (symbol_t *this)
204{
205 if (this->class == nterm_sym)
206 {
207 this->number += ntokens;
208 }
209 else if (this->alias)
210 {
211 /* This symbol and its alias are a single token defn.
212 Allocate a tokno, and assign to both check agreement of
213 prec and assoc fields and make both the same */
214 if (this->number == NUMBER_UNDEFINED)
215 {
216 if (this == eoftoken || this->alias == eoftoken)
217 this->number = this->alias->number = 0;
218 else
219 {
220 assert (this->alias->number != NUMBER_UNDEFINED);
221 this->number = this->alias->number;
222 }
223 }
224 /* Do not do processing below for USER_NUMBER_ALIASs. */
225 if (this->user_token_number == USER_NUMBER_ALIAS)
226 return TRUE;
227 }
228 else /* this->class == token_sym */
229 {
230 assert (this->number != NUMBER_UNDEFINED);
231 }
232
233 symbols[this->number] = this;
234 return TRUE;
235}
236
237
238
239
240/*--------------------------------------------------.
241| Put THIS in TOKEN_TRANSLATIONS if it is a token. |
242`--------------------------------------------------*/
243
244static bool
245symbol_translation (symbol_t *this)
246{
247 /* Non-terminal? */
248 if (this->class == token_sym
249 && this->user_token_number != USER_NUMBER_ALIAS)
250 {
251 /* A token which translation has already been set? */
252 if (token_translations[this->user_token_number] != undeftoken->number)
253 complain (_("tokens %s and %s both assigned number %d"),
254 symbols[token_translations[this->user_token_number]]->tag,
255 this->tag, this->user_token_number);
256
257 token_translations[this->user_token_number] = this->number;
258 }
259
260 return TRUE;
261}
262
72a23c97
AD
263
264/*----------------------.
2f1afb73 265| A symbol hash table. |
72a23c97 266`----------------------*/
40675e7c 267
db8837cb 268/* Initial capacity of symbols hash table. */
72a23c97
AD
269#define HT_INITIAL_CAPACITY 257
270
db8837cb 271static struct hash_table *symbol_table = NULL;
72a23c97
AD
272
273static bool
db8837cb 274hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
72a23c97
AD
275{
276 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
277}
278
279static unsigned int
db8837cb 280hash_symbol_t (const symbol_t *m, unsigned int tablesize)
72a23c97
AD
281{
282 return hash_string (m->tag, tablesize);
283}
284
285
286/*-------------------------------.
2f1afb73 287| Create the symbol hash table. |
72a23c97
AD
288`-------------------------------*/
289
290void
db8837cb 291symbols_new (void)
72a23c97 292{
db8837cb 293 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
72a23c97 294 NULL,
db8837cb
AD
295 (Hash_hasher) hash_symbol_t,
296 (Hash_comparator) hash_compare_symbol_t,
297 (Hash_data_freer) symbol_free);
40675e7c
DM
298}
299
300
1e9798d5
AD
301/*----------------------------------------------------------------.
302| Find the symbol named KEY, and return it. If it does not exist |
303| yet, create it. |
304`----------------------------------------------------------------*/
305
db8837cb 306symbol_t *
4a120d45 307getsym (const char *key)
40675e7c 308{
db8837cb
AD
309 symbol_t probe;
310 symbol_t *entry;
40675e7c 311
72a23c97 312 (const char *) probe.tag = key;
db8837cb 313 entry = hash_lookup (symbol_table, &probe);
40675e7c 314
72a23c97 315 if (!entry)
40675e7c 316 {
72a23c97 317 /* First insertion in the hash. */
db8837cb
AD
318 entry = symbol_new (key);
319 hash_insert (symbol_table, entry);
40675e7c 320 }
72a23c97
AD
321 return entry;
322}
40675e7c 323
40675e7c 324
72a23c97 325/*-------------------.
db8837cb 326| Free the symbols. |
72a23c97
AD
327`-------------------*/
328
329void
db8837cb 330symbols_free (void)
72a23c97 331{
db8837cb 332 hash_free (symbol_table);
40675e7c
DM
333}
334
335
72a23c97 336/*---------------------------------------------------------------.
db8837cb 337| Look for undefined symbols, report an error, and consider them |
72a23c97
AD
338| terminals. |
339`---------------------------------------------------------------*/
340
40675e7c 341void
db8837cb 342symbols_do (symbol_processor processor, void *processor_data)
40675e7c 343{
db8837cb 344 hash_do_for_each (symbol_table,
72a23c97
AD
345 (Hash_processor) processor,
346 processor_data);
40675e7c 347}
2f1afb73
AD
348
349
350/*--------------------------------------------------------------.
351| Check that all the symbols are defined. Report any undefined |
352| symbols and consider them nonterminals. |
353`--------------------------------------------------------------*/
354
355void
356symbols_check_defined (void)
357{
358 symbols_do (symbol_check_defined, NULL);
359}
360
361/*------------------------------------------------------------------.
362| Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
363| number. |
364`------------------------------------------------------------------*/
365
366static void
367symbols_token_translations_init (void)
368{
369 int num_256_available_p = TRUE;
370 int i;
371
372 /* Find the highest user token number, and whether 256, the POSIX
373 preferred user token number for the error token, is used. */
374 max_user_token_number = 0;
375 for (i = 0; i < ntokens; ++i)
376 {
377 symbol_t *this = symbols[i];
378 if (this->user_token_number != USER_NUMBER_UNDEFINED)
379 {
380 if (this->user_token_number > max_user_token_number)
381 max_user_token_number = this->user_token_number;
382 if (this->user_token_number == 256)
383 num_256_available_p = FALSE;
384 }
385 }
386
387 /* If 256 is not used, assign it to error, to follow POSIX. */
388 if (num_256_available_p
389 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
390 errtoken->user_token_number = 256;
391
392 /* Set the missing user numbers. */
393 if (max_user_token_number < 256)
394 max_user_token_number = 256;
395
396 for (i = 0; i < ntokens; ++i)
397 {
398 symbol_t *this = symbols[i];
399 if (this->user_token_number == USER_NUMBER_UNDEFINED)
400 this->user_token_number = ++max_user_token_number;
401 if (this->user_token_number > max_user_token_number)
402 max_user_token_number = this->user_token_number;
403 }
404
405 token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1);
406
407 /* Initialize all entries for literal tokens to 2, the internal
408 token number for $undefined., which represents all invalid
409 inputs. */
410 for (i = 0; i < max_user_token_number + 1; i++)
411 token_translations[i] = undeftoken->number;
412 symbols_do (symbol_translation, NULL);
413}
414
415
416/*----------------------------------------------------------------.
417| Assign symbol numbers, and write definition of token names into |
418| FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
419`----------------------------------------------------------------*/
420
421void
422symbols_pack (void)
423{
424 symbols = XCALLOC (symbol_t *, nsyms);
425
426 symbols_do (symbol_check_alias_consistence, NULL);
427 symbols_do (symbol_pack, NULL);
428
429 symbols_token_translations_init ();
430
431 if (startsymbol->class == unknown_sym)
432 fatal (_("the start symbol %s is undefined"), startsymbol->tag);
433 else if (startsymbol->class == token_sym)
434 fatal (_("the start symbol %s is a token"), startsymbol->tag);
435}