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