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