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