traces: always print the reduced grammar and fix it

* src/gram.c (grammar_dump): Print the effective number first instead
of last.  And fix it (remove the incorrect "+1").
Use t/f for Booleans.
* src/reduce.c: When asked, always print the reduced grammar, even if
there was nothing useless.
* tests/sets.at (Reduced Grammar): Check that.
This commit is contained in:
Akim Demaille
2019-01-23 07:26:23 +01:00
parent 83463dfbee
commit 21a7fa8063
3 changed files with 83 additions and 22 deletions

View File

@@ -237,27 +237,28 @@ grammar_dump (FILE *out, const char *title)
fprintf (out, "Rules\n-----\n\n");
{
/* Reduced number, then original number in the sources. */
fprintf (out,
"Num (Prec, Assoc, Useful, Ritem Range) Lhs"
" -> Rhs (Ritem range) [Num]\n");
for (rule_number i = 0; i < nrules + nuseless_productions; i++)
"Num (Num, Prec, Assoc, Useful, UselessChain, Ritem Range)"
" Lhs -> Rhs (Ritem range)\n");
for (rule_number i = 0; i < nrules + nuseless_productions; ++i)
{
rule const *rule_i = &rules[i];
unsigned const rhs_itemno = rule_i->rhs - ritem;
unsigned length = rule_rhs_length (rule_i);
fprintf (out, "%3d (%2d, %2d, %2d, %2u-%2u) %2d ->",
fprintf (out, "%3d (%3d, %2d, %2d, %2s, %2u-%2u) %2d ->",
item_number_as_rule_number (rule_i->rhs[length]),
i,
rule_i->prec ? rule_i->prec->prec : 0,
rule_i->prec ? rule_i->prec->assoc : 0,
rule_i->useful,
rule_i->useful ? "t" : "f",
rhs_itemno,
rhs_itemno + length - 1,
rule_i->lhs->number);
/* Dumped the RHS. */
for (item_number *rhsp = rule_i->rhs; 0 <= *rhsp; ++rhsp)
fprintf (out, " %3d", *rhsp);
fprintf (out, " [%d]\n",
item_number_as_rule_number (rule_i->rhs[length+1]));
fputc ('\n', out);
}
}
fprintf (out, "\n\n");

View File

@@ -378,23 +378,23 @@ reduce_grammar (void)
inaccessable_symbols ();
/* Did we reduce something? */
if (!nuseless_nonterminals && !nuseless_productions)
return;
if (nuseless_nonterminals || nuseless_productions)
{
reduce_print ();
reduce_print ();
if (!bitset_test (N, accept->content->number - ntokens))
complain (&startsymbol_location, fatal,
_("start symbol %s does not derive any sentence"),
startsymbol->tag);
if (!bitset_test (N, accept->content->number - ntokens))
complain (&startsymbol_location, fatal,
_("start symbol %s does not derive any sentence"),
startsymbol->tag);
/* First reduce the nonterminals, as they renumber themselves in the
whole grammar. If you change the order, nonterms would be
renumbered only in the reduced grammar. */
if (nuseless_nonterminals)
nonterminals_reduce ();
if (nuseless_productions)
reduce_grammar_tables ();
/* First reduce the nonterminals, as they renumber themselves in the
whole grammar. If you change the order, nonterms would be
renumbered only in the reduced grammar. */
if (nuseless_nonterminals)
nonterminals_reduce ();
if (nuseless_productions)
reduce_grammar_tables ();
}
if (trace_flag & trace_grammar)
{