mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
gram: fix handling of nterms in actions when some are unused
Since Bison 3.3, semantic values in rule actions (i.e., '$...') are passed to the m4 backend as the symbol number. Unfortunately, when there are unused symbols, the symbols are renumbered _after_ the numbers were used in the rule actions. As a result, the evaluation of the skeleton failed because it used non existing symbol numbers. Which is the happy scenario: we could use numbers of other existing symbols... Reported by Balázs Scheidler. http://lists.gnu.org/archive/html/bug-bison/2019-01/msg00044.html Translating the rule actions after the symbol renumbering moves too many parts in bison. Relying on the symbol identifiers is more troublesome than it might first seem: some don't have an identifier (tokens with only a literal string), some might have a complex one (tokens with a literal string with characters special for M4). Well, these are tokens, but nterms also have issues: "dummy" nterms (for midrule actions) are named $@32 etc. which is risky for M4. Instead, let's simply give M4 the mapping between the old numbers and the new ones. To avoid confusion between old and new numbers, always emit pre-renumbering numbers as "orig NUM". * data/README: Give details about "orig NUM". * data/skeletons/bison.m4 (__b4_symbol, _b4_symbol): Resolve the "orig NUM". * src/output.c (prepare_symbol_definitions): Pass nterm_map to m4. * src/reduce.h, src/reduce.c (nterm_map): Extract it from nonterminals_reduce, to make it public. (reduce_free): Free it. * src/scan-code.l (handle_action_dollar): When referring to a nterm, use "orig NUM". * tests/reduce.at (Useless Parts): New, based Balázs Scheidler's report.
This commit is contained in:
@@ -38,6 +38,7 @@
|
||||
#include "muscle-tab.h"
|
||||
#include "output.h"
|
||||
#include "reader.h"
|
||||
#include "reduce.h"
|
||||
#include "scan-code.h" /* max_left_semantic_context */
|
||||
#include "scan-skel.h"
|
||||
#include "symtab.h"
|
||||
@@ -414,6 +415,14 @@ merger_output (FILE *out)
|
||||
static void
|
||||
prepare_symbol_definitions (void)
|
||||
{
|
||||
/* Map "orig NUM" to new numbers. See data/README. */
|
||||
for (symbol_number i = ntokens; i < nsyms + nuseless_nonterminals; ++i)
|
||||
{
|
||||
obstack_printf (&format_obstack, "symbol(orig %d, number)", i);
|
||||
const char *key = obstack_finish0 (&format_obstack);
|
||||
MUSCLE_INSERT_INT (key, nterm_map ? nterm_map[i - ntokens] : i);
|
||||
}
|
||||
|
||||
for (int i = 0; i < nsyms; ++i)
|
||||
{
|
||||
symbol *sym = symbols[i];
|
||||
|
||||
@@ -259,13 +259,14 @@ reduce_grammar_tables (void)
|
||||
| Remove useless nonterminals. |
|
||||
`------------------------------*/
|
||||
|
||||
symbol_number *nterm_map = NULL;
|
||||
|
||||
static void
|
||||
nonterminals_reduce (void)
|
||||
{
|
||||
nterm_map = xnmalloc (nvars, sizeof *nterm_map);
|
||||
/* Map the nonterminals to their new index: useful first, useless
|
||||
afterwards. Kept for later report. */
|
||||
|
||||
symbol_number *nterm_map = xnmalloc (nvars, sizeof *nterm_map);
|
||||
{
|
||||
symbol_number n = ntokens;
|
||||
for (symbol_number i = ntokens; i < nsyms; ++i)
|
||||
@@ -306,8 +307,6 @@ nonterminals_reduce (void)
|
||||
|
||||
nsyms -= nuseless_nonterminals;
|
||||
nvars -= nuseless_nonterminals;
|
||||
|
||||
free (nterm_map);
|
||||
}
|
||||
|
||||
|
||||
@@ -433,4 +432,6 @@ reduce_free (void)
|
||||
bitset_free (V);
|
||||
bitset_free (V1);
|
||||
bitset_free (P);
|
||||
free (nterm_map);
|
||||
nterm_map = NULL;
|
||||
}
|
||||
|
||||
@@ -32,6 +32,11 @@ bool reduce_nonterminal_useless_in_grammar (const sym_content *sym);
|
||||
|
||||
void reduce_free (void);
|
||||
|
||||
/** Map initial nterm numbers to the new ones. Built by
|
||||
* reduce_grammar. Size nvars. */
|
||||
extern symbol_number *nterm_map;
|
||||
|
||||
extern unsigned nuseless_nonterminals;
|
||||
extern unsigned nuseless_productions;
|
||||
|
||||
#endif /* !REDUCE_H_ */
|
||||
|
||||
@@ -648,7 +648,7 @@ handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
|
||||
untyped_var_seen = true;
|
||||
}
|
||||
|
||||
obstack_printf (&obstack_for_string, "]b4_lhs_value(%d, ",
|
||||
obstack_printf (&obstack_for_string, "]b4_lhs_value(orig %d, ",
|
||||
sym->content.sym->content->number);
|
||||
obstack_quote (&obstack_for_string, type_name);
|
||||
obstack_sgrow (&obstack_for_string, ")[");
|
||||
@@ -677,7 +677,9 @@ handle_action_dollar (symbol_list *rule, char *text, location dollar_loc)
|
||||
"]b4_rhs_value(%d, %d, ",
|
||||
effective_rule_length, n);
|
||||
if (sym)
|
||||
obstack_printf (&obstack_for_string, "%d, ", sym->content.sym->content->number);
|
||||
obstack_printf (&obstack_for_string, "%s%d, ",
|
||||
sym->content.sym->content->class == nterm_sym ? "orig " : "",
|
||||
sym->content.sym->content->number);
|
||||
else
|
||||
obstack_sgrow (&obstack_for_string, "[], ");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user