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