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