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
,
197 complain_at_indent (second
, complaint
, &i
,
198 _("%s redeclaration for %s"), what
, s
->tag
);
200 complain_at_indent (first
, complaint
, &i
,
201 _("previous declaration"));
205 semantic_type_redeclaration (semantic_type
*s
, const char *what
, location first
,
209 complain_at_indent (second
, complaint
, &i
,
210 _("%s redeclaration for <%s>"), what
, s
->tag
);
212 complain_at_indent (first
, complaint
, &i
,
213 _("previous declaration"));
218 /*-----------------------------------------------------------------.
219 | Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
221 `-----------------------------------------------------------------*/
224 symbol_type_set (symbol
*sym
, uniqstr type_name
, location loc
)
229 symbol_redeclaration (sym
, "%type", sym
->type_location
, loc
);
230 uniqstr_assert (type_name
);
231 sym
->type_name
= type_name
;
232 sym
->type_location
= loc
;
236 /*--------------------------------------------------------.
237 | Set the DESTRUCTOR or PRINTER associated with the SYM. |
238 `--------------------------------------------------------*/
241 symbol_code_props_set (symbol
*sym
, code_props_type kind
,
242 code_props
const *code
)
244 if (sym
->props
[kind
].code
)
245 symbol_redeclaration (sym
, code_props_type_string (kind
),
246 sym
->props
[kind
].location
,
248 sym
->props
[kind
] = *code
;
251 /*-----------------------------------------------------.
252 | Set the DESTRUCTOR or PRINTER associated with TYPE. |
253 `-----------------------------------------------------*/
256 semantic_type_code_props_set (semantic_type
*type
,
257 code_props_type kind
,
258 code_props
const *code
)
260 if (type
->props
[kind
].code
)
261 semantic_type_redeclaration (type
, code_props_type_string (kind
),
262 type
->props
[kind
].location
,
264 type
->props
[kind
] = *code
;
267 /*---------------------------------------------------.
268 | Get the computed %destructor or %printer for SYM. |
269 `---------------------------------------------------*/
272 symbol_code_props_get (symbol
*sym
, code_props_type kind
)
274 /* Per-symbol code props. */
275 if (sym
->props
[kind
].code
)
276 return &sym
->props
[kind
];
278 /* Per-type code props. */
282 &semantic_type_get (sym
->type_name
, NULL
)->props
[kind
];
287 /* Apply default code props's only to user-defined symbols. */
288 if (sym
->tag
[0] != '$' && sym
!= errtoken
)
291 &semantic_type_get (sym
->type_name
? "*" : "", NULL
)->props
[kind
];
295 return &code_props_none
;
298 /*-----------------------------------------------------------------.
299 | Set the PRECEDENCE associated with SYM. Does nothing if invoked |
300 | with UNDEF_ASSOC as ASSOC. |
301 `-----------------------------------------------------------------*/
304 symbol_precedence_set (symbol
*sym
, int prec
, assoc a
, location loc
)
306 if (a
!= undef_assoc
)
309 symbol_redeclaration (sym
, assoc_to_string (a
), sym
->prec_location
,
313 sym
->prec_location
= loc
;
316 /* Only terminals have a precedence. */
317 symbol_class_set (sym
, token_sym
, loc
, false);
321 /*------------------------------------.
322 | Set the CLASS associated with SYM. |
323 `------------------------------------*/
326 symbol_class_set (symbol
*sym
, symbol_class
class, location loc
, bool declaring
)
329 if (sym
->class != unknown_sym
&& sym
->class != class)
331 complain_at (loc
, complaint
, _("symbol %s redefined"), sym
->tag
);
332 // Don't report both "redefined" and "redeclared".
336 if (class == nterm_sym
&& sym
->class != nterm_sym
)
337 sym
->number
= nvars
++;
338 else if (class == token_sym
&& sym
->number
== NUMBER_UNDEFINED
)
339 sym
->number
= ntokens
++;
345 if (sym
->status
== declared
&& !warned
)
346 complain_at (loc
, Wother
, _("symbol %s redeclared"), sym
->tag
);
347 sym
->status
= declared
;
352 /*------------------------------------------------.
353 | Set the USER_TOKEN_NUMBER associated with SYM. |
354 `------------------------------------------------*/
357 symbol_user_token_number_set (symbol
*sym
, int user_token_number
, location loc
)
359 int *user_token_numberp
;
361 if (sym
->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
362 user_token_numberp
= &sym
->user_token_number
;
364 user_token_numberp
= &sym
->alias
->user_token_number
;
365 if (*user_token_numberp
!= USER_NUMBER_UNDEFINED
366 && *user_token_numberp
!= user_token_number
)
367 complain_at (loc
, complaint
, _("redefining user token number of %s"),
370 *user_token_numberp
= user_token_number
;
371 /* User defined $end token? */
372 if (user_token_number
== 0)
375 /* It is always mapped to 0, so it was already counted in
377 if (endtoken
->number
!= NUMBER_UNDEFINED
)
379 endtoken
->number
= 0;
384 /*----------------------------------------------------------.
385 | If SYM is not defined, report an error, and consider it a |
387 `----------------------------------------------------------*/
390 symbol_check_defined (symbol
*sym
)
392 if (sym
->class == unknown_sym
)
394 assert (sym
->status
!= declared
);
395 complain_at (sym
->location
,
396 sym
->status
== needed
? complaint
: Wother
,
397 _("symbol %s is used, but is not defined as a token"
398 " and has no rules"),
400 sym
->class = nterm_sym
;
401 sym
->number
= nvars
++;
404 for (int i
= 0; i
< 2; ++i
)
405 symbol_code_props_get (sym
, i
)->is_used
= true;
407 /* Set the semantic type status associated to the current symbol to
408 'declared' so that we could check semantic types unnecessary uses. */
411 semantic_type
*sem_type
= semantic_type_get (sym
->type_name
, NULL
);
413 sem_type
->status
= declared
;
420 semantic_type_check_defined (semantic_type
*sem_type
)
422 // <*> and <> do not have to be "declared".
423 if (sem_type
->status
== declared
425 || STREQ(sem_type
->tag
, "*"))
427 for (int i
= 0; i
< 2; ++i
)
428 if (sem_type
->props
[i
].kind
!= CODE_PROPS_NONE
429 && ! sem_type
->props
[i
].is_used
)
430 complain_at (sem_type
->location
, Wother
,
431 _("useless %s for type <%s>"),
432 code_props_type_string (i
), sem_type
->tag
);
435 complain_at (sem_type
->location
, Wother
,
436 _("type <%s> is used, but is not associated to any symbol"),
443 symbol_check_defined_processor (void *sym
, void *null ATTRIBUTE_UNUSED
)
445 return symbol_check_defined (sym
);
449 semantic_type_check_defined_processor (void *sem_type
,
450 void *null ATTRIBUTE_UNUSED
)
452 return semantic_type_check_defined (sem_type
);
457 symbol_make_alias (symbol
*sym
, symbol
*str
, location loc
)
460 complain_at (loc
, Wother
,
461 _("symbol %s used more than once as a literal string"), str
->tag
);
463 complain_at (loc
, Wother
,
464 _("symbol %s given more than one literal string"), sym
->tag
);
467 str
->class = token_sym
;
468 str
->user_token_number
= sym
->user_token_number
;
469 sym
->user_token_number
= USER_NUMBER_HAS_STRING_ALIAS
;
472 str
->number
= sym
->number
;
473 symbol_type_set (str
, sym
->type_name
, loc
);
478 /*---------------------------------------------------------.
479 | Check that THIS, and its alias, have same precedence and |
481 `---------------------------------------------------------*/
484 symbol_check_alias_consistency (symbol
*this)
487 symbol
*str
= this->alias
;
489 /* Check only the symbol in the symbol-string pair. */
491 && this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
))
494 if (str
->type_name
!= sym
->type_name
)
497 symbol_type_set (sym
, str
->type_name
, str
->type_location
);
499 symbol_type_set (str
, sym
->type_name
, sym
->type_location
);
503 for (int i
= 0; i
< CODE_PROPS_SIZE
; ++i
)
504 if (str
->props
[i
].code
)
505 symbol_code_props_set (sym
, i
, &str
->props
[i
]);
506 else if (sym
->props
[i
].code
)
507 symbol_code_props_set (str
, i
, &sym
->props
[i
]);
509 if (sym
->prec
|| str
->prec
)
512 symbol_precedence_set (sym
, str
->prec
, str
->assoc
,
515 symbol_precedence_set (str
, sym
->prec
, sym
->assoc
,
521 symbol_check_alias_consistency_processor (void *this,
522 void *null ATTRIBUTE_UNUSED
)
524 symbol_check_alias_consistency (this);
529 /*-------------------------------------------------------------------.
530 | Assign a symbol number, and write the definition of the token name |
531 | into FDEFINES. Put in SYMBOLS. |
532 `-------------------------------------------------------------------*/
535 symbol_pack (symbol
*this)
537 aver (this->number
!= NUMBER_UNDEFINED
);
538 if (this->class == nterm_sym
)
539 this->number
+= ntokens
;
540 else if (this->user_token_number
== USER_NUMBER_HAS_STRING_ALIAS
)
543 symbols
[this->number
] = this;
548 symbol_pack_processor (void *this, void *null ATTRIBUTE_UNUSED
)
550 return symbol_pack (this);
555 user_token_number_redeclaration (int num
, symbol
*first
, symbol
*second
)
558 /* User token numbers are not assigned during the parsing, but in a
559 second step, via a traversal of the symbol table sorted on tag.
561 However, error messages make more sense if we keep the first
562 declaration first. */
563 if (location_cmp (first
->location
, second
->location
) > 0)
569 complain_at_indent (second
->location
, complaint
, &i
,
570 _("user token number %d redeclaration for %s"),
573 complain_at_indent (first
->location
, complaint
, &i
,
574 _("previous declaration for %s"),
578 /*--------------------------------------------------.
579 | Put THIS in TOKEN_TRANSLATIONS if it is a token. |
580 `--------------------------------------------------*/
583 symbol_translation (symbol
*this)
586 if (this->class == token_sym
587 && this->user_token_number
!= USER_NUMBER_HAS_STRING_ALIAS
)
589 /* A token which translation has already been set? */
590 if (token_translations
[this->user_token_number
] != undeftoken
->number
)
591 user_token_number_redeclaration
592 (this->user_token_number
,
593 symbols
[token_translations
[this->user_token_number
]],
596 token_translations
[this->user_token_number
] = this->number
;
603 symbol_translation_processor (void *this, void *null ATTRIBUTE_UNUSED
)
605 return symbol_translation (this);
609 /*---------------------------------------.
610 | Symbol and semantic type hash tables. |
611 `---------------------------------------*/
613 /* Initial capacity of symbol and semantic type hash table. */
614 #define HT_INITIAL_CAPACITY 257
616 static struct hash_table
*symbol_table
= NULL
;
617 static struct hash_table
*semantic_type_table
= NULL
;
620 hash_compare_symbol (const symbol
*m1
, const symbol
*m2
)
622 /* Since tags are unique, we can compare the pointers themselves. */
623 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
627 hash_compare_semantic_type (const semantic_type
*m1
, const semantic_type
*m2
)
629 /* Since names are unique, we can compare the pointers themselves. */
630 return UNIQSTR_EQ (m1
->tag
, m2
->tag
);
634 hash_symbol_comparator (void const *m1
, void const *m2
)
636 return hash_compare_symbol (m1
, m2
);
640 hash_semantic_type_comparator (void const *m1
, void const *m2
)
642 return hash_compare_semantic_type (m1
, m2
);
646 hash_symbol (const symbol
*m
, size_t tablesize
)
648 /* Since tags are unique, we can hash the pointer itself. */
649 return ((uintptr_t) m
->tag
) % tablesize
;
653 hash_semantic_type (const semantic_type
*m
, size_t tablesize
)
655 /* Since names are unique, we can hash the pointer itself. */
656 return ((uintptr_t) m
->tag
) % tablesize
;
660 hash_symbol_hasher (void const *m
, size_t tablesize
)
662 return hash_symbol (m
, tablesize
);
666 hash_semantic_type_hasher (void const *m
, size_t tablesize
)
668 return hash_semantic_type (m
, tablesize
);
671 /*-------------------------------.
672 | Create the symbol hash table. |
673 `-------------------------------*/
678 symbol_table
= hash_initialize (HT_INITIAL_CAPACITY
,
681 hash_symbol_comparator
,
683 semantic_type_table
= hash_initialize (HT_INITIAL_CAPACITY
,
685 hash_semantic_type_hasher
,
686 hash_semantic_type_comparator
,
691 /*----------------------------------------------------------------.
692 | Find the symbol named KEY, and return it. If it does not exist |
694 `----------------------------------------------------------------*/
697 symbol_from_uniqstr (const uniqstr key
, location loc
)
703 entry
= hash_lookup (symbol_table
, &probe
);
707 /* First insertion in the hash. */
708 aver (!symbols_sorted
);
709 entry
= symbol_new (key
, loc
);
710 if (!hash_insert (symbol_table
, entry
))
717 /*-----------------------------------------------------------------------.
718 | Find the semantic type named KEY, and return it. If it does not exist |
720 `-----------------------------------------------------------------------*/
723 semantic_type_from_uniqstr (const uniqstr key
, const location
*loc
)
726 semantic_type
*entry
;
729 entry
= hash_lookup (semantic_type_table
, &probe
);
733 /* First insertion in the hash. */
734 entry
= semantic_type_new (key
, loc
);
735 if (!hash_insert (semantic_type_table
, entry
))
742 /*----------------------------------------------------------------.
743 | Find the symbol named KEY, and return it. If it does not exist |
745 `----------------------------------------------------------------*/
748 symbol_get (const char *key
, location loc
)
750 return symbol_from_uniqstr (uniqstr_new (key
), loc
);
754 /*-----------------------------------------------------------------------.
755 | Find the semantic type named KEY, and return it. If it does not exist |
757 `-----------------------------------------------------------------------*/
760 semantic_type_get (const char *key
, const location
*loc
)
762 return semantic_type_from_uniqstr (uniqstr_new (key
), loc
);
766 /*------------------------------------------------------------------.
767 | Generate a dummy nonterminal, whose name cannot conflict with the |
769 `------------------------------------------------------------------*/
772 dummy_symbol_get (location loc
)
774 /* Incremented for each generated symbol. */
775 static int dummy_count
= 0;
776 static char buf
[256];
780 sprintf (buf
, "$@%d", ++dummy_count
);
781 sym
= symbol_get (buf
, loc
);
782 sym
->class = nterm_sym
;
783 sym
->number
= nvars
++;
788 symbol_is_dummy (const symbol
*sym
)
790 return sym
->tag
[0] == '@' || (sym
->tag
[0] == '$' && sym
->tag
[1] == '@');
793 /*-------------------.
794 | Free the symbols. |
795 `-------------------*/
800 hash_free (symbol_table
);
801 hash_free (semantic_type_table
);
803 free (symbols_sorted
);
807 /*---------------------------------------------------------------.
808 | Look for undefined symbols, report an error, and consider them |
810 `---------------------------------------------------------------*/
813 symbols_cmp (symbol
const *a
, symbol
const *b
)
815 return strcmp (a
->tag
, b
->tag
);
819 symbols_cmp_qsort (void const *a
, void const *b
)
821 return symbols_cmp (*(symbol
* const *)a
, *(symbol
* const *)b
);
825 symbols_do (Hash_processor processor
, void *processor_data
,
826 struct hash_table
*table
, symbol
**sorted
)
828 size_t count
= hash_get_n_entries (table
);
831 sorted
= xnmalloc (count
, sizeof *sorted
);
832 hash_get_entries (table
, (void**)sorted
, count
);
833 qsort (sorted
, count
, sizeof *sorted
, symbols_cmp_qsort
);
837 for (i
= 0; i
< count
; ++i
)
838 processor (sorted
[i
], processor_data
);
842 /*--------------------------------------------------------------.
843 | Check that all the symbols are defined. Report any undefined |
844 | symbols and consider them nonterminals. |
845 `--------------------------------------------------------------*/
848 symbols_check_defined (void)
850 symbols_do (symbol_check_defined_processor
, NULL
,
851 symbol_table
, symbols_sorted
);
852 symbols_do (semantic_type_check_defined_processor
, NULL
,
853 semantic_type_table
, semantic_types_sorted
);
856 /*------------------------------------------------------------------.
857 | Set TOKEN_TRANSLATIONS. Check that no two symbols share the same |
859 `------------------------------------------------------------------*/
862 symbols_token_translations_init (void)
864 bool num_256_available_p
= true;
867 /* Find the highest user token number, and whether 256, the POSIX
868 preferred user token number for the error token, is used. */
869 max_user_token_number
= 0;
870 for (i
= 0; i
< ntokens
; ++i
)
872 symbol
*this = symbols
[i
];
873 if (this->user_token_number
!= USER_NUMBER_UNDEFINED
)
875 if (this->user_token_number
> max_user_token_number
)
876 max_user_token_number
= this->user_token_number
;
877 if (this->user_token_number
== 256)
878 num_256_available_p
= false;
882 /* If 256 is not used, assign it to error, to follow POSIX. */
883 if (num_256_available_p
884 && errtoken
->user_token_number
== USER_NUMBER_UNDEFINED
)
885 errtoken
->user_token_number
= 256;
887 /* Set the missing user numbers. */
888 if (max_user_token_number
< 256)
889 max_user_token_number
= 256;
891 for (i
= 0; i
< ntokens
; ++i
)
893 symbol
*this = symbols
[i
];
894 if (this->user_token_number
== USER_NUMBER_UNDEFINED
)
895 this->user_token_number
= ++max_user_token_number
;
896 if (this->user_token_number
> max_user_token_number
)
897 max_user_token_number
= this->user_token_number
;
900 token_translations
= xnmalloc (max_user_token_number
+ 1,
901 sizeof *token_translations
);
903 /* Initialize all entries for literal tokens to the internal token
904 number for $undefined, which represents all invalid inputs. */
905 for (i
= 0; i
< max_user_token_number
+ 1; i
++)
906 token_translations
[i
] = undeftoken
->number
;
907 symbols_do (symbol_translation_processor
, NULL
,
908 symbol_table
, symbols_sorted
);
912 /*----------------------------------------------------------------.
913 | Assign symbol numbers, and write definition of token names into |
914 | FDEFINES. Set up vectors SYMBOL_TABLE, TAGS of symbols. |
915 `----------------------------------------------------------------*/
920 symbols_do (symbol_check_alias_consistency_processor
, NULL
,
921 symbol_table
, symbols_sorted
);
923 symbols
= xcalloc (nsyms
, sizeof *symbols
);
924 symbols_do (symbol_pack_processor
, NULL
, symbol_table
, symbols_sorted
);
926 /* Aliases leave empty slots in symbols, so remove them. */
930 int nsyms_old
= nsyms
;
931 for (writei
= 0, readi
= 0; readi
< nsyms_old
; readi
+= 1)
933 if (symbols
[readi
] == NULL
)
940 symbols
[writei
] = symbols
[readi
];
941 symbols
[writei
]->number
= writei
;
942 if (symbols
[writei
]->alias
)
943 symbols
[writei
]->alias
->number
= writei
;
948 symbols
= xnrealloc (symbols
, nsyms
, sizeof *symbols
);
950 symbols_token_translations_init ();
952 if (startsymbol
->class == unknown_sym
)
953 complain_at (startsymbol_location
, fatal
,
954 _("the start symbol %s is undefined"),
956 else if (startsymbol
->class == token_sym
)
957 complain_at (startsymbol_location
, fatal
,
958 _("the start symbol %s is a token"),