mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-18 08:43:03 +00:00
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:
9
NEWS
9
NEWS
@@ -33,13 +33,14 @@ GNU Bison NEWS
|
|||||||
^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
|
|
||||||
Now, rules whose left-hand side symbol is useless are no longer reported
|
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: 1 nonterminal useless in grammar [-Wother]
|
||||||
warning: 4 rules useless in grammar [-Wother]
|
warning: 4 rules useless in grammar [-Wother]
|
||||||
2.14-16: warning: nonterminal useless in grammar: exp [-Wother]
|
3.1-3: warning: nonterminal useless in grammar: exp [-Wother]
|
||||||
input: '0' | exp
|
exp: exp '+' exp | exp '-' exp | '(' exp ')'
|
||||||
^^^
|
^^^
|
||||||
2.14-16: warning: rule useless in grammar [-Wother]
|
2.14-16: warning: rule useless in grammar [-Wother]
|
||||||
input: '0' | exp
|
input: '0' | exp
|
||||||
^^^
|
^^^
|
||||||
|
|||||||
@@ -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
|
/* Bison implementation for Yacc-like parsers in C
|
||||||
|
|
||||||
@@ -44,7 +44,7 @@
|
|||||||
#define YYBISON 1
|
#define YYBISON 1
|
||||||
|
|
||||||
/* Bison version. */
|
/* Bison version. */
|
||||||
#define YYBISON_VERSION "3.0.2.13-975bb-dirty"
|
#define YYBISON_VERSION "3.0.2.29-9a91e"
|
||||||
|
|
||||||
/* Skeleton name. */
|
/* Skeleton name. */
|
||||||
#define YYSKELETON_NAME "yacc.c"
|
#define YYSKELETON_NAME "yacc.c"
|
||||||
@@ -3118,6 +3118,8 @@ current_lhs (symbol *sym, location loc, named_ref *ref)
|
|||||||
{
|
{
|
||||||
current_lhs_symbol = sym;
|
current_lhs_symbol = sym;
|
||||||
current_lhs_location = loc;
|
current_lhs_location = loc;
|
||||||
|
if (sym)
|
||||||
|
symbol_location_as_lhs_set (sym, loc);
|
||||||
/* In order to simplify memory management, named references for lhs
|
/* In order to simplify memory management, named references for lhs
|
||||||
are always assigned by deep copy into the current symbol_list
|
are always assigned by deep copy into the current symbol_list
|
||||||
node. This is because a single named-ref in the grammar may
|
node. This is because a single named-ref in the grammar may
|
||||||
|
|||||||
@@ -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
|
/* Bison interface for Yacc-like parsers in C
|
||||||
|
|
||||||
|
|||||||
@@ -865,6 +865,8 @@ current_lhs (symbol *sym, location loc, named_ref *ref)
|
|||||||
{
|
{
|
||||||
current_lhs_symbol = sym;
|
current_lhs_symbol = sym;
|
||||||
current_lhs_location = loc;
|
current_lhs_location = loc;
|
||||||
|
if (sym)
|
||||||
|
symbol_location_as_lhs_set (sym, loc);
|
||||||
/* In order to simplify memory management, named references for lhs
|
/* In order to simplify memory management, named references for lhs
|
||||||
are always assigned by deep copy into the current symbol_list
|
are always assigned by deep copy into the current symbol_list
|
||||||
node. This is because a single named-ref in the grammar may
|
node. This is because a single named-ref in the grammar may
|
||||||
|
|||||||
@@ -105,6 +105,7 @@ symbol_new (uniqstr tag, location loc)
|
|||||||
|
|
||||||
res->tag = tag;
|
res->tag = tag;
|
||||||
res->location = loc;
|
res->location = loc;
|
||||||
|
res->location_of_lhs = false;
|
||||||
res->alias = NULL;
|
res->alias = NULL;
|
||||||
res->content = sym_content_new (res);
|
res->content = sym_content_new (res);
|
||||||
res->is_alias = false;
|
res->is_alias = false;
|
||||||
@@ -189,6 +190,7 @@ code_props_type_string (code_props_type kind)
|
|||||||
assert (0);
|
assert (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*----------------------------------------.
|
/*----------------------------------------.
|
||||||
| Create a new semantic type, named TAG. |
|
| 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 |
|
| Set the TYPE_NAME associated with SYM. Does nothing if passed 0 |
|
||||||
|
|||||||
19
src/symtab.h
19
src/symtab.h
@@ -87,10 +87,19 @@ struct symbol
|
|||||||
{
|
{
|
||||||
/** The key, name of the symbol. */
|
/** The key, name of the symbol. */
|
||||||
uniqstr tag;
|
uniqstr tag;
|
||||||
/** The location of its first occurrence. */
|
|
||||||
|
/** The "defining" location. */
|
||||||
location 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;
|
symbol *alias;
|
||||||
|
|
||||||
/** Whether this symbol is the alias of another or not. */
|
/** 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);
|
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.
|
/** Set the \c type_name associated with \c sym.
|
||||||
|
|
||||||
Do nothing if passed 0 as \c type_name. */
|
Do nothing if passed 0 as \c type_name. */
|
||||||
|
|||||||
@@ -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-reduced.y:14.1-13: warning: nonterminal useless in grammar: not_reachable [-Wother]
|
||||||
not_reachable: useful { /* A not reachable action. */ }
|
not_reachable: useful { /* A not reachable action. */ }
|
||||||
^^^^^^^^^^^^^
|
^^^^^^^^^^^^^
|
||||||
not-reduced.y:11.6-19: warning: nonterminal useless in grammar: non_productive [-Wother]
|
not-reduced.y:17.1-14: warning: nonterminal useless in grammar: non_productive [-Wother]
|
||||||
| non_productive { /* A non productive action. */ }
|
non_productive: non_productive useless_token
|
||||||
^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^
|
||||||
not-reduced.y:11.6-57: warning: rule useless in grammar [-Wother]
|
not-reduced.y:11.6-57: warning: rule useless in grammar [-Wother]
|
||||||
| non_productive { /* A non productive action. */ }
|
| non_productive { /* A non productive action. */ }
|
||||||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
||||||
@@ -330,12 +330,12 @@ indirection: underivable;
|
|||||||
AT_BISON_CHECK([[-fcaret input.y]], 0, [],
|
AT_BISON_CHECK([[-fcaret input.y]], 0, [],
|
||||||
[[input.y: warning: 2 nonterminals useless in grammar [-Wother]
|
[[input.y: warning: 2 nonterminals useless in grammar [-Wother]
|
||||||
input.y: warning: 3 rules 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]
|
input.y:6.1-11: warning: nonterminal useless in grammar: underivable [-Wother]
|
||||||
exp: useful | underivable;
|
|
||||||
^^^^^^^^^^^
|
|
||||||
input.y:6.14-24: warning: nonterminal useless in grammar: indirection [-Wother]
|
|
||||||
underivable: indirection;
|
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]
|
input.y:5.15-25: warning: rule useless in grammar [-Wother]
|
||||||
exp: useful | underivable;
|
exp: useful | underivable;
|
||||||
^^^^^^^^^^^
|
^^^^^^^^^^^
|
||||||
|
|||||||
Reference in New Issue
Block a user