1 /* Symbol table manager for Bison.
3 Copyright (C) 1984, 1989, 2000-2002, 2004-2012 Free Software
6 This file is part of Bison, the GNU Compiler Compiler.
8 This program 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 3 of the License, or
11 (at your option) any later version.
13 This program 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.
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>. */
30 /*-------------------------------------------------------------------.
31 | Symbols sorted by tag. Allocated by the first invocation of |
32 | symbols_do, after which no more symbols should be created. |
33 `-------------------------------------------------------------------*/
35 static symbol
**symbols_sorted
= NULL
;
36 static symbol
**semantic_types_sorted
= NULL
;
38 /*------------------------.
39 | Distinguished symbols. |
40 `------------------------*/
42 symbol
*errtoken
= NULL
;
43 symbol
*undeftoken
= NULL
;
44 symbol
*endtoken
= NULL
;
45 symbol
*accept
= NULL
;
46 symbol
*startsymbol
= NULL
;
47 location startsymbol_location
;
50 /*---------------------------------.
51 | Create a new symbol, named TAG. |
52 `---------------------------------*/
55 symbol_new (uniqstr tag
, location loc
)
57 symbol
*res
= xmalloc (sizeof *res
);
61 /* If the tag is not a string (starts with a double quote), check
62 that it is valid for Yacc. */
63 if (tag
[0] != '\"' && tag
[0] != '\'' && strchr (tag
, '-'))
64 complain_at (loc
, Wyacc
,
65 _("POSIX Yacc forbids dashes in symbol names: %s"), tag
);
70 res
->type_name
= NULL
;
71 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
72 code_props_none_init (&res
->props
[i
]);
74 res
->number
= NUMBER_UNDEFINED
;
76 res
->assoc
= undef_assoc
;
77 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
80 res
->class = unknown_sym
;
81 res
->status
= undeclared
;
83 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
84 complain (fatal
, _("too many symbols in input grammar (limit is %d)"),
85 SYMBOL_NUMBER_MAXIMUM
);
91 code_props_type_string (code_props_type kind
)
103 /*----------------------------------------.
104 | Create a new semantic type, named TAG. |
105 `----------------------------------------*/
107 static semantic_type
*
108 semantic_type_new (uniqstr tag
, const location
*loc
)
110 semantic_type
*res
= xmalloc (sizeof *res
);
112 uniqstr_assert (tag
);
115 res
->location
= *loc
;
116 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
117 code_props_none_init (&res
->props
[i
]);
127 #define SYMBOL_ATTR_PRINT(Attr) \
129 fprintf (f, " %s { %s }", #Attr, s->Attr)
131 #define SYMBOL_CODE_PRINT(Attr) \
132 if (s->props[Attr].code) \
133 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
136 symbol_print (symbol
const *s
, FILE *f
)
140 fprintf (f
, "\"%s\"", s
->tag
);
141 SYMBOL_ATTR_PRINT (type_name
);
142 SYMBOL_CODE_PRINT (destructor
);
143 SYMBOL_CODE_PRINT (printer
);
146 fprintf (f
, "<NULL>");
149 #undef SYMBOL_ATTR_PRINT
150 #undef SYMBOL_CODE_PRINT
153 /*----------------------------------.
154 | Whether S is a valid identifier. |
155 `----------------------------------*/
158 is_identifier (uniqstr s
)
160 static char const alphanum
[26 + 26 + 1 + 10] =
161 "abcdefghijklmnopqrstuvwxyz"
162 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
165 if (!s
|| ! memchr (alphanum
, *s
, sizeof alphanum
- 10))
168 if (! memchr (alphanum
, *s
, sizeof alphanum
))
174 /*-----------------------------------------------.
175 | Get the identifier associated to this symbol. |
176 `-----------------------------------------------*/
178 symbol_id_get (symbol
const *sym
)
180 aver (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
);
183 return is_identifier (sym
->tag
) ? sym
->tag
: 0;
187 /*------------------------------------------------------------------.
188 | Complain that S's WHAT is redeclared at SECOND, and was first set |
190 `------------------------------------------------------------------*/
193 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
196 complain_at (second
, complaint
, _("%s redeclaration for %s"), what
, s
->tag
);
197 complain_at (first
, complaint
, _("previous declaration"));
201 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
204 complain_at (second
, complaint
, _("%s redeclaration for <%s>"), what
, s
->tag
);
205 complain_at (first
, complaint
, _("previous declaration"));
210 /*-----------------------------------------------------------------.
211 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
213 `-----------------------------------------------------------------*/
216 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
221 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
222 uniqstr_assert (type_name
);
223 sym
->type_name
= type_name
;
224 sym
->type_location
= loc
;
228 /*--------------------------------------------------------.
229 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
230 `--------------------------------------------------------*/
233 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
234 code_props
const *code
)
236 if (sym
->props
[kind
].code
)
237 symbol_redeclaration (sym
, code_props_type_string (kind
),
238 sym
->props
[kind
].location
,
240 sym
->props
[kind
] = *code
;
243 /*-----------------------------------------------------.
244 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
245 `-----------------------------------------------------*/
248 semantic_type_code_props_set (semantic_type
*type
,
249 code_props_type kind
,
250 code_props
const *code
)
252 if (type
->props
[kind
].code
)
253 semantic_type_redeclaration (type
, code_props_type_string (kind
),
254 type
->props
[kind
].location
,
256 type
->props
[kind
] = *code
;
259 /*---------------------------------------------------.
260 | Get the computed %destructor or %printer for SYM. |
261 `---------------------------------------------------*/
264 symbol_code_props_get (symbol
*sym
, code_props_type kind
)
266 /* Per-symbol code props. */
267 if (sym
->props
[kind
].code
)
268 return &sym
->props
[kind
];
270 /* Per-type code props. */
274 &semantic_type_get (sym
->type_name
, NULL
)->props
[kind
];
279 /* Apply default code props's only to user-defined symbols. */
280 if (sym
->tag
[0] != '$' && sym
!= errtoken
)
283 &semantic_type_get (sym
->type_name
? "*" : "", NULL
)->props
[kind
];
287 return &code_props_none
;
290 /*-----------------------------------------------------------------.
291 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
292 | with UNDEF_ASSOC as ASSOC. |
293 `-----------------------------------------------------------------*/
296 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
298 if (a
!= undef_assoc
)
301 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
305 sym
->prec_location
= loc
;
308 /* Only terminals have a precedence. */
309 symbol_class_set (sym
, token_sym
, loc
, false);
313 /*------------------------------------.
314 | Set the CLASS associated with SYM. |
315 `------------------------------------*/
318 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
321 if (sym
->class != unknown_sym
&& sym
->class != class)
323 complain_at (loc
, complaint
, _("symbol %s redefined"), sym
->tag
);
324 // Don't report both "redefined" and "redeclared".
328 if (class == nterm_sym
&& sym
->class != nterm_sym
)
329 sym
->number
= nvars
++;
330 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
331 sym
->number
= ntokens
++;
337 if (sym
->status
== declared
&& !warned
)
338 complain_at (loc
, Wother
, _("symbol %s redeclared"), sym
->tag
);
339 sym
->status
= declared
;
344 /*------------------------------------------------.
345 | Set the USER_TOKEN_NUMBER associated with SYM. |
346 `------------------------------------------------*/
349 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
351 int *user_token_numberp
;
353 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
354 user_token_numberp
= &sym
->user_token_number
;
356 user_token_numberp
= &sym
->alias
->user_token_number
;
357 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
358 && *user_token_numberp
!= user_token_number
)
359 complain_at (loc
, complaint
, _("redefining user token number of %s"),
362 *user_token_numberp
= user_token_number
;
363 /* User defined $end token? */
364 if (user_token_number
== 0)
367 /* It is always mapped to 0, so it was already counted in
369 if (endtoken
->number
!= NUMBER_UNDEFINED
)
371 endtoken
->number
= 0;
376 /*----------------------------------------------------------.
377 | If SYM is not defined, report an error, and consider it a |
379 `----------------------------------------------------------*/
382 symbol_check_defined (symbol
*sym
)
384 if (sym
->class == unknown_sym
)
389 complain_at (sym
->location
, Wother
,
390 _("symbol %s is used, but is not defined as a token"
391 " and has no rules"),
396 complain_at (sym
->location
, complaint
,
397 _("symbol %s is used, but is not defined as a token"
398 " and has no rules"),
402 /* If declared, then sym->class != unknown_sym. */
406 sym
->class = nterm_sym
;
407 sym
->number
= nvars
++;
410 for (int i
= 0; i
< 2; ++i
)
411 symbol_code_props_get (sym
, i
)->is_used
= true;
413 /* Set the semantic type status associated to the current symbol to
414 'declared' so that we could check semantic types unnecessary uses. */
417 semantic_type
*sem_type
= semantic_type_get (sym
->type_name
, NULL
);
419 sem_type
->status
= declared
;
426 semantic_type_check_defined (semantic_type
*sem_type
)
428 // <*> and <> do not have to be "declared".
429 if (sem_type
->status
== declared
431 || STREQ(sem_type
->tag
, "*"))
433 for (int i
= 0; i
< 2; ++i
)
434 if (sem_type
->props
[i
].kind
!= CODE_PROPS_NONE
435 && ! sem_type
->props
[i
].is_used
)
436 complain_at (sem_type
->location
, Wother
,
437 _("useless %s for type <%s>"),
438 code_props_type_string (i
), sem_type
->tag
);
441 complain_at (sem_type
->location
, Wother
,
442 _("type <%s> is used, but is not associated to any symbol"),
449 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
451 return symbol_check_defined (sym
);
455 semantic_type_check_defined_processor (void *sem_type
,
456 void *null ATTRIBUTE_UNUSED
)
458 return semantic_type_check_defined (sem_type
);
463 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
466 complain_at (loc
, Wother
,
467 _("symbol %s used more than once as a literal string"), str
->tag
);
469 complain_at (loc
, Wother
,
470 _("symbol %s given more than one literal string"), sym
->tag
);
473 str
->class = token_sym
;
474 str
->user_token_number
= sym
->user_token_number
;
475 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
478 str
->number
= sym
->number
;
479 symbol_type_set (str
, sym
->type_name
, loc
);
484 /*---------------------------------------------------------.
485 | Check that THIS, and its alias, have same precedence and |
487 `---------------------------------------------------------*/
490 symbol_check_alias_consistency (symbol
*this)
493 symbol
*str
= this->alias
;
495 /* Check only the symbol in the symbol-string pair. */
497 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
500 if (str
->type_name
!= sym
->type_name
)
503 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
505 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
509 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
510 if (str
->props
[i
].code
)
511 symbol_code_props_set (sym
, i
, &str
->props
[i
]);
512 else if (sym
->props
[i
].code
)
513 symbol_code_props_set (str
, i
, &sym
->props
[i
]);
515 if (sym
->prec
|| str
->prec
)
518 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
521 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
527 symbol_check_alias_consistency_processor (void *this,
528 void *null ATTRIBUTE_UNUSED
)
530 symbol_check_alias_consistency (this);
535 /*-------------------------------------------------------------------.
536 | Assign a symbol number, and write the definition of the token name |
537 | into FDEFINES. Put in SYMBOLS. |
538 `-------------------------------------------------------------------*/
541 symbol_pack (symbol
*this)
543 aver (this->number
!= NUMBER_UNDEFINED
);
544 if (this->class == nterm_sym
)
545 this->number
+= ntokens
;
546 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
549 symbols
[this->number
] = this;
554 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
556 return symbol_pack (this);
561 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
563 /* User token numbers are not assigned during the parsing, but in a
564 second step, via a traversal of the symbol table sorted on tag.
566 However, error messages make more sense if we keep the first
567 declaration first. */
568 if (location_cmp (first
->location
, second
->location
) > 0)
574 complain_at (second
->location
, complaint
,
575 _("user token number %d redeclaration for %s"),
577 complain_at (first
->location
, complaint
, _("previous declaration for %s"),
581 /*--------------------------------------------------.
582 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
583 `--------------------------------------------------*/
586 symbol_translation (symbol
*this)
589 if (this->class == token_sym
590 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
592 /* A token which translation has already been set? */
593 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
594 user_token_number_redeclaration
595 (this->user_token_number
,
596 symbols
[token_translations
[this->user_token_number
]],
599 token_translations
[this->user_token_number
] = this->number
;
606 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
608 return symbol_translation (this);
612 /*---------------------------------------.
613 | Symbol and semantic type hash tables. |
614 `---------------------------------------*/
616 /* Initial capacity of symbol and semantic type hash table. */
617 #define HT_INITIAL_CAPACITY 257
619 static struct hash_table
*symbol_table
= NULL
;
620 static struct hash_table
*semantic_type_table
= NULL
;
623 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
625 /* Since tags are unique, we can compare the pointers themselves. */
626 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
630 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
632 /* Since names are unique, we can compare the pointers themselves. */
633 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
637 hash_symbol_comparator (void const *m1
, void const *m2
)
639 return hash_compare_symbol (m1
, m2
);
643 hash_semantic_type_comparator (void const *m1
, void const *m2
)
645 return hash_compare_semantic_type (m1
, m2
);
649 hash_symbol (const symbol
*m
, size_t tablesize
)
651 /* Since tags are unique, we can hash the pointer itself. */
652 return ((uintptr_t) m
->tag
) % tablesize
;
656 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
658 /* Since names are unique, we can hash the pointer itself. */
659 return ((uintptr_t) m
->tag
) % tablesize
;
663 hash_symbol_hasher (void const *m
, size_t tablesize
)
665 return hash_symbol (m
, tablesize
);
669 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
671 return hash_semantic_type (m
, tablesize
);
674 /*-------------------------------.
675 | Create the symbol hash table. |
676 `-------------------------------*/
681 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
684 hash_symbol_comparator
,
686 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
688 hash_semantic_type_hasher
,
689 hash_semantic_type_comparator
,
694 /*----------------------------------------------------------------.
695 | Find the symbol named KEY, and return it. If it does not exist |
697 `----------------------------------------------------------------*/
700 symbol_from_uniqstr (const uniqstr key
, location loc
)
706 entry
= hash_lookup (symbol_table
, &probe
);
710 /* First insertion in the hash. */
711 aver (!symbols_sorted
);
712 entry
= symbol_new (key
, loc
);
713 if (!hash_insert (symbol_table
, entry
))
720 /*-----------------------------------------------------------------------.
721 | Find the semantic type named KEY, and return it. If it does not exist |
723 `-----------------------------------------------------------------------*/
726 semantic_type_from_uniqstr (const uniqstr key
, const location
*loc
)
729 semantic_type
*entry
;
732 entry
= hash_lookup (semantic_type_table
, &probe
);
736 /* First insertion in the hash. */
737 entry
= semantic_type_new (key
, loc
);
738 if (!hash_insert (semantic_type_table
, entry
))
745 /*----------------------------------------------------------------.
746 | Find the symbol named KEY, and return it. If it does not exist |
748 `----------------------------------------------------------------*/
751 symbol_get (const char *key
, location loc
)
753 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
757 /*-----------------------------------------------------------------------.
758 | Find the semantic type named KEY, and return it. If it does not exist |
760 `-----------------------------------------------------------------------*/
763 semantic_type_get (const char *key
, const location
*loc
)
765 return semantic_type_from_uniqstr (uniqstr_new (key
), loc
);
769 /*------------------------------------------------------------------.
770 | Generate a dummy nonterminal, whose name cannot conflict with the |
772 `------------------------------------------------------------------*/
775 dummy_symbol_get (location loc
)
777 /* Incremented for each generated symbol. */
778 static int dummy_count
= 0;
779 static char buf
[256];
783 sprintf (buf
, "$@%d", ++dummy_count
);
784 sym
= symbol_get (buf
, loc
);
785 sym
->class = nterm_sym
;
786 sym
->number
= nvars
++;
791 symbol_is_dummy (const symbol
*sym
)
793 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
796 /*-------------------.
797 | Free the symbols. |
798 `-------------------*/
803 hash_free (symbol_table
);
804 hash_free (semantic_type_table
);
806 free (symbols_sorted
);
810 /*---------------------------------------------------------------.
811 | Look for undefined symbols, report an error, and consider them |
813 `---------------------------------------------------------------*/
816 symbols_cmp (symbol
const *a
, symbol
const *b
)
818 return strcmp (a
->tag
, b
->tag
);
822 symbols_cmp_qsort (void const *a
, void const *b
)
824 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
828 symbols_do (Hash_processor processor
, void *processor_data
,
829 struct hash_table
*table
, symbol
**sorted
)
831 size_t count
= hash_get_n_entries (table
);
834 sorted
= xnmalloc (count
, sizeof *sorted
);
835 hash_get_entries (table
, (void**)sorted
, count
);
836 qsort (sorted
, count
, sizeof *sorted
, symbols_cmp_qsort
);
840 for (i
= 0; i
< count
; ++i
)
841 processor (sorted
[i
], processor_data
);
845 /*--------------------------------------------------------------.
846 | Check that all the symbols are defined. Report any undefined |
847 | symbols and consider them nonterminals. |
848 `--------------------------------------------------------------*/
851 symbols_check_defined (void)
853 symbols_do (symbol_check_defined_processor
, NULL
,
854 symbol_table
, symbols_sorted
);
855 symbols_do (semantic_type_check_defined_processor
, NULL
,
856 semantic_type_table
, semantic_types_sorted
);
859 /*------------------------------------------------------------------.
860 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
862 `------------------------------------------------------------------*/
865 symbols_token_translations_init (void)
867 bool num_256_available_p
= true;
870 /* Find the highest user token number, and whether 256, the POSIX
871 preferred user token number for the error token, is used. */
872 max_user_token_number
= 0;
873 for (i
= 0; i
< ntokens
; ++i
)
875 symbol
*this = symbols
[i
];
876 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
878 if (this->user_token_number
> max_user_token_number
)
879 max_user_token_number
= this->user_token_number
;
880 if (this->user_token_number
== 256)
881 num_256_available_p
= false;
885 /* If 256 is not used, assign it to error, to follow POSIX. */
886 if (num_256_available_p
887 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
888 errtoken
->user_token_number
= 256;
890 /* Set the missing user numbers. */
891 if (max_user_token_number
< 256)
892 max_user_token_number
= 256;
894 for (i
= 0; i
< ntokens
; ++i
)
896 symbol
*this = symbols
[i
];
897 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
898 this->user_token_number
= ++max_user_token_number
;
899 if (this->user_token_number
> max_user_token_number
)
900 max_user_token_number
= this->user_token_number
;
903 token_translations
= xnmalloc (max_user_token_number
+ 1,
904 sizeof *token_translations
);
906 /* Initialize all entries for literal tokens to the internal token
907 number for $undefined, which represents all invalid inputs. */
908 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
909 token_translations
[i
] = undeftoken
->number
;
910 symbols_do (symbol_translation_processor
, NULL
,
911 symbol_table
, symbols_sorted
);
915 /*----------------------------------------------------------------.
916 | Assign symbol numbers, and write definition of token names into |
917 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
918 `----------------------------------------------------------------*/
923 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
924 symbol_table
, symbols_sorted
);
926 symbols
= xcalloc (nsyms
, sizeof *symbols
);
927 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, symbols_sorted
);
929 /* Aliases leave empty slots in symbols, so remove them. */
933 int nsyms_old
= nsyms
;
934 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
936 if (symbols
[readi
] == NULL
)
943 symbols
[writei
] = symbols
[readi
];
944 symbols
[writei
]->number
= writei
;
945 if (symbols
[writei
]->alias
)
946 symbols
[writei
]->alias
->number
= writei
;
951 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
953 symbols_token_translations_init ();
955 if (startsymbol
->class == unknown_sym
)
956 complain_at (startsymbol_location
, fatal
,
957 _("the start symbol %s is undefined"),
959 else if (startsymbol
->class == token_sym
)
960 complain_at (startsymbol_location
, fatal
,
961 _("the start symbol %s is a token"),