mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-22 10:43:02 +00:00
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:
15
src/gram.c
15
src/gram.c
@@ -237,27 +237,28 @@ grammar_dump (FILE *out, const char *title)
|
|||||||
|
|
||||||
fprintf (out, "Rules\n-----\n\n");
|
fprintf (out, "Rules\n-----\n\n");
|
||||||
{
|
{
|
||||||
|
/* Reduced number, then original number in the sources. */
|
||||||
fprintf (out,
|
fprintf (out,
|
||||||
"Num (Prec, Assoc, Useful, Ritem Range) Lhs"
|
"Num (Num, Prec, Assoc, Useful, UselessChain, Ritem Range)"
|
||||||
" -> Rhs (Ritem range) [Num]\n");
|
" Lhs -> Rhs (Ritem range)\n");
|
||||||
for (rule_number i = 0; i < nrules + nuseless_productions; i++)
|
for (rule_number i = 0; i < nrules + nuseless_productions; ++i)
|
||||||
{
|
{
|
||||||
rule const *rule_i = &rules[i];
|
rule const *rule_i = &rules[i];
|
||||||
unsigned const rhs_itemno = rule_i->rhs - ritem;
|
unsigned const rhs_itemno = rule_i->rhs - ritem;
|
||||||
unsigned length = rule_rhs_length (rule_i);
|
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,
|
i,
|
||||||
rule_i->prec ? rule_i->prec->prec : 0,
|
rule_i->prec ? rule_i->prec->prec : 0,
|
||||||
rule_i->prec ? rule_i->prec->assoc : 0,
|
rule_i->prec ? rule_i->prec->assoc : 0,
|
||||||
rule_i->useful,
|
rule_i->useful ? "t" : "f",
|
||||||
rhs_itemno,
|
rhs_itemno,
|
||||||
rhs_itemno + length - 1,
|
rhs_itemno + length - 1,
|
||||||
rule_i->lhs->number);
|
rule_i->lhs->number);
|
||||||
/* Dumped the RHS. */
|
/* Dumped the RHS. */
|
||||||
for (item_number *rhsp = rule_i->rhs; 0 <= *rhsp; ++rhsp)
|
for (item_number *rhsp = rule_i->rhs; 0 <= *rhsp; ++rhsp)
|
||||||
fprintf (out, " %3d", *rhsp);
|
fprintf (out, " %3d", *rhsp);
|
||||||
fprintf (out, " [%d]\n",
|
fputc ('\n', out);
|
||||||
item_number_as_rule_number (rule_i->rhs[length+1]));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fprintf (out, "\n\n");
|
fprintf (out, "\n\n");
|
||||||
|
|||||||
30
src/reduce.c
30
src/reduce.c
@@ -378,23 +378,23 @@ reduce_grammar (void)
|
|||||||
inaccessable_symbols ();
|
inaccessable_symbols ();
|
||||||
|
|
||||||
/* Did we reduce something? */
|
/* Did we reduce something? */
|
||||||
if (!nuseless_nonterminals && !nuseless_productions)
|
if (nuseless_nonterminals || nuseless_productions)
|
||||||
return;
|
{
|
||||||
|
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))
|
/* First reduce the nonterminals, as they renumber themselves in the
|
||||||
complain (&startsymbol_location, fatal,
|
whole grammar. If you change the order, nonterms would be
|
||||||
_("start symbol %s does not derive any sentence"),
|
renumbered only in the reduced grammar. */
|
||||||
startsymbol->tag);
|
if (nuseless_nonterminals)
|
||||||
|
nonterminals_reduce ();
|
||||||
/* First reduce the nonterminals, as they renumber themselves in the
|
if (nuseless_productions)
|
||||||
whole grammar. If you change the order, nonterms would be
|
reduce_grammar_tables ();
|
||||||
renumbered only in the reduced grammar. */
|
}
|
||||||
if (nuseless_nonterminals)
|
|
||||||
nonterminals_reduce ();
|
|
||||||
if (nuseless_productions)
|
|
||||||
reduce_grammar_tables ();
|
|
||||||
|
|
||||||
if (trace_flag & trace_grammar)
|
if (trace_flag & trace_grammar)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -300,3 +300,63 @@ AT_CHECK([sed -n '
|
|||||||
0, [expout])
|
0, [expout])
|
||||||
|
|
||||||
AT_CLEANUP
|
AT_CLEANUP
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
## ----------------- ##
|
||||||
|
## Reduced Grammar. ##
|
||||||
|
## ----------------- ##
|
||||||
|
|
||||||
|
# Check information about the grammar, once reduced.
|
||||||
|
|
||||||
|
AT_SETUP([Reduced Grammar])
|
||||||
|
|
||||||
|
AT_DATA([input.y],
|
||||||
|
[[%%
|
||||||
|
expr: expr "+" term | term
|
||||||
|
term: term "*" fact | fact
|
||||||
|
fact: "num"
|
||||||
|
]])
|
||||||
|
|
||||||
|
AT_BISON_CHECK([[--trace=grammar -o input.c input.y]], [], [],
|
||||||
|
[[Reduced Grammar
|
||||||
|
|
||||||
|
ntokens = 6, nvars = 4, nsyms = 10, nrules = 6, nritems = 17
|
||||||
|
|
||||||
|
Variables
|
||||||
|
---------
|
||||||
|
|
||||||
|
Value Sprec Sassoc Tag
|
||||||
|
6 0 0 $accept
|
||||||
|
7 0 0 expr
|
||||||
|
8 0 0 term
|
||||||
|
9 0 0 fact
|
||||||
|
|
||||||
|
|
||||||
|
Rules
|
||||||
|
-----
|
||||||
|
|
||||||
|
Num (Num, Prec, Assoc, Useful, UselessChain, Ritem Range) Lhs -> Rhs (Ritem range)
|
||||||
|
0 ( 0, 0, 0, t, 0- 1) 6 -> 7 0
|
||||||
|
1 ( 1, 0, 0, t, 3- 5) 7 -> 7 3 8
|
||||||
|
2 ( 2, 0, 0, t, 7- 7) 7 -> 8
|
||||||
|
3 ( 3, 0, 0, t, 9-11) 8 -> 8 4 9
|
||||||
|
4 ( 4, 0, 0, t, 13-13) 8 -> 9
|
||||||
|
5 ( 5, 0, 0, t, 15-15) 9 -> 5
|
||||||
|
|
||||||
|
|
||||||
|
Rules interpreted
|
||||||
|
-----------------
|
||||||
|
|
||||||
|
0 $accept: expr $end
|
||||||
|
1 expr: expr "+" term
|
||||||
|
2 expr: term
|
||||||
|
3 term: term "*" fact
|
||||||
|
4 term: fact
|
||||||
|
5 fact: "num"
|
||||||
|
|
||||||
|
|
||||||
|
reduced input.y defines 6 terminals, 4 nonterminals, and 6 productions.
|
||||||
|
]])
|
||||||
|
|
||||||
|
AT_CLEANUP
|
||||||
|
|||||||
Reference in New Issue
Block a user