]> git.saurik.com Git - bison.git/blob - src/symtab.c
deterministic user-token-number redeclaration errors.
[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_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 /*------------------------------------------------------------------.
420 | Declare the new symbol SYM. Make it an alias of SYMVAL, and type |
421 | SYMVAL with SYM's type. |
422 `------------------------------------------------------------------*/
423
424 void
425 symbol_make_alias (symbol *sym, symbol *symval, location loc)
426 {
427 if (symval->alias)
428 warn_at (loc, _("symbol `%s' used more than once as a literal string"),
429 symval->tag);
430 else if (sym->alias)
431 warn_at (loc, _("symbol `%s' given more than one literal string"),
432 sym->tag);
433 else
434 {
435 symval->class = token_sym;
436 symval->user_token_number = sym->user_token_number;
437 sym->user_token_number = USER_NUMBER_ALIAS;
438 symval->alias = sym;
439 sym->alias = symval;
440 symval->number = sym->number;
441 symbol_type_set (symval, sym->type_name, loc);
442 }
443 }
444
445
446 /*---------------------------------------------------------.
447 | Check that THIS, and its alias, have same precedence and |
448 | associativity. |
449 `---------------------------------------------------------*/
450
451 static inline void
452 symbol_check_alias_consistency (symbol *this)
453 {
454 symbol *alias = this;
455 symbol *orig = this->alias;
456
457 /* Check only those that _are_ the aliases. */
458 if (!(this->alias && this->user_token_number == USER_NUMBER_ALIAS))
459 return;
460
461 if (orig->type_name != alias->type_name)
462 {
463 if (orig->type_name)
464 symbol_type_set (alias, orig->type_name, orig->type_location);
465 else
466 symbol_type_set (orig, alias->type_name, alias->type_location);
467 }
468
469
470 if (orig->destructor.code || alias->destructor.code)
471 {
472 if (orig->destructor.code)
473 symbol_destructor_set (alias, &orig->destructor);
474 else
475 symbol_destructor_set (orig, &alias->destructor);
476 }
477
478 if (orig->printer.code || alias->printer.code)
479 {
480 if (orig->printer.code)
481 symbol_printer_set (alias, &orig->printer);
482 else
483 symbol_printer_set (orig, &alias->printer);
484 }
485
486 if (alias->prec || orig->prec)
487 {
488 if (orig->prec)
489 symbol_precedence_set (alias, orig->prec, orig->assoc,
490 orig->prec_location);
491 else
492 symbol_precedence_set (orig, alias->prec, alias->assoc,
493 alias->prec_location);
494 }
495 }
496
497 static bool
498 symbol_check_alias_consistency_processor (void *this,
499 void *null ATTRIBUTE_UNUSED)
500 {
501 symbol_check_alias_consistency (this);
502 return true;
503 }
504
505
506 /*-------------------------------------------------------------------.
507 | Assign a symbol number, and write the definition of the token name |
508 | into FDEFINES. Put in SYMBOLS. |
509 `-------------------------------------------------------------------*/
510
511 static inline bool
512 symbol_pack (symbol *this)
513 {
514 if (this->class == nterm_sym)
515 {
516 this->number += ntokens;
517 }
518 else if (this->alias)
519 {
520 /* This symbol and its alias are a single token defn.
521 Allocate a tokno, and assign to both check agreement of
522 prec and assoc fields and make both the same */
523 if (this->number == NUMBER_UNDEFINED)
524 {
525 if (this == endtoken || this->alias == endtoken)
526 this->number = this->alias->number = 0;
527 else
528 {
529 aver (this->alias->number != NUMBER_UNDEFINED);
530 this->number = this->alias->number;
531 }
532 }
533 /* Do not do processing below for USER_NUMBER_ALIASes. */
534 if (this->user_token_number == USER_NUMBER_ALIAS)
535 return true;
536 }
537 else /* this->class == token_sym */
538 aver (this->number != NUMBER_UNDEFINED);
539
540 symbols[this->number] = this;
541 return true;
542 }
543
544 static bool
545 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED)
546 {
547 return symbol_pack (this);
548 }
549
550
551 static void
552 user_token_number_redeclaration (int num, symbol *first, symbol *second)
553 {
554 /* User token numbers are not assigned during the parsing, but in a
555 second step, via a (nondeterministic) traversal of the symbol
556 hash table.
557
558 Make errors deterministic: keep the first declaration first. */
559 if (location_cmp (first->location, second->location) > 0)
560 {
561 symbol* tmp = first;
562 first = second;
563 second = tmp;
564 }
565 complain_at (second->location,
566 _("user token number %d redeclaration for %s"),
567 num, second->tag);
568 complain_at (first->location, _("previous declaration for %s"),
569 first->tag);
570 }
571
572 /*--------------------------------------------------.
573 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
574 `--------------------------------------------------*/
575
576 static inline bool
577 symbol_translation (symbol *this)
578 {
579 /* Non-terminal? */
580 if (this->class == token_sym
581 && this->user_token_number != USER_NUMBER_ALIAS)
582 {
583 /* A token which translation has already been set? */
584 if (token_translations[this->user_token_number] != undeftoken->number)
585 user_token_number_redeclaration
586 (this->user_token_number,
587 symbols[token_translations[this->user_token_number]],
588 this);
589
590 token_translations[this->user_token_number] = this->number;
591 }
592
593 return true;
594 }
595
596 static bool
597 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED)
598 {
599 return symbol_translation (this);
600 }
601
602
603 /*---------------------------------------.
604 | Symbol and semantic type hash tables. |
605 `---------------------------------------*/
606
607 /* Initial capacity of symbol and semantic type hash table. */
608 #define HT_INITIAL_CAPACITY 257
609
610 static struct hash_table *symbol_table = NULL;
611 static struct hash_table *semantic_type_table = NULL;
612
613 static inline bool
614 hash_compare_symbol (const symbol *m1, const symbol *m2)
615 {
616 /* Since tags are unique, we can compare the pointers themselves. */
617 return UNIQSTR_EQ (m1->tag, m2->tag);
618 }
619
620 static inline bool
621 hash_compare_semantic_type (const semantic_type *m1, const semantic_type *m2)
622 {
623 /* Since names are unique, we can compare the pointers themselves. */
624 return UNIQSTR_EQ (m1->tag, m2->tag);
625 }
626
627 static bool
628 hash_symbol_comparator (void const *m1, void const *m2)
629 {
630 return hash_compare_symbol (m1, m2);
631 }
632
633 static bool
634 hash_semantic_type_comparator (void const *m1, void const *m2)
635 {
636 return hash_compare_semantic_type (m1, m2);
637 }
638
639 static inline size_t
640 hash_symbol (const symbol *m, size_t tablesize)
641 {
642 /* Since tags are unique, we can hash the pointer itself. */
643 return ((uintptr_t) m->tag) % tablesize;
644 }
645
646 static inline size_t
647 hash_semantic_type (const semantic_type *m, size_t tablesize)
648 {
649 /* Since names are unique, we can hash the pointer itself. */
650 return ((uintptr_t) m->tag) % tablesize;
651 }
652
653 static size_t
654 hash_symbol_hasher (void const *m, size_t tablesize)
655 {
656 return hash_symbol (m, tablesize);
657 }
658
659 static size_t
660 hash_semantic_type_hasher (void const *m, size_t tablesize)
661 {
662 return hash_semantic_type (m, tablesize);
663 }
664
665 /*-------------------------------.
666 | Create the symbol hash table. |
667 `-------------------------------*/
668
669 void
670 symbols_new (void)
671 {
672 symbol_table = hash_initialize (HT_INITIAL_CAPACITY,
673 NULL,
674 hash_symbol_hasher,
675 hash_symbol_comparator,
676 free);
677 semantic_type_table = hash_initialize (HT_INITIAL_CAPACITY,
678 NULL,
679 hash_semantic_type_hasher,
680 hash_semantic_type_comparator,
681 free);
682 }
683
684
685 /*----------------------------------------------------------------.
686 | Find the symbol named KEY, and return it. If it does not exist |
687 | yet, create it. |
688 `----------------------------------------------------------------*/
689
690 symbol *
691 symbol_from_uniqstr (const uniqstr key, location loc)
692 {
693 symbol probe;
694 symbol *entry;
695
696 probe.tag = key;
697 entry = hash_lookup (symbol_table, &probe);
698
699 if (!entry)
700 {
701 /* First insertion in the hash. */
702 entry = symbol_new (key, loc);
703 if (!hash_insert (symbol_table, entry))
704 xalloc_die ();
705 }
706 return entry;
707 }
708
709
710 /*-----------------------------------------------------------------------.
711 | Find the semantic type named KEY, and return it. If it does not exist |
712 | yet, create it. |
713 `-----------------------------------------------------------------------*/
714
715 semantic_type *
716 semantic_type_from_uniqstr (const uniqstr key)
717 {
718 semantic_type probe;
719 semantic_type *entry;
720
721 probe.tag = key;
722 entry = hash_lookup (semantic_type_table, &probe);
723
724 if (!entry)
725 {
726 /* First insertion in the hash. */
727 entry = semantic_type_new (key);
728 if (!hash_insert (semantic_type_table, entry))
729 xalloc_die ();
730 }
731 return entry;
732 }
733
734
735 /*----------------------------------------------------------------.
736 | Find the symbol named KEY, and return it. If it does not exist |
737 | yet, create it. |
738 `----------------------------------------------------------------*/
739
740 symbol *
741 symbol_get (const char *key, location loc)
742 {
743 return symbol_from_uniqstr (uniqstr_new (key), loc);
744 }
745
746
747 /*-----------------------------------------------------------------------.
748 | Find the semantic type named KEY, and return it. If it does not exist |
749 | yet, create it. |
750 `-----------------------------------------------------------------------*/
751
752 semantic_type *
753 semantic_type_get (const char *key)
754 {
755 return semantic_type_from_uniqstr (uniqstr_new (key));
756 }
757
758
759 /*------------------------------------------------------------------.
760 | Generate a dummy nonterminal, whose name cannot conflict with the |
761 | user's names. |
762 `------------------------------------------------------------------*/
763
764 symbol *
765 dummy_symbol_get (location loc)
766 {
767 /* Incremented for each generated symbol. */
768 static int dummy_count = 0;
769 static char buf[256];
770
771 symbol *sym;
772
773 sprintf (buf, "$@%d", ++dummy_count);
774 sym = symbol_get (buf, loc);
775 sym->class = nterm_sym;
776 sym->number = nvars++;
777 return sym;
778 }
779
780 bool
781 symbol_is_dummy (const symbol *sym)
782 {
783 return sym->tag[0] == '@' || (sym->tag[0] == '$' && sym->tag[1] == '@');
784 }
785
786 /*-------------------.
787 | Free the symbols. |
788 `-------------------*/
789
790 void
791 symbols_free (void)
792 {
793 hash_free (symbol_table);
794 hash_free (semantic_type_table);
795 free (symbols);
796 }
797
798
799 /*---------------------------------------------------------------.
800 | Look for undefined symbols, report an error, and consider them |
801 | terminals. |
802 `---------------------------------------------------------------*/
803
804 static void
805 symbols_do (Hash_processor processor, void *processor_data)
806 {
807 hash_do_for_each (symbol_table, processor, processor_data);
808 }
809
810
811 /*--------------------------------------------------------------.
812 | Check that all the symbols are defined. Report any undefined |
813 | symbols and consider them nonterminals. |
814 `--------------------------------------------------------------*/
815
816 void
817 symbols_check_defined (void)
818 {
819 symbols_do (symbol_check_defined_processor, NULL);
820 }
821
822 /*------------------------------------------------------------------.
823 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
824 | number. |
825 `------------------------------------------------------------------*/
826
827 static void
828 symbols_token_translations_init (void)
829 {
830 bool num_256_available_p = true;
831 int i;
832
833 /* Find the highest user token number, and whether 256, the POSIX
834 preferred user token number for the error token, is used. */
835 max_user_token_number = 0;
836 for (i = 0; i < ntokens; ++i)
837 {
838 symbol *this = symbols[i];
839 if (this->user_token_number != USER_NUMBER_UNDEFINED)
840 {
841 if (this->user_token_number > max_user_token_number)
842 max_user_token_number = this->user_token_number;
843 if (this->user_token_number == 256)
844 num_256_available_p = false;
845 }
846 }
847
848 /* If 256 is not used, assign it to error, to follow POSIX. */
849 if (num_256_available_p
850 && errtoken->user_token_number == USER_NUMBER_UNDEFINED)
851 errtoken->user_token_number = 256;
852
853 /* Set the missing user numbers. */
854 if (max_user_token_number < 256)
855 max_user_token_number = 256;
856
857 for (i = 0; i < ntokens; ++i)
858 {
859 symbol *this = symbols[i];
860 if (this->user_token_number == USER_NUMBER_UNDEFINED)
861 this->user_token_number = ++max_user_token_number;
862 if (this->user_token_number > max_user_token_number)
863 max_user_token_number = this->user_token_number;
864 }
865
866 token_translations = xnmalloc (max_user_token_number + 1,
867 sizeof *token_translations);
868
869 /* Initialize all entries for literal tokens to 2, the internal
870 token number for $undefined, which represents all invalid inputs.
871 */
872 for (i = 0; i < max_user_token_number + 1; i++)
873 token_translations[i] = undeftoken->number;
874 symbols_do (symbol_translation_processor, NULL);
875 }
876
877
878 /*----------------------------------------------------------------.
879 | Assign symbol numbers, and write definition of token names into |
880 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
881 `----------------------------------------------------------------*/
882
883 void
884 symbols_pack (void)
885 {
886 symbols_do (symbol_check_alias_consistency_processor, NULL);
887
888 symbols = xcalloc (nsyms, sizeof *symbols);
889 symbols_do (symbol_pack_processor, NULL);
890
891 /* Aliases leave empty slots in symbols, so remove them. */
892 {
893 int writei;
894 int readi;
895 int nsyms_old = nsyms;
896 for (writei = 0, readi = 0; readi < nsyms_old; readi += 1)
897 {
898 if (symbols[readi] == NULL)
899 {
900 nsyms -= 1;
901 ntokens -= 1;
902 }
903 else
904 {
905 symbols[writei] = symbols[readi];
906 symbols[writei]->number = writei;
907 if (symbols[writei]->alias)
908 symbols[writei]->alias->number = writei;
909 writei += 1;
910 }
911 }
912 }
913 symbols = xnrealloc (symbols, nsyms, sizeof *symbols);
914
915 symbols_token_translations_init ();
916
917 if (startsymbol->class == unknown_sym)
918 fatal_at (startsymbol_location,
919 _("the start symbol %s is undefined"),
920 startsymbol->tag);
921 else if (startsymbol->class == token_sym)
922 fatal_at (startsymbol_location,
923 _("the start symbol %s is a token"),
924 startsymbol->tag);
925 }
926
927
928 /*--------------------------------------------------.
929 | Set default tagged/tagless %destructor/%printer. |
930 `--------------------------------------------------*/
931
932 void
933 default_tagged_destructor_set (code_props const *destructor)
934 {
935 if (default_tagged_destructor.code)
936 {
937 complain_at (destructor->location,
938 _("redeclaration for default tagged %%destructor"));
939 complain_at (default_tagged_destructor.location,
940 _("previous declaration"));
941 }
942 default_tagged_destructor = *destructor;
943 }
944
945 void
946 default_tagless_destructor_set (code_props const *destructor)
947 {
948 if (default_tagless_destructor.code)
949 {
950 complain_at (destructor->location,
951 _("redeclaration for default tagless %%destructor"));
952 complain_at (default_tagless_destructor.location,
953 _("previous declaration"));
954 }
955 default_tagless_destructor = *destructor;
956 }
957
958 void
959 default_tagged_printer_set (code_props const *printer)
960 {
961 if (default_tagged_printer.code)
962 {
963 complain_at (printer->location,
964 _("redeclaration for default tagged %%printer"));
965 complain_at (default_tagged_printer.location,
966 _("previous declaration"));
967 }
968 default_tagged_printer = *printer;
969 }
970
971 void
972 default_tagless_printer_set (code_props const *printer)
973 {
974 if (default_tagless_printer.code)
975 {
976 complain_at (printer->location,
977 _("redeclaration for default tagless %%printer"));
978 complain_at (default_tagless_printer.location,
979 _("previous declaration"));
980 }
981 default_tagless_printer = *printer;
982 }