symbol: use the first occurrence as an LHS as defining location

Currently on the following grammar:

    %type <foo> foo
    %%
    start: foo | bar | "baz"
    foo: foo
    bar: bar

bison reports:

    warning: 2 nonterminals useless in grammar [-Wother]
    warning: 4 rules useless in grammar [-Wother]
    1.13-15: warning: nonterminal useless in grammar: foo [-Wother]
     %type <foo> foo
                 ^^^
    3.14-16: warning: nonterminal useless in grammar: bar [-Wother]
     start: foo | bar | "baz"
                  ^^^
    [...]

i.e., the location of the first occurrence of a symbol is taken as its
definition point.  In the case of nonterminals, the first occurrence
as a left-hand side of a rule makes more sense:

    warning: 2 nonterminals useless in grammar [-Wother]
    warning: 4 rules useless in grammar [-Wother]
    4.1-3: warning: nonterminal useless in grammar: foo [-Wother]
     foo: foo
     ^^^
    5.1-3: warning: nonterminal useless in grammar: bar [-Wother]
     bar: bar
     ^^^
    [...]

* src/symtab.h, src/symtab.c (symbol::location_of_lhs): New.
(symbol_location_as_lhs_set): New.
* src/parse-gram.y (current_lhs): Use it.
* tests/reduce.at: Update locations.
This commit is contained in:
Akim Demaille
2015-01-14 13:41:32 +01:00
parent 650af77812
commit 875ef1b90c
7 changed files with 46 additions and 17 deletions

9
NEWS
View File

@@ -33,13 +33,14 @@ GNU Bison NEWS
^^^^^^^^^^^
Now, rules whose left-hand side symbol is useless are no longer reported
as useless.
as useless. The locations of the errors have also been adjusted to point
to the first use of the nonterminal as a left-hand side of a rule:
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
^^^
3.1-3: warning: nonterminal useless in grammar: exp [-Wother]
exp: exp '+' exp | exp '-' exp | '(' exp ')'
^^^
2.14-16: warning: rule useless in grammar [-Wother]
input: '0' | exp
^^^

View File

@@ -1,4 +1,4 @@
/* A Bison parser, made by GNU Bison 3.0.2.13-975bb-dirty. */
/* A Bison parser, made by GNU Bison 3.0.2.29-9a91e. */
/* Bison implementation for Yacc-like parsers in C
@@ -44,7 +44,7 @@
#define YYBISON 1
/* Bison version. */
#define YYBISON_VERSION "3.0.2.13-975bb-dirty"
#define YYBISON_VERSION "3.0.2.29-9a91e"
/* Skeleton name. */
#define YYSKELETON_NAME "yacc.c"
@@ -3118,6 +3118,8 @@ current_lhs (symbol *sym, location loc, named_ref *ref)
{
current_lhs_symbol = sym;
current_lhs_location = loc;
if (sym)
symbol_location_as_lhs_set (sym, loc);
/* In order to simplify memory management, named references for lhs
are always assigned by deep copy into the current symbol_list
node. This is because a single named-ref in the grammar may

View File

@@ -1,4 +1,4 @@
/* A Bison parser, made by GNU Bison 3.0.2.13-975bb-dirty. */
/* A Bison parser, made by GNU Bison 3.0.2.29-9a91e. */
/* Bison interface for Yacc-like parsers in C

View File

@@ -865,6 +865,8 @@ current_lhs (symbol *sym, location loc, named_ref *ref)
{
current_lhs_symbol = sym;
current_lhs_location = loc;
if (sym)
symbol_location_as_lhs_set (sym, loc);
/* In order to simplify memory management, named references for lhs
are always assigned by deep copy into the current symbol_list
node. This is because a single named-ref in the grammar may

View File

@@ -105,6 +105,7 @@ symbol_new (uniqstr tag, location loc)
res->tag = tag;
res->location = loc;
res->location_of_lhs = false;
res->alias = NULL;
res->content = sym_content_new (res);
res->is_alias = false;
@@ -189,6 +190,7 @@ code_props_type_string (code_props_type kind)
assert (0);
}
/*----------------------------------------.
| Create a new semantic type, named TAG. |
`----------------------------------------*/
@@ -307,6 +309,13 @@ semantic_type_redeclaration (semantic_type *s, const char *what, location first,
}
void
symbol_location_as_lhs_set (symbol *sym, location loc)
{
if (!sym->location_of_lhs)
sym->location = loc;
}
/*-----------------------------------------------------------------.
| Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |

View File

@@ -87,10 +87,19 @@ struct symbol
{
/** The key, name of the symbol. */
uniqstr tag;
/** The location of its first occurrence. */
/** The "defining" location. */
location location;
/* Points to the other in the symbol-string pair for an alias. */
/** Whether \a location is about the first uses as left-hand side
symbol of a rule (true), or simply the first occurrence (e.g.,
in a %type, or as a rhs symbol of a rule). The former type of
location is more natural in error messages. This Boolean helps
moving from location of the first occurrence to first use as
lhs. */
bool location_of_lhs;
/** Points to the other in the symbol-string pair for an alias. */
symbol *alias;
/** Whether this symbol is the alias of another or not. */
@@ -177,6 +186,12 @@ uniqstr symbol_id_get (symbol const *sym);
*/
void symbol_make_alias (symbol *sym, symbol *str, location loc);
/**
* This symbol is used as the lhs of a rule. Record this location
* as definition point, if not already done.
*/
void symbol_location_as_lhs_set (symbol *sym, location loc);
/** Set the \c type_name associated with \c sym.
Do nothing if passed 0 as \c type_name. */

View File

@@ -254,9 +254,9 @@ not-reduced.y: warning: 3 rules useless in grammar [-Wother]
not-reduced.y:14.1-13: warning: nonterminal useless in grammar: not_reachable [-Wother]
not_reachable: useful { /* A not reachable action. */ }
^^^^^^^^^^^^^
not-reduced.y:11.6-19: warning: nonterminal useless in grammar: non_productive [-Wother]
| non_productive { /* A non productive action. */ }
^^^^^^^^^^^^^^
not-reduced.y:17.1-14: warning: nonterminal useless in grammar: non_productive [-Wother]
non_productive: non_productive useless_token
^^^^^^^^^^^^^^
not-reduced.y:11.6-57: warning: rule useless in grammar [-Wother]
| non_productive { /* A non productive action. */ }
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -330,12 +330,12 @@ indirection: underivable;
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]
input.y:6.1-11: warning: nonterminal useless in grammar: underivable [-Wother]
underivable: indirection;
^^^^^^^^^^^
^^^^^^^^^^^
input.y:7.1-11: warning: nonterminal useless in grammar: indirection [-Wother]
indirection: underivable;
^^^^^^^^^^^
input.y:5.15-25: warning: rule useless in grammar [-Wother]
exp: useful | underivable;
^^^^^^^^^^^