reduce: don't complain about rules whose lhs is useless

In the following grammar, the 'exp' nonterminal is trivially useless.
So, of course, its rules are useless too.

    %%
    input: '0' | exp
    exp: exp '+' exp | exp '-' exp | '(' exp ')'

Previously all the useless rules were reported, including those whose
left-hand side is the 'exp' nonterminal:

    warning: 1 nonterminal useless in grammar [-Wother]
    warning: 4 rules useless in grammar [-Wother]
    2.14-16: warning: nonterminal useless in grammar: exp [-Wother]
     input: '0' | exp
                  ^^^
    2.14-16: warning: rule useless in grammar [-Wother]
     input: '0' | exp
                  ^^^
  ! 3.6-16: warning: rule useless in grammar [-Wother]
  !  exp: exp '+' exp | exp '-' exp | '(' exp ')'
  !       ^^^^^^^^^^^
  ! 3.20-30: warning: rule useless in grammar [-Wother]
  !  exp: exp '+' exp | exp '-' exp | '(' exp ')'
  !                     ^^^^^^^^^^^
  ! 3.34-44: warning: rule useless in grammar [-Wother]
  !  exp: exp '+' exp | exp '-' exp | '(' exp ')'
  !                                   ^^^^^^^^^^^

The interest of being so verbose is dubious.  I suspect most of the
time nonterminals are not expected to be useless, so the user wants to
fix the nonterminal, not remove its rules.  And even if the user
wanted to get rid of its rules, the position of these rules probably
does not help more that just having the name of the nonterminal.

This commit discard these messages, marked with '!', and keep the
others.  In particular, we still report:

    2.14-16: warning: rule useless in grammar [-Wother]
     input: '0' | exp
                  ^^^

All the useless rules (including the '!' ones) are still reported in
the reports (xml, text, etc.); only the diagnostics on stderr change.

* src/gram.c (grammar_rules_useless_report): Don't complain about
useless rules whose lhs is useless.
* src/reduce.h, src/reduce.c (reduce_nonterminal_useless_in_grammar):
Take a sym_content as argument.
Adjust callers.
* tests/reduce.at (Useless Rules, Underivable Rules, Reduced Automaton):
Adjust.
This commit is contained in:
Akim Demaille
2015-01-13 13:53:02 +01:00
parent c03a8db0cc
commit 650af77812
6 changed files with 66 additions and 42 deletions

View File

@@ -172,33 +172,6 @@ input.y:13.1-8: warning: nonterminal useless in grammar: useless8 [-Wother]
input.y:14.1-8: warning: nonterminal useless in grammar: useless9 [-Wother]
useless9: '9';
^^^^^^^^
input.y:6.11-13: warning: rule useless in grammar [-Wother]
useless1: '1';
^^^
input.y:7.11-13: warning: rule useless in grammar [-Wother]
useless2: '2';
^^^
input.y:8.11-13: warning: rule useless in grammar [-Wother]
useless3: '3';
^^^
input.y:9.11-13: warning: rule useless in grammar [-Wother]
useless4: '4';
^^^
input.y:10.11-13: warning: rule useless in grammar [-Wother]
useless5: '5';
^^^
input.y:11.11-13: warning: rule useless in grammar [-Wother]
useless6: '6';
^^^
input.y:12.11-13: warning: rule useless in grammar [-Wother]
useless7: '7';
^^^
input.y:13.11-13: warning: rule useless in grammar [-Wother]
useless8: '8';
^^^
input.y:14.11-13: warning: rule useless in grammar [-Wother]
useless9: '9';
^^^
]])
@@ -287,12 +260,6 @@ not-reduced.y:11.6-19: warning: nonterminal useless in grammar: non_productive [
not-reduced.y:11.6-57: warning: rule useless in grammar [-Wother]
| non_productive { /* A non productive action. */ }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
not-reduced.y:14.16-56: warning: rule useless in grammar [-Wother]
not_reachable: useful { /* A not reachable action. */ }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
not-reduced.y:17.17-18.63: warning: rule useless in grammar [-Wother]
non_productive: non_productive useless_token
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
]])
AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' not-reduced.output]], 0,
@@ -360,14 +327,18 @@ underivable: indirection;
indirection: underivable;
]])
AT_BISON_CHECK([[input.y]], 0, [],
AT_BISON_CHECK([[-fcaret input.y]], 0, [],
[[input.y: warning: 2 nonterminals useless in grammar [-Wother]
input.y: warning: 3 rules useless in grammar [-Wother]
input.y:5.15-25: warning: nonterminal useless in grammar: underivable [-Wother]
exp: useful | underivable;
^^^^^^^^^^^
input.y:6.14-24: warning: nonterminal useless in grammar: indirection [-Wother]
underivable: indirection;
^^^^^^^^^^^
input.y:5.15-25: warning: rule useless in grammar [-Wother]
input.y:6.14-24: warning: rule useless in grammar [-Wother]
input.y:7.14-24: warning: rule useless in grammar [-Wother]
exp: useful | underivable;
^^^^^^^^^^^
]])
AT_CHECK([[sed -n '/^Grammar/q;/^$/!p' input.output]], 0,