]> git.saurik.com Git - bison.git/blob - src/symtab.c
* src/reader.c (parse_action): Don't store directly into the
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison,
2 Copyright (C) 1984, 1989, 2000, 2001, 2002 Free Software Foundation, Inc.
3
4 This file is part of Bison, the GNU Compiler Compiler.
5
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.
10
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.
15
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. */
20
21
22 #include "system.h"
23 #include "hash.h"
24 #include "complain.h"
25 #include "symtab.h"
26 #include "gram.h"
27
28 /*------------------------.
29 | Distinguished symbols. |
30 `------------------------*/
31
32 symbol_t *errtoken = NULL;
33 symbol_t *undeftoken = NULL;
34 symbol_t *eoftoken = NULL;
35 symbol_t *axiom = NULL;
36 symbol_t *startsymbol = NULL;
37
38 /*---------------------------------.
39 | Create a new symbol, named TAG. |
40 `---------------------------------*/
41
42 static symbol_t *
43 symbol_new (const char *tag)
44 {
45 symbol_t *res = XMALLOC (symbol_t, 1);
46
47 res->tag = xstrdup (tag);
48 res->type_name = NULL;
49 res->number = NUMBER_UNDEFINED;
50 res->prec = 0;
51 res->assoc = right_assoc;
52 res->user_token_number = USER_NUMBER_UNDEFINED;
53 res->alias = NULL;
54 res->class = unknown_sym;
55
56 nsyms++;
57
58 return res;
59 }
60
61
62 /*-----------------------------------------.
63 | Set the TYPE_NAME associated to SYMBOL. |
64 `-----------------------------------------*/
65
66 void
67 symbol_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
79 void
80 symbol_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
90 /*-------------------------------------.
91 | Set the CLASS associated to SYMBOL. |
92 `-------------------------------------*/
93
94 void
95 symbol_class_set (symbol_t *symbol, symbol_class class)
96 {
97 if (symbol->class != unknown_sym && symbol->class != class)
98 complain (_("symbol %s redefined"), symbol->tag);
99
100 if (class == nterm_sym && symbol->class != nterm_sym)
101 symbol->number = nvars++;
102 else if (class == token_sym && symbol->number == NUMBER_UNDEFINED)
103 symbol->number = ntokens++;
104
105 symbol->class = class;
106 }
107
108
109 /*-------------------------------------------------.
110 | Set the USER_TOKEN_NUMBER associated to SYMBOL. |
111 `-------------------------------------------------*/
112
113 void
114 symbol_user_token_number_set (symbol_t *symbol, int user_token_number)
115 {
116 assert (symbol->class == token_sym);
117
118 if (symbol->user_token_number != USER_NUMBER_UNDEFINED
119 && symbol->user_token_number != user_token_number)
120 complain (_("redefining user token number of %s"), symbol->tag);
121
122 symbol->user_token_number = user_token_number;
123 /* User defined EOF token? */
124 if (user_token_number == 0)
125 {
126 eoftoken = symbol;
127 eoftoken->number = 0;
128 /* It is always mapped to 0, so it was already counted in
129 NTOKENS. */
130 --ntokens;
131 }
132 }
133
134
135 /*------------.
136 | Free THIS. |
137 `------------*/
138
139 static void
140 symbol_free (symbol_t *this)
141 {
142 #if 0
143 /* This causes crashes because one string can appear more
144 than once. */
145 XFREE (this->type_name);
146 #endif
147 XFREE (this->tag);
148 XFREE (this);
149 }
150
151
152 /*-----------------------------------------------------------.
153 | If THIS is not defined, report an error, and consider it a |
154 | nonterminal. |
155 `-----------------------------------------------------------*/
156
157 static bool
158 symbol_check_defined (symbol_t *this)
159 {
160 if (this->class == unknown_sym)
161 {
162 complain
163 (_("symbol %s is used, but is not defined as a token and has no rules"),
164 this->tag);
165 this->class = nterm_sym;
166 this->number = nvars++;
167 }
168
169 return TRUE;
170 }
171
172
173 /*-------------------------------------------------------------------.
174 | Declare the new SYMBOL. Make it an alias of SYMVAL, and type them |
175 | with TYPENAME. |
176 `-------------------------------------------------------------------*/
177
178 void
179 symbol_make_alias (symbol_t *symbol, symbol_t *symval, char *typename)
180 {
181 if (symval->alias)
182 warn (_("symbol `%s' used more than once as a literal string"),
183 symval->tag);
184 else if (symbol->alias)
185 warn (_("symbol `%s' given more than one literal string"),
186 symbol->tag);
187 else
188 {
189 symval->class = token_sym;
190 symval->type_name = typename;
191 symval->user_token_number = symbol->user_token_number;
192 symbol->user_token_number = USER_NUMBER_ALIAS;
193 symval->alias = symbol;
194 symbol->alias = symval;
195 /* symbol and symval combined are only one symbol */
196 nsyms--;
197 ntokens--;
198 assert (ntokens == symbol->number || ntokens == symval->number);
199 symbol->number = symval->number =
200 (symval->number < symbol->number) ? symval->number : symbol->number;
201 }
202 }
203
204
205 /*---------------------------------------------------------.
206 | Check that THIS, and its alias, have same precedence and |
207 | associativity. |
208 `---------------------------------------------------------*/
209
210 static bool
211 symbol_check_alias_consistence (symbol_t *this)
212 {
213 /* Check only those who _are_ the aliases. */
214 if (this->alias && this->user_token_number == USER_NUMBER_ALIAS)
215 {
216 if (this->prec != this->alias->prec)
217 {
218 if (this->prec != 0 && this->alias->prec != 0)
219 complain (_("conflicting precedences for %s and %s"),
220 this->tag, this->alias->tag);
221 if (this->prec != 0)
222 this->alias->prec = this->prec;
223 else
224 this->prec = this->alias->prec;
225 }
226
227 if (this->assoc != this->alias->assoc)
228 {
229 if (this->assoc != 0 && this->alias->assoc != 0)
230 complain (_("conflicting assoc values for %s and %s"),
231 this->tag, this->alias->tag);
232 if (this->assoc != 0)
233 this->alias->assoc = this->assoc;
234 else
235 this->assoc = this->alias->assoc;
236 }
237 }
238 return TRUE;
239 }
240
241
242 /*-------------------------------------------------------------------.
243 | Assign a symbol number, and write the definition of the token name |
244 | into FDEFINES. Put in SYMBOLS. |
245 `-------------------------------------------------------------------*/
246
247 static bool
248 symbol_pack (symbol_t *this)
249 {
250 if (this->class == nterm_sym)
251 {
252 this->number += ntokens;
253 }
254 else if (this->alias)
255 {
256 /* This symbol and its alias are a single token defn.
257 Allocate a tokno, and assign to both check agreement of
258 prec and assoc fields and make both the same */
259 if (this->number == NUMBER_UNDEFINED)
260 {
261 if (this == eoftoken || this->alias == eoftoken)
262 this->number = this->alias->number = 0;
263 else
264 {
265 assert (this->alias->number != NUMBER_UNDEFINED);
266 this->number = this->alias->number;
267 }
268 }
269 /* Do not do processing below for USER_NUMBER_ALIASs. */
270 if (this->user_token_number == USER_NUMBER_ALIAS)
271 return TRUE;
272 }
273 else /* this->class == token_sym */
274 {
275 assert (this->number != NUMBER_UNDEFINED);
276 }
277
278 symbols[this->number] = this;
279 return TRUE;
280 }
281
282
283
284
285 /*--------------------------------------------------.
286 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
287 `--------------------------------------------------*/
288
289 static bool
290 symbol_translation (symbol_t *this)
291 {
292 /* Non-terminal? */
293 if (this->class == token_sym
294 && this->user_token_number != USER_NUMBER_ALIAS)
295 {
296 /* A token which translation has already been set? */
297 if (token_translations[this->user_token_number] != undeftoken->number)
298 complain (_("tokens %s and %s both assigned number %d"),
299 symbols[token_translations[this->user_token_number]]->tag,
300 this->tag, this->user_token_number);
301
302 token_translations[this->user_token_number] = this->number;
303 }
304
305 return TRUE;
306 }
307
308
309 /*----------------------.
310 | A symbol hash table. |
311 `----------------------*/
312
313 /* Initial capacity of symbols hash table. */
314 #define HT_INITIAL_CAPACITY 257
315
316 static struct hash_table *symbol_table = NULL;
317
318 static bool
319 hash_compare_symbol_t (const symbol_t *m1, const symbol_t *m2)
320 {
321 return strcmp (m1->tag, m2->tag) ? FALSE : TRUE;
322 }
323
324 static unsigned int
325 hash_symbol_t (const symbol_t *m, unsigned int tablesize)
326 {
327 return hash_string (m->tag, tablesize);
328 }
329
330
331 /*-------------------------------.
332 | Create the symbol hash table. |
333 `-------------------------------*/
334
335 void
336 symbols_new (void)
337 {
338 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
339 NULL,
340 (Hash_hasher) hash_symbol_t,
341 (Hash_comparator) hash_compare_symbol_t,
342 (Hash_data_freer) symbol_free);
343 }
344
345
346 /*----------------------------------------------------------------.
347 | Find the symbol named KEY, and return it. If it does not exist |
348 | yet, create it. |
349 `----------------------------------------------------------------*/
350
351 symbol_t *
352 getsym (const char *key)
353 {
354 symbol_t probe;
355 symbol_t *entry;
356
357 (const char *) probe.tag = key;
358 entry = hash_lookup (symbol_table, &probe);
359
360 if (!entry)
361 {
362 /* First insertion in the hash. */
363 entry = symbol_new (key);
364 hash_insert (symbol_table, entry);
365 }
366 return entry;
367 }
368
369
370 /*-------------------.
371 | Free the symbols. |
372 `-------------------*/
373
374 void
375 symbols_free (void)
376 {
377 hash_free (symbol_table);
378 }
379
380
381 /*---------------------------------------------------------------.
382 | Look for undefined symbols, report an error, and consider them |
383 | terminals. |
384 `---------------------------------------------------------------*/
385
386 void
387 symbols_do (symbol_processor processor, void *processor_data)
388 {
389 hash_do_for_each (symbol_table,
390 (Hash_processor) processor,
391 processor_data);
392 }
393
394
395 /*--------------------------------------------------------------.
396 | Check that all the symbols are defined. Report any undefined |
397 | symbols and consider them nonterminals. |
398 `--------------------------------------------------------------*/
399
400 void
401 symbols_check_defined (void)
402 {
403 symbols_do (symbol_check_defined, NULL);
404 }
405
406 /*------------------------------------------------------------------.
407 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
408 | number. |
409 `------------------------------------------------------------------*/
410
411 static void
412 symbols_token_translations_init (void)
413 {
414 int num_256_available_p = TRUE;
415 int i;
416
417 /* Find the highest user token number, and whether 256, the POSIX
418 preferred user token number for the error token, is used. */
419 max_user_token_number = 0;
420 for (i = 0; i < ntokens; ++i)
421 {
422 symbol_t *this = symbols[i];
423 if (this->user_token_number != USER_NUMBER_UNDEFINED)
424 {
425 if (this->user_token_number > max_user_token_number)
426 max_user_token_number = this->user_token_number;
427 if (this->user_token_number == 256)
428 num_256_available_p = FALSE;
429 }
430 }
431
432 /* If 256 is not used, assign it to error, to follow POSIX. */
433 if (num_256_available_p
434 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
435 errtoken->user_token_number = 256;
436
437 /* Set the missing user numbers. */
438 if (max_user_token_number < 256)
439 max_user_token_number = 256;
440
441 for (i = 0; i < ntokens; ++i)
442 {
443 symbol_t *this = symbols[i];
444 if (this->user_token_number == USER_NUMBER_UNDEFINED)
445 this->user_token_number = ++max_user_token_number;
446 if (this->user_token_number > max_user_token_number)
447 max_user_token_number = this->user_token_number;
448 }
449
450 token_translations = XCALLOC (symbol_number_t, max_user_token_number + 1);
451
452 /* Initialize all entries for literal tokens to 2, the internal
453 token number for $undefined., which represents all invalid
454 inputs. */
455 for (i = 0; i < max_user_token_number + 1; i++)
456 token_translations[i] = undeftoken->number;
457 symbols_do (symbol_translation, NULL);
458 }
459
460
461 /*----------------------------------------------------------------.
462 | Assign symbol numbers, and write definition of token names into |
463 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
464 `----------------------------------------------------------------*/
465
466 void
467 symbols_pack (void)
468 {
469 symbols = XCALLOC (symbol_t *, nsyms);
470
471 symbols_do (symbol_check_alias_consistence, NULL);
472 symbols_do (symbol_pack, NULL);
473
474 symbols_token_translations_init ();
475
476 if (startsymbol->class == unknown_sym)
477 fatal (_("the start symbol %s is undefined"), startsymbol->tag);
478 else if (startsymbol->class == token_sym)
479 fatal (_("the start symbol %s is a token"), startsymbol->tag);
480 }