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
;
37 /*------------------------.
38 | Distinguished symbols. |
39 `------------------------*/
41 symbol
*errtoken
= NULL
;
42 symbol
*undeftoken
= NULL
;
43 symbol
*endtoken
= NULL
;
44 symbol
*accept
= NULL
;
45 symbol
*startsymbol
= NULL
;
46 location startsymbol_location
;
48 /*---------------------------------------.
49 | Default %destructor's and %printer's. |
50 `---------------------------------------*/
52 static code_props default_tagged_code_props
[CODE_PROPS_SIZE
] =
57 static code_props default_tagless_code_props
[CODE_PROPS_SIZE
] =
63 /*---------------------------------.
64 | Create a new symbol, named TAG. |
65 `---------------------------------*/
68 symbol_new (uniqstr tag
, location loc
)
70 symbol
*res
= xmalloc (sizeof *res
);
74 /* If the tag is not a string (starts with a double quote), check
75 that it is valid for Yacc. */
76 if (tag
[0] != '\"' && tag
[0] != '\'' && strchr (tag
, '-'))
77 yacc_at (loc
, _("POSIX Yacc forbids dashes in symbol names: %s"),
83 res
->type_name
= NULL
;
84 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
85 code_props_none_init (&res
->props
[i
]);
87 res
->number
= NUMBER_UNDEFINED
;
89 res
->assoc
= undef_assoc
;
90 res
->user_token_number
= USER_NUMBER_UNDEFINED
;
93 res
->class = unknown_sym
;
94 res
->status
= undeclared
;
96 if (nsyms
== SYMBOL_NUMBER_MAXIMUM
)
97 fatal (_("too many symbols in input grammar (limit is %d)"),
98 SYMBOL_NUMBER_MAXIMUM
);
104 code_props_type_string (code_props_type kind
)
109 return "%destructor";
116 /*----------------------------------------.
117 | Create a new semantic type, named TAG. |
118 `----------------------------------------*/
120 static semantic_type
*
121 semantic_type_new (uniqstr tag
)
123 semantic_type
*res
= xmalloc (sizeof *res
);
125 uniqstr_assert (tag
);
127 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
128 code_props_none_init (&res
->props
[i
]);
138 #define SYMBOL_ATTR_PRINT(Attr) \
140 fprintf (f, " %s { %s }", #Attr, s->Attr)
142 #define SYMBOL_CODE_PRINT(Attr) \
143 if (s->props[Attr].code) \
144 fprintf (f, " %s { %s }", #Attr, s->props[Attr].code)
147 symbol_print (symbol
*s
, FILE *f
)
151 fprintf (f
, "\"%s\"", s
->tag
);
152 SYMBOL_ATTR_PRINT (type_name
);
153 SYMBOL_CODE_PRINT (destructor
);
154 SYMBOL_CODE_PRINT (printer
);
157 fprintf (f
, "<NULL>");
160 #undef SYMBOL_ATTR_PRINT
161 #undef SYMBOL_CODE_PRINT
164 /*----------------------------------.
165 | Whether S is a valid identifier. |
166 `----------------------------------*/
169 is_identifier (uniqstr s
)
171 static char const alphanum
[26 + 26 + 1 + 10] =
172 "abcdefghijklmnopqrstuvwxyz"
173 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
176 if (!s
|| ! memchr (alphanum
, *s
, sizeof alphanum
- 10))
179 if (! memchr (alphanum
, *s
, sizeof alphanum
))
185 /*-----------------------------------------------.
186 | Get the identifier associated to this symbol. |
187 `-----------------------------------------------*/
189 symbol_id_get (symbol
const *sym
)
191 aver (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
);
194 return is_identifier (sym
->tag
) ? sym
->tag
: 0;
198 /*------------------------------------------------------------------.
199 | Complain that S's WHAT is redeclared at SECOND, and was first set |
201 `------------------------------------------------------------------*/
204 symbol_redeclaration (symbol
*s
, const char *what
, location first
,
207 complain_at (second
, _("%s redeclaration for %s"), what
, s
->tag
);
208 complain_at (first
, _("previous declaration"));
212 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
215 complain_at (second
, _("%s redeclaration for <%s>"), what
, s
->tag
);
216 complain_at (first
, _("previous declaration"));
221 /*-----------------------------------------------------------------.
222 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
224 `-----------------------------------------------------------------*/
227 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
232 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
233 uniqstr_assert (type_name
);
234 sym
->type_name
= type_name
;
235 sym
->type_location
= loc
;
239 /*--------------------------------------------------------.
240 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
241 `--------------------------------------------------------*/
244 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
245 code_props
const *code
)
247 if (sym
->props
[kind
].code
)
248 symbol_redeclaration (sym
, code_props_type_string (kind
),
249 sym
->props
[kind
].location
,
251 sym
->props
[kind
] = *code
;
254 /*-----------------------------------------------------.
255 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
256 `-----------------------------------------------------*/
259 semantic_type_code_props_set (semantic_type
*type
,
260 code_props_type kind
,
261 code_props
const *code
)
263 if (type
->props
[kind
].code
)
264 semantic_type_redeclaration (type
, code_props_type_string (kind
),
265 type
->props
[kind
].location
,
267 type
->props
[kind
] = *code
;
270 /*---------------------------------------------------.
271 | Get the computed %destructor or %printer for SYM. |
272 `---------------------------------------------------*/
275 symbol_code_props_get (symbol
const *sym
,
276 code_props_type kind
)
278 /* Per-symbol code props. */
279 if (sym
->props
[kind
].code
)
280 return &sym
->props
[kind
];
282 /* Per-type code props. */
285 code_props
const *code
=
286 &semantic_type_get (sym
->type_name
)->props
[kind
];
291 /* Apply default code props's only to user-defined symbols. */
292 if (sym
->tag
[0] == '$' || sym
== errtoken
)
293 return &code_props_none
;
296 return &default_tagged_code_props
[kind
];
297 return &default_tagless_code_props
[kind
];
300 /*-----------------------------------------------------------------.
301 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
302 | with UNDEF_ASSOC as ASSOC. |
303 `-----------------------------------------------------------------*/
306 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
308 if (a
!= undef_assoc
)
311 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
315 sym
->prec_location
= loc
;
318 /* Only terminals have a precedence. */
319 symbol_class_set (sym
, token_sym
, loc
, false);
323 /*------------------------------------.
324 | Set the CLASS associated with SYM. |
325 `------------------------------------*/
328 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
331 if (sym
->class != unknown_sym
&& sym
->class != class)
333 complain_at (loc
, _("symbol %s redefined"), sym
->tag
);
334 // Don't report both "redefined" and "redeclared".
338 if (class == nterm_sym
&& sym
->class != nterm_sym
)
339 sym
->number
= nvars
++;
340 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
341 sym
->number
= ntokens
++;
347 if (sym
->status
== declared
&& !warned
)
348 warn_at (loc
, _("symbol %s redeclared"), sym
->tag
);
349 sym
->status
= declared
;
354 /*------------------------------------------------.
355 | Set the USER_TOKEN_NUMBER associated with SYM. |
356 `------------------------------------------------*/
359 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
361 int *user_token_numberp
;
363 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
364 user_token_numberp
= &sym
->user_token_number
;
366 user_token_numberp
= &sym
->alias
->user_token_number
;
367 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
368 && *user_token_numberp
!= user_token_number
)
369 complain_at (loc
, _("redefining user token number of %s"), sym
->tag
);
371 *user_token_numberp
= user_token_number
;
372 /* User defined $end token? */
373 if (user_token_number
== 0)
376 /* It is always mapped to 0, so it was already counted in
378 if (endtoken
->number
!= NUMBER_UNDEFINED
)
380 endtoken
->number
= 0;
385 /*----------------------------------------------------------.
386 | If SYM is not defined, report an error, and consider it a |
388 `----------------------------------------------------------*/
391 symbol_check_defined (symbol
*sym
)
393 if (sym
->class == unknown_sym
)
398 warn_at (sym
->location
,
399 _("symbol %s is used, but is not defined as a token"
400 " and has no rules"),
405 complain_at (sym
->location
,
406 _("symbol %s is used, but is not defined as a token"
407 " and has no rules"),
411 /* If declared, then sym->class != unknown_sym. */
415 sym
->class = nterm_sym
;
416 sym
->number
= nvars
++;
423 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
425 return symbol_check_defined (sym
);
430 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
433 warn_at (loc
, _("symbol %s used more than once as a literal string"),
436 warn_at (loc
, _("symbol %s given more than one literal string"),
440 str
->class = token_sym
;
441 str
->user_token_number
= sym
->user_token_number
;
442 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
445 str
->number
= sym
->number
;
446 symbol_type_set (str
, sym
->type_name
, loc
);
451 /*---------------------------------------------------------.
452 | Check that THIS, and its alias, have same precedence and |
454 `---------------------------------------------------------*/
457 symbol_check_alias_consistency (symbol
*this)
460 symbol
*str
= this->alias
;
462 /* Check only the symbol in the symbol-string pair. */
464 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
467 if (str
->type_name
!= sym
->type_name
)
470 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
472 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
476 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
477 if (str
->props
[i
].code
)
478 symbol_code_props_set (sym
, i
, &str
->props
[i
]);
479 else if (sym
->props
[i
].code
)
480 symbol_code_props_set (str
, i
, &sym
->props
[i
]);
482 if (sym
->prec
|| str
->prec
)
485 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
488 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
494 symbol_check_alias_consistency_processor (void *this,
495 void *null ATTRIBUTE_UNUSED
)
497 symbol_check_alias_consistency (this);
502 /*-------------------------------------------------------------------.
503 | Assign a symbol number, and write the definition of the token name |
504 | into FDEFINES. Put in SYMBOLS. |
505 `-------------------------------------------------------------------*/
508 symbol_pack (symbol
*this)
510 aver (this->number
!= NUMBER_UNDEFINED
);
511 if (this->class == nterm_sym
)
512 this->number
+= ntokens
;
513 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
516 symbols
[this->number
] = this;
521 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
523 return symbol_pack (this);
528 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
530 /* User token numbers are not assigned during the parsing, but in a
531 second step, via a traversal of the symbol table sorted on tag.
533 However, error messages make more sense if we keep the first
534 declaration first. */
535 if (location_cmp (first
->location
, second
->location
) > 0)
541 complain_at (second
->location
,
542 _("user token number %d redeclaration for %s"),
544 complain_at (first
->location
, _("previous declaration for %s"),
548 /*--------------------------------------------------.
549 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
550 `--------------------------------------------------*/
553 symbol_translation (symbol
*this)
556 if (this->class == token_sym
557 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
559 /* A token which translation has already been set? */
560 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
561 user_token_number_redeclaration
562 (this->user_token_number
,
563 symbols
[token_translations
[this->user_token_number
]],
566 token_translations
[this->user_token_number
] = this->number
;
573 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
575 return symbol_translation (this);
579 /*---------------------------------------.
580 | Symbol and semantic type hash tables. |
581 `---------------------------------------*/
583 /* Initial capacity of symbol and semantic type hash table. */
584 #define HT_INITIAL_CAPACITY 257
586 static struct hash_table
*symbol_table
= NULL
;
587 static struct hash_table
*semantic_type_table
= NULL
;
590 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
592 /* Since tags are unique, we can compare the pointers themselves. */
593 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
597 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
599 /* Since names are unique, we can compare the pointers themselves. */
600 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
604 hash_symbol_comparator (void const *m1
, void const *m2
)
606 return hash_compare_symbol (m1
, m2
);
610 hash_semantic_type_comparator (void const *m1
, void const *m2
)
612 return hash_compare_semantic_type (m1
, m2
);
616 hash_symbol (const symbol
*m
, size_t tablesize
)
618 /* Since tags are unique, we can hash the pointer itself. */
619 return ((uintptr_t) m
->tag
) % tablesize
;
623 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
625 /* Since names are unique, we can hash the pointer itself. */
626 return ((uintptr_t) m
->tag
) % tablesize
;
630 hash_symbol_hasher (void const *m
, size_t tablesize
)
632 return hash_symbol (m
, tablesize
);
636 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
638 return hash_semantic_type (m
, tablesize
);
641 /*-------------------------------.
642 | Create the symbol hash table. |
643 `-------------------------------*/
648 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
651 hash_symbol_comparator
,
653 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
655 hash_semantic_type_hasher
,
656 hash_semantic_type_comparator
,
661 /*----------------------------------------------------------------.
662 | Find the symbol named KEY, and return it. If it does not exist |
664 `----------------------------------------------------------------*/
667 symbol_from_uniqstr (const uniqstr key
, location loc
)
673 entry
= hash_lookup (symbol_table
, &probe
);
677 /* First insertion in the hash. */
678 aver (!symbols_sorted
);
679 entry
= symbol_new (key
, loc
);
680 if (!hash_insert (symbol_table
, entry
))
687 /*-----------------------------------------------------------------------.
688 | Find the semantic type named KEY, and return it. If it does not exist |
690 `-----------------------------------------------------------------------*/
693 semantic_type_from_uniqstr (const uniqstr key
)
696 semantic_type
*entry
;
699 entry
= hash_lookup (semantic_type_table
, &probe
);
703 /* First insertion in the hash. */
704 entry
= semantic_type_new (key
);
705 if (!hash_insert (semantic_type_table
, entry
))
712 /*----------------------------------------------------------------.
713 | Find the symbol named KEY, and return it. If it does not exist |
715 `----------------------------------------------------------------*/
718 symbol_get (const char *key
, location loc
)
720 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
724 /*-----------------------------------------------------------------------.
725 | Find the semantic type named KEY, and return it. If it does not exist |
727 `-----------------------------------------------------------------------*/
730 semantic_type_get (const char *key
)
732 return semantic_type_from_uniqstr (uniqstr_new (key
));
736 /*------------------------------------------------------------------.
737 | Generate a dummy nonterminal, whose name cannot conflict with the |
739 `------------------------------------------------------------------*/
742 dummy_symbol_get (location loc
)
744 /* Incremented for each generated symbol. */
745 static int dummy_count
= 0;
746 static char buf
[256];
750 sprintf (buf
, "$@%d", ++dummy_count
);
751 sym
= symbol_get (buf
, loc
);
752 sym
->class = nterm_sym
;
753 sym
->number
= nvars
++;
758 symbol_is_dummy (const symbol
*sym
)
760 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
763 /*-------------------.
764 | Free the symbols. |
765 `-------------------*/
770 hash_free (symbol_table
);
771 hash_free (semantic_type_table
);
773 free (symbols_sorted
);
777 /*---------------------------------------------------------------.
778 | Look for undefined symbols, report an error, and consider them |
780 `---------------------------------------------------------------*/
783 symbols_cmp (symbol
const *a
, symbol
const *b
)
785 return strcmp (a
->tag
, b
->tag
);
789 symbols_cmp_qsort (void const *a
, void const *b
)
791 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
795 symbols_do (Hash_processor processor
, void *processor_data
,
796 struct hash_table
*table
, symbol
**sorted
)
798 size_t count
= hash_get_n_entries (table
);
801 sorted
= xnmalloc (count
, sizeof *sorted
);
802 hash_get_entries (table
, (void**)sorted
, count
);
803 qsort (sorted
, count
, sizeof *sorted
, symbols_cmp_qsort
);
807 for (i
= 0; i
< count
; ++i
)
808 processor (sorted
[i
], processor_data
);
812 /*--------------------------------------------------------------.
813 | Check that all the symbols are defined. Report any undefined |
814 | symbols and consider them nonterminals. |
815 `--------------------------------------------------------------*/
818 symbols_check_defined (void)
820 symbols_do (symbol_check_defined_processor
, NULL
,
821 symbol_table
, symbols_sorted
);
824 /*------------------------------------------------------------------.
825 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
827 `------------------------------------------------------------------*/
830 symbols_token_translations_init (void)
832 bool num_256_available_p
= true;
835 /* Find the highest user token number, and whether 256, the POSIX
836 preferred user token number for the error token, is used. */
837 max_user_token_number
= 0;
838 for (i
= 0; i
< ntokens
; ++i
)
840 symbol
*this = symbols
[i
];
841 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
843 if (this->user_token_number
> max_user_token_number
)
844 max_user_token_number
= this->user_token_number
;
845 if (this->user_token_number
== 256)
846 num_256_available_p
= false;
850 /* If 256 is not used, assign it to error, to follow POSIX. */
851 if (num_256_available_p
852 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
853 errtoken
->user_token_number
= 256;
855 /* Set the missing user numbers. */
856 if (max_user_token_number
< 256)
857 max_user_token_number
= 256;
859 for (i
= 0; i
< ntokens
; ++i
)
861 symbol
*this = symbols
[i
];
862 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
863 this->user_token_number
= ++max_user_token_number
;
864 if (this->user_token_number
> max_user_token_number
)
865 max_user_token_number
= this->user_token_number
;
868 token_translations
= xnmalloc (max_user_token_number
+ 1,
869 sizeof *token_translations
);
871 /* Initialize all entries for literal tokens to the internal token
872 number for $undefined, which represents all invalid inputs. */
873 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
874 token_translations
[i
] = undeftoken
->number
;
875 symbols_do (symbol_translation_processor
, NULL
,
876 symbol_table
, symbols_sorted
);
880 /*----------------------------------------------------------------.
881 | Assign symbol numbers, and write definition of token names into |
882 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
883 `----------------------------------------------------------------*/
888 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
889 symbol_table
, symbols_sorted
);
891 symbols
= xcalloc (nsyms
, sizeof *symbols
);
892 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, symbols_sorted
);
894 /* Aliases leave empty slots in symbols, so remove them. */
898 int nsyms_old
= nsyms
;
899 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
901 if (symbols
[readi
] == NULL
)
908 symbols
[writei
] = symbols
[readi
];
909 symbols
[writei
]->number
= writei
;
910 if (symbols
[writei
]->alias
)
911 symbols
[writei
]->alias
->number
= writei
;
916 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
918 symbols_token_translations_init ();
920 if (startsymbol
->class == unknown_sym
)
921 fatal_at (startsymbol_location
,
922 _("the start symbol %s is undefined"),
924 else if (startsymbol
->class == token_sym
)
925 fatal_at (startsymbol_location
,
926 _("the start symbol %s is a token"),
931 /*--------------------------------------------------.
932 | Set default tagged/tagless %destructor/%printer. |
933 `--------------------------------------------------*/
936 default_tagged_code_props_set (code_props_type kind
, code_props
const *code
)
938 if (default_tagged_code_props
[kind
].code
)
940 complain_at (code
->location
,
941 _("redeclaration for default tagged %s"),
942 code_props_type_string (kind
));
943 complain_at (default_tagged_code_props
[kind
].location
,
944 _("previous declaration"));
946 default_tagged_code_props
[kind
] = *code
;
950 default_tagless_code_props_set (code_props_type kind
, code_props
const *code
)
952 if (default_tagless_code_props
[kind
].code
)
954 complain_at (code
->location
,
955 _("redeclaration for default tagless %s"),
956 code_props_type_string (kind
));
957 complain_at (default_tagless_code_props
[kind
].location
,
958 _("previous declaration"));
960 default_tagless_code_props
[kind
] = *code
;