]> git.saurik.com Git - bison.git/blob - src/symtab.c
fd2a8b36d0582e37e81473154e98250c28696d5c
[bison.git] / src / symtab.c
1 /* Symbol table manager for Bison.
2
3 Copyright (C) 1984, 1989, 2000, 2001, 2002, 2004, 2005, 2006, 2007,
4 2008, 2009
5 Free Software Foundation, Inc.
6
7 This file is part of Bison, the GNU Compiler Compiler.
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>. */
21
22 #include <config.h>
23 #include "system.h"
24
25 #include <hash.h>
26 #include <quotearg.h>
27
28 #include "complain.h"
29 #include "gram.h"
30 #include "symtab.h"
31
32 /*------------------------.
33 | Distinguished symbols. |
34 `------------------------*/
35
36 symbol *errtoken = NULL;
37 symbol *undeftoken = NULL;
38 symbol *endtoken = NULL;
39 symbol *accept = NULL;
40 symbol *startsymbol = NULL;
41 location startsymbol_location;
42
43 /*---------------------------------------.
44 | Default %destructor's and %printer's. |
45 `---------------------------------------*/
46
47 static code_props default_tagged_destructor = CODE_PROPS_NONE_INIT;
48 static code_props default_tagless_destructor = CODE_PROPS_NONE_INIT;
49 static code_props default_tagged_printer = CODE_PROPS_NONE_INIT;
50 static code_props default_tagless_printer = CODE_PROPS_NONE_INIT;
51
52 /*---------------------------------.
53 | Create a new symbol, named TAG. |
54 `---------------------------------*/
55
56 static symbol *
57 symbol_new (uniqstr tag, location loc)
58 {
59 symbol *res = xmalloc (sizeof *res);
60
61 uniqstr_assert (tag);
62
63 /* If the tag is not a string (starts with a double quote), check
64 that it is valid for Yacc. */
65 if (tag[0] != '\"' && tag[0] != '\'' && strchr (tag, '-'))
66 yacc_at (loc, _("POSIX Yacc forbids dashes in symbol names: %s"),
67 tag);
68
69 res->tag = tag;
70 res->location = loc;
71
72 res->type_name = NULL;
73 code_props_none_init (&res->destructor);
74 code_props_none_init (&res->printer);
75
76 res->number = NUMBER_UNDEFINED;
77 res->prec = 0;
78 res->assoc = undef_assoc;
79 res->user_token_number = USER_NUMBER_UNDEFINED;
80
81 res->alias = NULL;
82 res->class = unknown_sym;
83 res->declared = false;
84
85 if (nsyms == SYMBOL_NUMBER_MAXIMUM)
86 fatal (_("too many symbols in input grammar (limit is %d)"),
87 SYMBOL_NUMBER_MAXIMUM);
88 nsyms++;
89 return res;
90 }
91
92 /*----------------------------------------.
93 | Create a new semantic type, named TAG. |
94 `----------------------------------------*/
95
96 static semantic_type *
97 semantic_type_new (uniqstr tag)
98 {
99 semantic_type *res = xmalloc (sizeof *res);
100
101 uniqstr_assert (tag);
102 res->tag = tag;
103 code_props_none_init (&res->destructor);
104 code_props_none_init (&res->printer);
105
106 return res;
107 }
108
109
110 /*-----------------.
111 | Print a symbol. |
112 `-----------------*/
113
114 #define SYMBOL_ATTR_PRINT(Attr) \
115 if (s->Attr) \
116 fprintf (f, " %s { %s }", #Attr, s->Attr)
117
118 #define SYMBOL_CODE_PRINT(Attr) \
119 if (s->Attr.code) \
120 fprintf (f, " %s { %s }", #Attr, s->Attr.code)
121
122 void
123 symbol_print (symbol *s, FILE *f)
124 {
125 if (s)
126 {
127 fprintf (f, "\"%s\"", s->tag);
128 SYMBOL_ATTR_PRINT (type_name);
129 SYMBOL_CODE_PRINT (destructor);
130 SYMBOL_CODE_PRINT (printer);
131 }
132 else
133 fprintf (f, "<NULL>");
134 }
135
136 #undef SYMBOL_ATTR_PRINT
137 #undef SYMBOL_CODE_PRINT
138
139 /*------------------------------------------------------------------.
140 | Complain that S's WHAT is redeclared at SECOND, and was first set |
141 | at FIRST. |
142 `------------------------------------------------------------------*/
143
144 static void
145 symbol_redeclaration (symbol *s, const char *what, location first,
146 location second)
147 {
148 complain_at (second, _("%s redeclaration for %s"), what, s->tag);
149 complain_at (first, _("previous declaration"));
150 }
151
152 static void
153 semantic_type_redeclaration (semantic_type *s, const char *what, location first,
154 location second)
155 {
156 complain_at (second, _("%s redeclaration for <%s>"), what, s->tag);
157 complain_at (first, _("previous declaration"));
158 }
159
160
161
162 /*-----------------------------------------------------------------.
163 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
164 | as TYPE_NAME. |
165 `-----------------------------------------------------------------*/
166
167 void
168 symbol_type_set (symbol *sym, uniqstr type_name, location loc)
169 {
170 if (type_name)
171 {
172 if (sym->type_name)
173 symbol_redeclaration (sym, "%type", sym->type_location, loc);
174 uniqstr_assert (type_name);
175 sym->type_name = type_name;
176 sym->type_location = loc;
177 }
178 }
179
180 /*-----------------------------------.
181 | Get the CLASS associated with SYM. |
182 `-----------------------------------*/
183
184 const char *
185 symbol_class_get_string (symbol *sym)
186 {
187 if (sym->class)
188 {
189 if (sym->class == token_sym)
190 return "terminal";
191 else if (sym->class == nterm_sym)
192 return "nonterminal";
193 }
194 return "unknown";
195 }
196
197
198 /*-----------------------------------------.
199 | Set the DESTRUCTOR associated with SYM. |
200 `-----------------------------------------*/
201
202 void
203 symbol_destructor_set (symbol *sym, code_props const *destructor)
204 {
205 if (sym->destructor.code)
206 symbol_redeclaration (sym, "%destructor", sym->destructor.location,
207 destructor->location);
208 sym->destructor = *destructor;
209 }
210
211 /*------------------------------------------.
212 | Set the DESTRUCTOR associated with TYPE. |
213 `------------------------------------------*/
214
215 void
216 semantic_type_destructor_set (semantic_type *type,
217 code_props const *destructor)
218 {
219 if (type->destructor.code)
220 semantic_type_redeclaration (type, "%destructor",
221 type->destructor.location,
222 destructor->location);
223 type->destructor = *destructor;
224 }
225
226 /*---------------------------------------.
227 | Get the computed %destructor for SYM. |
228 `---------------------------------------*/
229
230 code_props const *
231 symbol_destructor_get (symbol const *sym)
232 {
233 /* Per-symbol %destructor. */
234 if (sym->destructor.code)
235 return &sym->destructor;
236
237 /* Per-type %destructor. */
238 if (sym->type_name)
239 {
240 code_props const *destructor =
241 &semantic_type_get (sym->type_name)->destructor;
242 if (destructor->code)
243 return destructor;
244 }
245
246 /* Apply default %destructor's only to user-defined symbols. */
247 if (sym->tag[0] == '$' || sym == errtoken)
248 return &code_props_none;
249
250 if (sym->type_name)
251 return &default_tagged_destructor;
252 return &default_tagless_destructor;
253 }
254
255 /*--------------------------------------.
256 | Set the PRINTER associated with SYM. |
257 `--------------------------------------*/
258
259 void
260 symbol_printer_set (symbol *sym, code_props const *printer)
261 {
262 if (sym->printer.code)
263 symbol_redeclaration (sym, "%printer",
264 sym->printer.location, printer->location);
265 sym->printer = *printer;
266 }
267
268 /*---------------------------------------.
269 | Set the PRINTER associated with TYPE. |
270 `---------------------------------------*/
271
272 void
273 semantic_type_printer_set (semantic_type *type, code_props const *printer)
274 {
275 if (type->printer.code)
276 semantic_type_redeclaration (type, "%printer",
277 type->printer.location, printer->location);
278 type->printer = *printer;
279 }
280
281 /*------------------------------------.
282 | Get the computed %printer for SYM. |
283 `------------------------------------*/
284
285 code_props const *
286 symbol_printer_get (symbol const *sym)
287 {
288 /* Per-symbol %printer. */
289 if (sym->printer.code)
290 return &sym->printer;
291
292 /* Per-type %printer. */
293 if (sym->type_name)
294 {
295 code_props const *printer = &semantic_type_get (sym->type_name)->printer;
296 if (printer->code)
297 return printer;
298 }
299
300 /* Apply the default %printer only to user-defined symbols. */
301 if (sym->tag[0] == '$' || sym == errtoken)
302 return &code_props_none;
303
304 if (sym->type_name)
305 return &default_tagged_printer;
306 return &default_tagless_printer;
307 }
308
309 /*-----------------------------------------------------------------.
310 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
311 | with UNDEF_ASSOC as ASSOC. |
312 `-----------------------------------------------------------------*/
313
314 void
315 symbol_precedence_set (symbol *sym, int prec, assoc a, location loc)
316 {
317 if (a != undef_assoc)
318 {
319 if (sym->prec != 0)
320 symbol_redeclaration (sym, assoc_to_string (a), sym->prec_location,
321 loc);
322 sym->prec = prec;
323 sym->assoc = a;
324 sym->prec_location = loc;
325 }
326
327 /* Only terminals have a precedence. */
328 symbol_class_set (sym, token_sym, loc, false);
329 }
330
331
332 /*------------------------------------.
333 | Set the CLASS associated with SYM. |
334 `------------------------------------*/
335
336 void
337 symbol_class_set (symbol *sym, symbol_class class, location loc, bool declaring)
338 {
339 if (sym->class != unknown_sym && sym->class != class)
340 {
341 complain_at (loc, _("symbol %s redefined"), sym->tag);
342 sym->declared = false;
343 }
344
345 if (class == nterm_sym && sym->class != nterm_sym)
346 sym->number = nvars++;
347 else if (class == token_sym && sym->number == NUMBER_UNDEFINED)
348 sym->number = ntokens++;
349
350 sym->class = class;
351
352 if (declaring)
353 {
354 if (sym->declared)
355 warn_at (loc, _("symbol %s redeclared"), sym->tag);
356 sym->declared = true;
357 }
358 }
359
360
361 /*------------------------------------------------.
362 | Set the USER_TOKEN_NUMBER associated with SYM. |
363 `------------------------------------------------*/
364
365 void
366 symbol_user_token_number_set (symbol *sym, int user_token_number, location loc)
367 {
368 int *user_token_numberp;
369
370 if (sym->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
371 user_token_numberp = &sym->user_token_number;
372 else
373 user_token_numberp = &sym->alias->user_token_number;
374 if (*user_token_numberp != USER_NUMBER_UNDEFINED
375 && *user_token_numberp != user_token_number)
376 complain_at (loc, _("redefining user token number of %s"), sym->tag);
377
378 *user_token_numberp = user_token_number;
379 /* User defined $end token? */
380 if (user_token_number == 0)
381 {
382 endtoken = sym;
383 endtoken->number = 0;
384 /* It is always mapped to 0, so it was already counted in
385 NTOKENS. */
386 --ntokens;
387 }
388 }
389
390
391 /*----------------------------------------------------------.
392 | If SYM is not defined, report an error, and consider it a |
393 | nonterminal. |
394 `----------------------------------------------------------*/
395
396 static inline bool
397 symbol_check_defined (symbol *sym)
398 {
399 if (sym->class == unknown_sym)
400 {
401 complain_at
402 (sym->location,
403 _("symbol %s is used, but is not defined as a token and has no rules"),
404 sym->tag);
405 sym->class = nterm_sym;
406 sym->number = nvars++;
407 }
408
409 return true;
410 }
411
412 static bool
413 symbol_check_defined_processor (void *sym, void *null ATTRIBUTE_UNUSED)
414 {
415 return symbol_check_defined (sym);
416 }
417
418
419 void
420 symbol_make_alias (symbol *sym, symbol *str, location loc)
421 {
422 if (str->alias)
423 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
424 str->tag);
425 else if (sym->alias)
426 warn_at (loc, _("symbol `%s' given more than one literal string"),
427 sym->tag);
428 else
429 {
430 str->class = token_sym;
431 str->user_token_number = sym->user_token_number;
432 sym->user_token_number = USER_NUMBER_HAS_STRING_ALIAS;
433 str->alias = sym;
434 sym->alias = str;
435 str->number = sym->number;
436 symbol_type_set (str, sym->type_name, loc);
437 }
438 }
439
440
441 /*---------------------------------------------------------.
442 | Check that THIS, and its alias, have same precedence and |
443 | associativity. |
444 `---------------------------------------------------------*/
445
446 static inline void
447 symbol_check_alias_consistency (symbol *this)
448 {
449 symbol *sym = this;
450 symbol *str = this->alias;
451
452 /* Check only the symbol in the symbol-string pair. */
453 if (!(this->alias
454 && this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS))
455 return;
456
457 if (str->type_name != sym->type_name)
458 {
459 if (str->type_name)
460 symbol_type_set (sym, str->type_name, str->type_location);
461 else
462 symbol_type_set (str, sym->type_name, sym->type_location);
463 }
464
465
466 if (str->destructor.code || sym->destructor.code)
467 {
468 if (str->destructor.code)
469 symbol_destructor_set (sym, &str->destructor);
470 else
471 symbol_destructor_set (str, &sym->destructor);
472 }
473
474 if (str->printer.code || sym->printer.code)
475 {
476 if (str->printer.code)
477 symbol_printer_set (sym, &str->printer);
478 else
479 symbol_printer_set (str, &sym->printer);
480 }
481
482 if (sym->prec || str->prec)
483 {
484 if (str->prec)
485 symbol_precedence_set (sym, str->prec, str->assoc,
486 str->prec_location);
487 else
488 symbol_precedence_set (str, sym->prec, sym->assoc,
489 sym->prec_location);
490 }
491 }
492
493 static bool
494 symbol_check_alias_consistency_processor (void *this,
495 void *null ATTRIBUTE_UNUSED)
496 {
497 symbol_check_alias_consistency (this);
498 return true;
499 }
500
501
502 /*-------------------------------------------------------------------.
503 | Assign a symbol number, and write the definition of the token name |
504 | into FDEFINES. Put in SYMBOLS. |
505 `-------------------------------------------------------------------*/
506
507 static inline bool
508 symbol_pack (symbol *this)
509 {
510 if (this->class == nterm_sym)
511 {
512 this->number += ntokens;
513 }
514 else if (this->alias)
515 {
516 /* This symbol and its alias are a single token defn.
517 Allocate a tokno, and assign to both check agreement of
518 prec and assoc fields and make both the same */
519 if (this->number == NUMBER_UNDEFINED)
520 {
521 if (this == endtoken || this->alias == endtoken)
522 this->number = this->alias->number = 0;
523 else
524 {
525 aver (this->alias->number != NUMBER_UNDEFINED);
526 this->number = this->alias->number;
527 }
528 }
529 /* Do not do processing below for USER_NUMBER_HAS_STRING_ALIASes. */
530 if (this->user_token_number == USER_NUMBER_HAS_STRING_ALIAS)
531 return true;
532 }
533 else /* this->class == token_sym */
534 aver (this->number != NUMBER_UNDEFINED);
535
536 symbols[this->number] = this;
537 return true;
538 }
539
540 static bool
541 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
542 {
543 return symbol_pack (this);
544 }
545
546
547 static void
548 user_token_number_redeclaration (int num, symbol *first, symbol *second)
549 {
550 /* User token numbers are not assigned during the parsing, but in a
551 second step, via a (nondeterministic) traversal of the symbol
552 hash table.
553
554 Make errors deterministic: keep the first declaration first. */
555 if (location_cmp (first->location, second->location) > 0)
556 {
557 symbol* tmp = first;
558 first = second;
559 second = tmp;
560 }
561 complain_at (second->location,
562 _("user token number %d redeclaration for %s"),
563 num, second->tag);
564 complain_at (first->location, _("previous declaration for %s"),
565 first->tag);
566 }
567
568 /*--------------------------------------------------.
569 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
570 `--------------------------------------------------*/
571
572 static inline bool
573 symbol_translation (symbol *this)
574 {
575 /* Non-terminal? */
576 if (this->class == token_sym
577 && this->user_token_number != USER_NUMBER_HAS_STRING_ALIAS)
578 {
579 /* A token which translation has already been set? */
580 if (token_translations[this->user_token_number] != undeftoken->number)
581 user_token_number_redeclaration
582 (this->user_token_number,
583 symbols[token_translations[this->user_token_number]],
584 this);
585
586 token_translations[this->user_token_number] = this->number;
587 }
588
589 return true;
590 }
591
592 static bool
593 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
594 {
595 return symbol_translation (this);
596 }
597
598
599 /*---------------------------------------.
600 | Symbol and semantic type hash tables. |
601 `---------------------------------------*/
602
603 /* Initial capacity of symbol and semantic type hash table. */
604 #define HT_INITIAL_CAPACITY 257
605
606 static struct hash_table *symbol_table = NULL;
607 static struct hash_table *semantic_type_table = NULL;
608
609 static inline bool
610 hash_compare_symbol (const symbol *m1, const symbol *m2)
611 {
612 /* Since tags are unique, we can compare the pointers themselves. */
613 return UNIQSTR_EQ (m1->tag, m2->tag);
614 }
615
616 static inline bool
617 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
618 {
619 /* Since names are unique, we can compare the pointers themselves. */
620 return UNIQSTR_EQ (m1->tag, m2->tag);
621 }
622
623 static bool
624 hash_symbol_comparator (void const *m1, void const *m2)
625 {
626 return hash_compare_symbol (m1, m2);
627 }
628
629 static bool
630 hash_semantic_type_comparator (void const *m1, void const *m2)
631 {
632 return hash_compare_semantic_type (m1, m2);
633 }
634
635 static inline size_t
636 hash_symbol (const symbol *m, size_t tablesize)
637 {
638 /* Since tags are unique, we can hash the pointer itself. */
639 return ((uintptr_t) m->tag) % tablesize;
640 }
641
642 static inline size_t
643 hash_semantic_type (const semantic_type *m, size_t tablesize)
644 {
645 /* Since names are unique, we can hash the pointer itself. */
646 return ((uintptr_t) m->tag) % tablesize;
647 }
648
649 static size_t
650 hash_symbol_hasher (void const *m, size_t tablesize)
651 {
652 return hash_symbol (m, tablesize);
653 }
654
655 static size_t
656 hash_semantic_type_hasher (void const *m, size_t tablesize)
657 {
658 return hash_semantic_type (m, tablesize);
659 }
660
661 /*-------------------------------.
662 | Create the symbol hash table. |
663 `-------------------------------*/
664
665 void
666 symbols_new (void)
667 {
668 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
669 NULL,
670 hash_symbol_hasher,
671 hash_symbol_comparator,
672 free);
673 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
674 NULL,
675 hash_semantic_type_hasher,
676 hash_semantic_type_comparator,
677 free);
678 }
679
680
681 /*----------------------------------------------------------------.
682 | Find the symbol named KEY, and return it. If it does not exist |
683 | yet, create it. |
684 `----------------------------------------------------------------*/
685
686 symbol *
687 symbol_from_uniqstr (const uniqstr key, location loc)
688 {
689 symbol probe;
690 symbol *entry;
691
692 probe.tag = key;
693 entry = hash_lookup (symbol_table, &probe);
694
695 if (!entry)
696 {
697 /* First insertion in the hash. */
698 entry = symbol_new (key, loc);
699 if (!hash_insert (symbol_table, entry))
700 xalloc_die ();
701 }
702 return entry;
703 }
704
705
706 /*-----------------------------------------------------------------------.
707 | Find the semantic type named KEY, and return it. If it does not exist |
708 | yet, create it. |
709 `-----------------------------------------------------------------------*/
710
711 semantic_type *
712 semantic_type_from_uniqstr (const uniqstr key)
713 {
714 semantic_type probe;
715 semantic_type *entry;
716
717 probe.tag = key;
718 entry = hash_lookup (semantic_type_table, &probe);
719
720 if (!entry)
721 {
722 /* First insertion in the hash. */
723 entry = semantic_type_new (key);
724 if (!hash_insert (semantic_type_table, entry))
725 xalloc_die ();
726 }
727 return entry;
728 }
729
730
731 /*----------------------------------------------------------------.
732 | Find the symbol named KEY, and return it. If it does not exist |
733 | yet, create it. |
734 `----------------------------------------------------------------*/
735
736 symbol *
737 symbol_get (const char *key, location loc)
738 {
739 return symbol_from_uniqstr (uniqstr_new (key), loc);
740 }
741
742
743 /*-----------------------------------------------------------------------.
744 | Find the semantic type named KEY, and return it. If it does not exist |
745 | yet, create it. |
746 `-----------------------------------------------------------------------*/
747
748 semantic_type *
749 semantic_type_get (const char *key)
750 {
751 return semantic_type_from_uniqstr (uniqstr_new (key));
752 }
753
754
755 /*------------------------------------------------------------------.
756 | Generate a dummy nonterminal, whose name cannot conflict with the |
757 | user's names. |
758 `------------------------------------------------------------------*/
759
760 symbol *
761 dummy_symbol_get (location loc)
762 {
763 /* Incremented for each generated symbol. */
764 static int dummy_count = 0;
765 static char buf[256];
766
767 symbol *sym;
768
769 sprintf (buf, "$@%d", ++dummy_count);
770 sym = symbol_get (buf, loc);
771 sym->class = nterm_sym;
772 sym->number = nvars++;
773 return sym;
774 }
775
776 bool
777 symbol_is_dummy (const symbol *sym)
778 {
779 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
780 }
781
782 /*-------------------.
783 | Free the symbols. |
784 `-------------------*/
785
786 void
787 symbols_free (void)
788 {
789 hash_free (symbol_table);
790 hash_free (semantic_type_table);
791 free (symbols);
792 }
793
794
795 /*---------------------------------------------------------------.
796 | Look for undefined symbols, report an error, and consider them |
797 | terminals. |
798 `---------------------------------------------------------------*/
799
800 static void
801 symbols_do (Hash_processor processor, void *processor_data)
802 {
803 hash_do_for_each (symbol_table, processor, processor_data);
804 }
805
806
807 /*--------------------------------------------------------------.
808 | Check that all the symbols are defined. Report any undefined |
809 | symbols and consider them nonterminals. |
810 `--------------------------------------------------------------*/
811
812 void
813 symbols_check_defined (void)
814 {
815 symbols_do (symbol_check_defined_processor, NULL);
816 }
817
818 /*------------------------------------------------------------------.
819 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
820 | number. |
821 `------------------------------------------------------------------*/
822
823 static void
824 symbols_token_translations_init (void)
825 {
826 bool num_256_available_p = true;
827 int i;
828
829 /* Find the highest user token number, and whether 256, the POSIX
830 preferred user token number for the error token, is used. */
831 max_user_token_number = 0;
832 for (i = 0; i < ntokens; ++i)
833 {
834 symbol *this = symbols[i];
835 if (this->user_token_number != USER_NUMBER_UNDEFINED)
836 {
837 if (this->user_token_number > max_user_token_number)
838 max_user_token_number = this->user_token_number;
839 if (this->user_token_number == 256)
840 num_256_available_p = false;
841 }
842 }
843
844 /* If 256 is not used, assign it to error, to follow POSIX. */
845 if (num_256_available_p
846 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
847 errtoken->user_token_number = 256;
848
849 /* Set the missing user numbers. */
850 if (max_user_token_number < 256)
851 max_user_token_number = 256;
852
853 for (i = 0; i < ntokens; ++i)
854 {
855 symbol *this = symbols[i];
856 if (this->user_token_number == USER_NUMBER_UNDEFINED)
857 this->user_token_number = ++max_user_token_number;
858 if (this->user_token_number > max_user_token_number)
859 max_user_token_number = this->user_token_number;
860 }
861
862 token_translations = xnmalloc (max_user_token_number + 1,
863 sizeof *token_translations);
864
865 /* Initialize all entries for literal tokens to 2, the internal
866 token number for $undefined, which represents all invalid inputs.
867 */
868 for (i = 0; i < max_user_token_number + 1; i++)
869 token_translations[i] = undeftoken->number;
870 symbols_do (symbol_translation_processor, NULL);
871 }
872
873
874 /*----------------------------------------------------------------.
875 | Assign symbol numbers, and write definition of token names into |
876 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
877 `----------------------------------------------------------------*/
878
879 void
880 symbols_pack (void)
881 {
882 symbols_do (symbol_check_alias_consistency_processor, NULL);
883
884 symbols = xcalloc (nsyms, sizeof *symbols);
885 symbols_do (symbol_pack_processor, NULL);
886
887 /* Aliases leave empty slots in symbols, so remove them. */
888 {
889 int writei;
890 int readi;
891 int nsyms_old = nsyms;
892 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
893 {
894 if (symbols[readi] == NULL)
895 {
896 nsyms -= 1;
897 ntokens -= 1;
898 }
899 else
900 {
901 symbols[writei] = symbols[readi];
902 symbols[writei]->number = writei;
903 if (symbols[writei]->alias)
904 symbols[writei]->alias->number = writei;
905 writei += 1;
906 }
907 }
908 }
909 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
910
911 symbols_token_translations_init ();
912
913 if (startsymbol->class == unknown_sym)
914 fatal_at (startsymbol_location,
915 _("the start symbol %s is undefined"),
916 startsymbol->tag);
917 else if (startsymbol->class == token_sym)
918 fatal_at (startsymbol_location,
919 _("the start symbol %s is a token"),
920 startsymbol->tag);
921 }
922
923
924 /*--------------------------------------------------.
925 | Set default tagged/tagless %destructor/%printer. |
926 `--------------------------------------------------*/
927
928 void
929 default_tagged_destructor_set (code_props const *destructor)
930 {
931 if (default_tagged_destructor.code)
932 {
933 complain_at (destructor->location,
934 _("redeclaration for default tagged %%destructor"));
935 complain_at (default_tagged_destructor.location,
936 _("previous declaration"));
937 }
938 default_tagged_destructor = *destructor;
939 }
940
941 void
942 default_tagless_destructor_set (code_props const *destructor)
943 {
944 if (default_tagless_destructor.code)
945 {
946 complain_at (destructor->location,
947 _("redeclaration for default tagless %%destructor"));
948 complain_at (default_tagless_destructor.location,
949 _("previous declaration"));
950 }
951 default_tagless_destructor = *destructor;
952 }
953
954 void
955 default_tagged_printer_set (code_props const *printer)
956 {
957 if (default_tagged_printer.code)
958 {
959 complain_at (printer->location,
960 _("redeclaration for default tagged %%printer"));
961 complain_at (default_tagged_printer.location,
962 _("previous declaration"));
963 }
964 default_tagged_printer = *printer;
965 }
966
967 void
968 default_tagless_printer_set (code_props const *printer)
969 {
970 if (default_tagless_printer.code)
971 {
972 complain_at (printer->location,
973 _("redeclaration for default tagless %%printer"));
974 complain_at (default_tagless_printer.location,
975 _("previous declaration"));
976 }
977 default_tagless_printer = *printer;
978 }