bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


From: Akim Demaille
Subject: master: don't complain about rules whose lhs is useless
Date: Wed, 14 Jan 2015 13:12:29 +0100

The following change (in master, so for 3.1 or so) reduces
the verbosity of some error messages.

commit 18b0bc47a3fce08cffff686e9279943f93797211
Author: Akim Demaille <address@hidden>
Date:   Tue Jan 13 13:53:02 2015 +0100

    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.

diff --git a/NEWS b/NEWS
index 1877194..45c066d 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,50 @@ GNU Bison NEWS
 
 * Noteworthy changes in release ?.? (????-??-??) [?]
 
+** Diagnositcs about useless rules
+
+  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 ')'
+                                      ^^^^^^^^^^^
+
+  Now, rules whose left-hand side symbol is useless are no longer reported
+  as useless.
+
+    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
+                  ^^^
+
+* Noteworthy changes in release ?.? (????-??-??) [?]
+
 ** Bug fixes
 
 *** C++ with Variants (lalr1.cc)
diff --git a/src/gram.c b/src/gram.c
index abd1872..594d835 100644
--- a/src/gram.c
+++ b/src/gram.c
@@ -293,7 +293,10 @@ grammar_rules_useless_report (const char *message)
 {
   rule_number r;
   for (r = 0; r < nrules ; ++r)
-    if (!rules[r].useful)
+    /* Don't complain about rules whose LHS is useless, we already
+       complained about it.  */
+    if (!reduce_nonterminal_useless_in_grammar (rules[r].lhs)
+        && !rules[r].useful)
       complain (&rules[r].location, Wother, "%s", message);
 }
 
diff --git a/src/print-xml.c b/src/print-xml.c
index 81a491c..f156424 100644
--- a/src/print-xml.c
+++ b/src/print-xml.c
@@ -417,7 +417,7 @@ print_grammar (FILE *out, int level)
                   "<nonterminal symbol-number=\"%d\" name=\"%s\""
                   " usefulness=\"%s\"/>",
                   i, xml_escape (tag),
-                  reduce_nonterminal_useless_in_grammar (i)
+                  reduce_nonterminal_useless_in_grammar (symbols[i]->content)
                     ? "useless-in-grammar" : "useful");
     }
   xml_puts (out, level + 1, "</nonterminals>");
diff --git a/src/reduce.c b/src/reduce.c
index 46b38ff..316833b 100644
--- a/src/reduce.c
+++ b/src/reduce.c
@@ -442,10 +442,11 @@ reduce_token_unused_in_grammar (symbol_number i)
 }
 
 bool
-reduce_nonterminal_useless_in_grammar (symbol_number i)
+reduce_nonterminal_useless_in_grammar (const sym_content *sym)
 {
-  aver (ntokens <= i && i < nsyms + nuseless_nonterminals);
-  return nsyms <= i;
+  symbol_number n = sym->number;
+  aver (ntokens <= n && n < nsyms + nuseless_nonterminals);
+  return nsyms <= n;
 }
 
 /*-----------------------------------------------------------.
diff --git a/src/reduce.h b/src/reduce.h
index 0dab504..254f7bc 100644
--- a/src/reduce.h
+++ b/src/reduce.h
@@ -24,7 +24,12 @@
 void reduce_grammar (void);
 void reduce_output (FILE *out);
 bool reduce_token_unused_in_grammar (symbol_number i);
-bool reduce_nonterminal_useless_in_grammar (symbol_number i);
+
+/** Whether symbol \a i is useless in the grammar.
+ * \pre  reduce_grammar was called before.
+ */
+bool reduce_nonterminal_useless_in_grammar (const sym_content *sym);
+
 void reduce_free (void);
 
 extern unsigned nuseless_nonterminals;
diff --git a/tests/reduce.at b/tests/reduce.at
index c28635e..c7edb10 100644
--- a/tests/reduce.at
+++ b/tests/reduce.at
@@ -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,




reply via email to

[Prev in Thread] Current Thread [Next in Thread]