grammar: record used associativity and print useless ones

Record which symbol associativity is used, and display useless ones.

* src/symtab.h, src/symtab.c (register_assoc, print_assoc_warnings): New
* src/symtab.c (init_assoc, is_assoc_used): New
* src/main.c: Use print_assoc_warnings
* src/conflicts.c: Use register_assoc
* tests/conflicts.at (Useless associativity warning): New.

Due to the new warning, many tests had to be updated.

* tests/conflicts.at tests/existing.at tests/regression.at:
Add the associativity warning in the expected results.
* tests/java.at: Fix the java calculator's grammar to remove a useless
associativity.
* doc/bison.texi (mfcalc example): Fix associativity to remove
warning.
This commit is contained in:
Valentin Tolmer
2013-01-29 16:27:04 +01:00
committed by Akim Demaille
parent 284bc49c83
commit e8f7155d98
9 changed files with 275 additions and 9 deletions

View File

@@ -303,16 +303,19 @@ resolve_sr_conflict (state *s, int ruleno, symbol **errors, int *nerrs)
break;
case right_assoc:
register_assoc (i, redrule->prec->number);
log_resolution (redrule, i, right_resolution);
flush_reduce (lookahead_tokens, i);
break;
case left_assoc:
register_assoc (i, redrule->prec->number);
log_resolution (redrule, i, left_resolution);
flush_shift (s, i);
break;
case non_assoc:
register_assoc (i, redrule->prec->number);
log_resolution (redrule, i, nonassoc_resolution);
flush_shift (s, i);
flush_reduce (lookahead_tokens, i);

View File

@@ -146,6 +146,8 @@ main (int argc, char *argv[])
print_precedence_warnings ();
print_assoc_warnings ();
/* Output file names. */
compute_output_file_names ();

View File

@@ -52,6 +52,12 @@ location startsymbol_location;
static symgraph **prec_nodes;
/*-----------------------------------.
| Store which associativity is used. |
`-----------------------------------*/
bool *used_assoc = NULL;
/*---------------------------------.
| Create a new symbol, named TAG. |
`---------------------------------*/
@@ -1073,3 +1079,61 @@ print_precedence_warnings (void)
_("useless precedence for %s"), s->tag);
}
}
/*---------------------------------------.
| Initialize association tracking table. |
`---------------------------------------*/
static void
init_assoc (void)
{
graphid i;
used_assoc = xcalloc(nsyms, sizeof(*used_assoc));
for (i = 0; i < nsyms; ++i)
used_assoc[i] = false;
}
/*------------------------------------------------------------------.
| Test if the associativity for the symbols is defined and useless. |
`------------------------------------------------------------------*/
static inline bool
is_assoc_useless (symbol *s)
{
return s
&& s->assoc != undef_assoc
&& s->assoc != precedence_assoc
&& !used_assoc[s->number];
}
/*-------------------------------.
| Register a used associativity. |
`-------------------------------*/
void
register_assoc (graphid i, graphid j)
{
if (!used_assoc)
init_assoc ();
used_assoc[i] = true;
used_assoc[j] = true;
}
/*------------------------------------------------------.
| Print a warning for each unused symbol associativity. |
`------------------------------------------------------*/
void
print_assoc_warnings (void)
{
graphid i;
if (!used_assoc)
init_assoc ();
for (i = 0; i < nsyms; ++i)
{
symbol *s = symbols[i];
if (is_assoc_useless (s))
complain (&s->location, Wother,
_("useless associativity for %s"), s->tag);
}
}

View File

@@ -270,6 +270,14 @@ void register_precedence (graphid first, graphid snd);
void print_precedence_warnings (void);
/*----------------------.
| Symbol associativity |
`----------------------*/
void register_assoc (int i, int j);
void print_assoc_warnings (void);
/*-----------------.
| Semantic types. |
`-----------------*/