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