mirror of
https://git.savannah.gnu.org/git/bison.git
synced 2026-03-09 12:23:04 +00:00
style: use midrule only, not mid-rule
The code was already using midrule only, never mid_rule. This is simpler to remember, and matches a similar change we made from look-ahead to lookahead. * NEWS, doc/bison.texi, src/reader.c, src/scan-code.h, src/scan-code.l * tests/actions.at, tests/c++.at, tests/existing.at: here.
This commit is contained in:
22
NEWS
22
NEWS
@@ -1024,7 +1024,7 @@ GNU Bison NEWS
|
|||||||
Although introduced more than four years ago, XML and Graphviz reports
|
Although introduced more than four years ago, XML and Graphviz reports
|
||||||
were not properly documented.
|
were not properly documented.
|
||||||
|
|
||||||
The translation of mid-rule actions is now described.
|
The translation of midrule actions is now described.
|
||||||
|
|
||||||
* Noteworthy changes in release 2.6.5 (2012-11-07) [stable]
|
* Noteworthy changes in release 2.6.5 (2012-11-07) [stable]
|
||||||
|
|
||||||
@@ -2020,16 +2020,16 @@ GNU Bison NEWS
|
|||||||
The prologue alternatives are experimental. More user feedback will help to
|
The prologue alternatives are experimental. More user feedback will help to
|
||||||
determine whether they should become permanent features.
|
determine whether they should become permanent features.
|
||||||
|
|
||||||
** Revised warning: unset or unused mid-rule values
|
** Revised warning: unset or unused midrule values
|
||||||
|
|
||||||
Since Bison 2.2, Bison has warned about mid-rule values that are set but not
|
Since Bison 2.2, Bison has warned about midrule values that are set but not
|
||||||
used within any of the actions of the parent rule. For example, Bison warns
|
used within any of the actions of the parent rule. For example, Bison warns
|
||||||
about unused $2 in:
|
about unused $2 in:
|
||||||
|
|
||||||
exp: '1' { $$ = 1; } '+' exp { $$ = $1 + $4; };
|
exp: '1' { $$ = 1; } '+' exp { $$ = $1 + $4; };
|
||||||
|
|
||||||
Now, Bison also warns about mid-rule values that are used but not set. For
|
Now, Bison also warns about midrule values that are used but not set. For
|
||||||
example, Bison warns about unset $$ in the mid-rule action in:
|
example, Bison warns about unset $$ in the midrule action in:
|
||||||
|
|
||||||
exp: '1' { $1 = 1; } '+' exp { $$ = $2 + $4; };
|
exp: '1' { $1 = 1; } '+' exp { $$ = $2 + $4; };
|
||||||
|
|
||||||
@@ -2055,7 +2055,7 @@ GNU Bison NEWS
|
|||||||
|
|
||||||
Bison no longer supports the "%symbol-default" notation from Bison 2.3a.
|
Bison no longer supports the "%symbol-default" notation from Bison 2.3a.
|
||||||
"<*>" and "<>" combined achieve the same effect with one exception: Bison no
|
"<*>" and "<>" combined achieve the same effect with one exception: Bison no
|
||||||
longer applies any %destructor to a mid-rule value if that mid-rule value is
|
longer applies any %destructor to a midrule value if that midrule value is
|
||||||
not actually ever referenced using either $$ or $n in a semantic action.
|
not actually ever referenced using either $$ or $n in a semantic action.
|
||||||
|
|
||||||
The default %destructor's and %printer's are experimental. More user
|
The default %destructor's and %printer's are experimental. More user
|
||||||
@@ -2233,7 +2233,7 @@ GNU Bison NEWS
|
|||||||
| exp "+" exp { $$ = $1; (void) $3; }
|
| exp "+" exp { $$ = $1; (void) $3; }
|
||||||
;
|
;
|
||||||
|
|
||||||
If there are mid-rule actions, the warning is issued if no action
|
If there are midrule actions, the warning is issued if no action
|
||||||
uses it. The following triggers no warning: $1 and $3 are used.
|
uses it. The following triggers no warning: $1 and $3 are used.
|
||||||
|
|
||||||
exp: exp { push ($1); } '+' exp { push ($3); sum (); };
|
exp: exp { push ($1); } '+' exp { push ($3); sum (); };
|
||||||
@@ -2508,13 +2508,13 @@ GNU Bison NEWS
|
|||||||
|
|
||||||
typed: ... untyped;
|
typed: ... untyped;
|
||||||
|
|
||||||
** Values of mid-rule actions
|
** Values of midrule actions
|
||||||
The following code:
|
The following code:
|
||||||
|
|
||||||
foo: { ... } { $$ = $1; } ...
|
foo: { ... } { $$ = $1; } ...
|
||||||
|
|
||||||
was incorrectly rejected: $1 is defined in the second mid-rule
|
was incorrectly rejected: $1 is defined in the second midrule
|
||||||
action, and is equal to the $$ of the first mid-rule action.
|
action, and is equal to the $$ of the first midrule action.
|
||||||
|
|
||||||
* Changes in version 1.50, 2002-10-04:
|
* Changes in version 1.50, 2002-10-04:
|
||||||
|
|
||||||
@@ -2650,7 +2650,7 @@ GNU Bison NEWS
|
|||||||
|
|
||||||
** Type clashes
|
** Type clashes
|
||||||
Previous versions don't complain when there is a type clash on
|
Previous versions don't complain when there is a type clash on
|
||||||
the default action if the rule has a mid-rule action, such as in:
|
the default action if the rule has a midrule action, such as in:
|
||||||
|
|
||||||
%type <foo> bar
|
%type <foo> bar
|
||||||
%%
|
%%
|
||||||
|
|||||||
132
doc/bison.texi
132
doc/bison.texi
@@ -216,16 +216,16 @@ Defining Language Semantics
|
|||||||
* Structured Value Type:: Providing a structured semantic value type.
|
* Structured Value Type:: Providing a structured semantic value type.
|
||||||
* Actions:: An action is the semantic definition of a grammar rule.
|
* Actions:: An action is the semantic definition of a grammar rule.
|
||||||
* Action Types:: Specifying data types for actions to operate on.
|
* Action Types:: Specifying data types for actions to operate on.
|
||||||
* Mid-Rule Actions:: Most actions go at the end of a rule.
|
* Midrule Actions: : Most actions go at the end of a rule.
|
||||||
This says when, why and how to use the exceptional
|
This says when, why and how to use the exceptional
|
||||||
action in the middle of a rule.
|
action in the middle of a rule.
|
||||||
|
|
||||||
Actions in Mid-Rule
|
Actions in Midrule
|
||||||
|
|
||||||
* Using Mid-Rule Actions:: Putting an action in the middle of a rule.
|
* Using Midrule Actions:: Putting an action in the middle of a rule.
|
||||||
* Typed Mid-Rule Actions:: Specifying the semantic type of their values.
|
* Typed Midrule Actions:: Specifying the semantic type of their values.
|
||||||
* Mid-Rule Action Translation:: How mid-rule actions are actually processed.
|
* Midrule Action Translation:: How midrule actions are actually processed.
|
||||||
* Mid-Rule Conflicts:: Mid-rule actions can cause conflicts.
|
* Midrule Conflicts:: Midrule actions can cause conflicts.
|
||||||
|
|
||||||
Tracking Locations
|
Tracking Locations
|
||||||
|
|
||||||
@@ -1267,7 +1267,7 @@ widget:
|
|||||||
false). However, this
|
false). However, this
|
||||||
does @emph{not} have the same effect if @code{new_args} and @code{old_args}
|
does @emph{not} have the same effect if @code{new_args} and @code{old_args}
|
||||||
have overlapping syntax.
|
have overlapping syntax.
|
||||||
Since the mid-rule actions testing @code{new_syntax} are deferred,
|
Since the midrule actions testing @code{new_syntax} are deferred,
|
||||||
a GLR parser first encounters the unresolved ambiguous reduction
|
a GLR parser first encounters the unresolved ambiguous reduction
|
||||||
for cases where @code{new_args} and @code{old_args} recognize the same string
|
for cases where @code{new_args} and @code{old_args} recognize the same string
|
||||||
@emph{before} performing the tests of @code{new_syntax}. It therefore
|
@emph{before} performing the tests of @code{new_syntax}. It therefore
|
||||||
@@ -3647,7 +3647,7 @@ the numbers associated with @var{x} and @var{y}.
|
|||||||
* Structured Value Type:: Providing a structured semantic value type.
|
* Structured Value Type:: Providing a structured semantic value type.
|
||||||
* Actions:: An action is the semantic definition of a grammar rule.
|
* Actions:: An action is the semantic definition of a grammar rule.
|
||||||
* Action Types:: Specifying data types for actions to operate on.
|
* Action Types:: Specifying data types for actions to operate on.
|
||||||
* Mid-Rule Actions:: Most actions go at the end of a rule.
|
* Midrule Actions:: Most actions go at the end of a rule.
|
||||||
This says when, why and how to use the exceptional
|
This says when, why and how to use the exceptional
|
||||||
action in the middle of a rule.
|
action in the middle of a rule.
|
||||||
@end menu
|
@end menu
|
||||||
@@ -3914,8 +3914,8 @@ An action consists of braced code containing C statements, and can be
|
|||||||
placed at any position in the rule;
|
placed at any position in the rule;
|
||||||
it is executed at that position. Most rules have just one action at the
|
it is executed at that position. Most rules have just one action at the
|
||||||
end of the rule, following all the components. Actions in the middle of
|
end of the rule, following all the components. Actions in the middle of
|
||||||
a rule are tricky and used only for special purposes (@pxref{Mid-Rule
|
a rule are tricky and used only for special purposes (@pxref{Midrule
|
||||||
Actions, ,Actions in Mid-Rule}).
|
Actions, ,Actions in Midrule}).
|
||||||
|
|
||||||
The C code in an action can refer to the semantic values of the
|
The C code in an action can refer to the semantic values of the
|
||||||
components matched by the rule with the construct @code{$@var{n}},
|
components matched by the rule with the construct @code{$@var{n}},
|
||||||
@@ -4058,43 +4058,43 @@ reference. For example, if you have defined types as shown here:
|
|||||||
then you can write @code{$<itype>1} to refer to the first subunit of the
|
then you can write @code{$<itype>1} to refer to the first subunit of the
|
||||||
rule as an integer, or @code{$<dtype>1} to refer to it as a double.
|
rule as an integer, or @code{$<dtype>1} to refer to it as a double.
|
||||||
|
|
||||||
@node Mid-Rule Actions
|
@node Midrule Actions
|
||||||
@subsection Actions in Mid-Rule
|
@subsection Actions in Midrule
|
||||||
@cindex actions in mid-rule
|
@cindex actions in midrule
|
||||||
@cindex mid-rule actions
|
@cindex midrule actions
|
||||||
|
|
||||||
Occasionally it is useful to put an action in the middle of a rule.
|
Occasionally it is useful to put an action in the middle of a rule.
|
||||||
These actions are written just like usual end-of-rule actions, but they
|
These actions are written just like usual end-of-rule actions, but they
|
||||||
are executed before the parser even recognizes the following components.
|
are executed before the parser even recognizes the following components.
|
||||||
|
|
||||||
@menu
|
@menu
|
||||||
* Using Mid-Rule Actions:: Putting an action in the middle of a rule.
|
* Using Midrule Actions:: Putting an action in the middle of a rule.
|
||||||
* Typed Mid-Rule Actions:: Specifying the semantic type of their values.
|
* Typed Midrule Actions:: Specifying the semantic type of their values.
|
||||||
* Mid-Rule Action Translation:: How mid-rule actions are actually processed.
|
* Midrule Action Translation:: How midrule actions are actually processed.
|
||||||
* Mid-Rule Conflicts:: Mid-rule actions can cause conflicts.
|
* Midrule Conflicts:: Midrule actions can cause conflicts.
|
||||||
@end menu
|
@end menu
|
||||||
|
|
||||||
@node Using Mid-Rule Actions
|
@node Using Midrule Actions
|
||||||
@subsubsection Using Mid-Rule Actions
|
@subsubsection Using Midrule Actions
|
||||||
|
|
||||||
A mid-rule action may refer to the components preceding it using
|
A midrule action may refer to the components preceding it using
|
||||||
@code{$@var{n}}, but it may not refer to subsequent components because
|
@code{$@var{n}}, but it may not refer to subsequent components because
|
||||||
it is run before they are parsed.
|
it is run before they are parsed.
|
||||||
|
|
||||||
The mid-rule action itself counts as one of the components of the rule.
|
The midrule action itself counts as one of the components of the rule.
|
||||||
This makes a difference when there is another action later in the same rule
|
This makes a difference when there is another action later in the same rule
|
||||||
(and usually there is another at the end): you have to count the actions
|
(and usually there is another at the end): you have to count the actions
|
||||||
along with the symbols when working out which number @var{n} to use in
|
along with the symbols when working out which number @var{n} to use in
|
||||||
@code{$@var{n}}.
|
@code{$@var{n}}.
|
||||||
|
|
||||||
The mid-rule action can also have a semantic value. The action can set
|
The midrule action can also have a semantic value. The action can set
|
||||||
its value with an assignment to @code{$$}, and actions later in the rule
|
its value with an assignment to @code{$$}, and actions later in the rule
|
||||||
can refer to the value using @code{$@var{n}}. Since there is no symbol
|
can refer to the value using @code{$@var{n}}. Since there is no symbol
|
||||||
to name the action, there is no way to declare a data type for the value
|
to name the action, there is no way to declare a data type for the value
|
||||||
in advance, so you must use the @samp{$<@dots{}>@var{n}} construct to
|
in advance, so you must use the @samp{$<@dots{}>@var{n}} construct to
|
||||||
specify a data type each time you refer to this value.
|
specify a data type each time you refer to this value.
|
||||||
|
|
||||||
There is no way to set the value of the entire rule with a mid-rule
|
There is no way to set the value of the entire rule with a midrule
|
||||||
action, because assignments to @code{$$} do not have that effect. The
|
action, because assignments to @code{$$} do not have that effect. The
|
||||||
only way to set the value for the entire rule is with an ordinary action
|
only way to set the value for the entire rule is with an ordinary action
|
||||||
at the end of the rule.
|
at the end of the rule.
|
||||||
@@ -4131,7 +4131,7 @@ list of accessible variables) as its semantic value, using alternative
|
|||||||
first action is finished, the embedded statement @code{stmt} can be
|
first action is finished, the embedded statement @code{stmt} can be
|
||||||
parsed.
|
parsed.
|
||||||
|
|
||||||
Note that the mid-rule action is component number 5, so the @samp{stmt} is
|
Note that the midrule action is component number 5, so the @samp{stmt} is
|
||||||
component number 6. Named references can be used to improve the readability
|
component number 6. Named references can be used to improve the readability
|
||||||
and maintainability (@pxref{Named References}):
|
and maintainability (@pxref{Named References}):
|
||||||
|
|
||||||
@@ -4157,23 +4157,23 @@ earlier action is used to restore the prior list of variables. This
|
|||||||
removes the temporary @code{let}-variable from the list so that it won't
|
removes the temporary @code{let}-variable from the list so that it won't
|
||||||
appear to exist while the rest of the program is parsed.
|
appear to exist while the rest of the program is parsed.
|
||||||
|
|
||||||
Because the types of the semantic values of mid-rule actions are unknown to
|
Because the types of the semantic values of midrule actions are unknown to
|
||||||
Bison, type-based features (e.g., @samp{%printer}, @samp{%destructor}) do
|
Bison, type-based features (e.g., @samp{%printer}, @samp{%destructor}) do
|
||||||
not work, which could result in memory leaks. They also forbid the use of
|
not work, which could result in memory leaks. They also forbid the use of
|
||||||
the @code{variant} implementation of the @code{api.value.type} in C++
|
the @code{variant} implementation of the @code{api.value.type} in C++
|
||||||
(@pxref{C++ Variants}).
|
(@pxref{C++ Variants}).
|
||||||
|
|
||||||
@xref{Typed Mid-Rule Actions}, for one way to address this issue, and
|
@xref{Typed Midrule Actions}, for one way to address this issue, and
|
||||||
@ref{Mid-Rule Action Translation}, for another: turning mid-action actions
|
@ref{Midrule Action Translation}, for another: turning mid-action actions
|
||||||
into regular actions.
|
into regular actions.
|
||||||
|
|
||||||
|
|
||||||
@node Typed Mid-Rule Actions
|
@node Typed Midrule Actions
|
||||||
@subsubsection Typed Mid-Rule Actions
|
@subsubsection Typed Midrule Actions
|
||||||
|
|
||||||
@findex %destructor
|
@findex %destructor
|
||||||
@cindex discarded symbols, mid-rule actions
|
@cindex discarded symbols, midrule actions
|
||||||
@cindex error recovery, mid-rule actions
|
@cindex error recovery, midrule actions
|
||||||
In the above example, if the parser initiates error recovery (@pxref{Error
|
In the above example, if the parser initiates error recovery (@pxref{Error
|
||||||
Recovery}) while parsing the tokens in the embedded statement @code{stmt},
|
Recovery}) while parsing the tokens in the embedded statement @code{stmt},
|
||||||
it might discard the previous semantic context @code{$<context>5} without
|
it might discard the previous semantic context @code{$<context>5} without
|
||||||
@@ -4181,11 +4181,11 @@ restoring it. Thus, @code{$<context>5} needs a destructor
|
|||||||
(@pxref{Destructor Decl, , Freeing Discarded Symbols}), and Bison needs the
|
(@pxref{Destructor Decl, , Freeing Discarded Symbols}), and Bison needs the
|
||||||
type of the semantic value (@code{context}) to select the right destructor.
|
type of the semantic value (@code{context}) to select the right destructor.
|
||||||
|
|
||||||
As an extension to Yacc's mid-rule actions, Bison offers a means to type
|
As an extension to Yacc's midrule actions, Bison offers a means to type
|
||||||
their semantic value: specify its type tag (@samp{<...>} before the mid-rule
|
their semantic value: specify its type tag (@samp{<...>} before the midrule
|
||||||
action.
|
action.
|
||||||
|
|
||||||
Consider the previous example, with an untyped mid-rule action:
|
Consider the previous example, with an untyped midrule action:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
@group
|
@group
|
||||||
@@ -4228,12 +4228,12 @@ C++ @code{variant}s can be used, and redundancy is reduced (@code{<context>}
|
|||||||
is specified once).
|
is specified once).
|
||||||
|
|
||||||
|
|
||||||
@node Mid-Rule Action Translation
|
@node Midrule Action Translation
|
||||||
@subsubsection Mid-Rule Action Translation
|
@subsubsection Midrule Action Translation
|
||||||
@vindex $@@@var{n}
|
@vindex $@@@var{n}
|
||||||
@vindex @@@var{n}
|
@vindex @@@var{n}
|
||||||
|
|
||||||
Mid-rule actions are actually transformed into regular rules and actions.
|
Midrule actions are actually transformed into regular rules and actions.
|
||||||
The various reports generated by Bison (textual, graphical, etc., see
|
The various reports generated by Bison (textual, graphical, etc., see
|
||||||
@ref{Understanding, , Understanding Your Parser}) reveal this translation,
|
@ref{Understanding, , Understanding Your Parser}) reveal this translation,
|
||||||
best explained by means of an example. The following rule:
|
best explained by means of an example. The following rule:
|
||||||
@@ -4255,8 +4255,8 @@ exp: $@@1 "b" $@@2 $@@3 "e" @{ f(); @};
|
|||||||
@noindent
|
@noindent
|
||||||
with new nonterminal symbols @code{$@@@var{n}}, where @var{n} is a number.
|
with new nonterminal symbols @code{$@@@var{n}}, where @var{n} is a number.
|
||||||
|
|
||||||
A mid-rule action is expected to generate a value if it uses @code{$$}, or
|
A midrule action is expected to generate a value if it uses @code{$$}, or
|
||||||
the (final) action uses @code{$@var{n}} where @var{n} denote the mid-rule
|
the (final) action uses @code{$@var{n}} where @var{n} denote the midrule
|
||||||
action. In that case its nonterminal is rather named @code{@@@var{n}}:
|
action. In that case its nonterminal is rather named @code{@@@var{n}}:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
@@ -4273,7 +4273,7 @@ $@@3: %empty @{ d(); @};
|
|||||||
exp: @@1 "b" @@2 $@@3 "e" @{ f = $1; @}
|
exp: @@1 "b" @@2 $@@3 "e" @{ f = $1; @}
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
There are probably two errors in the above example: the first mid-rule
|
There are probably two errors in the above example: the first midrule
|
||||||
action does not generate a value (it does not use @code{$$} although the
|
action does not generate a value (it does not use @code{$$} although the
|
||||||
final action uses it), and the value of the second one is not used (the
|
final action uses it), and the value of the second one is not used (the
|
||||||
final action does not use @code{$3}). Bison reports these errors when the
|
final action does not use @code{$3}). Bison reports these errors when the
|
||||||
@@ -4296,9 +4296,9 @@ mid.y:2.19-31: warning: unused value: $3
|
|||||||
|
|
||||||
@sp 1
|
@sp 1
|
||||||
|
|
||||||
It is sometimes useful to turn mid-rule actions into regular actions, e.g.,
|
It is sometimes useful to turn midrule actions into regular actions, e.g.,
|
||||||
to factor them, or to escape from their limitations. For instance, as an
|
to factor them, or to escape from their limitations. For instance, as an
|
||||||
alternative to @emph{typed} mid-rule action, you may bury the mid-rule
|
alternative to @emph{typed} midrule action, you may bury the midrule
|
||||||
action inside a nonterminal symbol and to declare a printer and a destructor
|
action inside a nonterminal symbol and to declare a printer and a destructor
|
||||||
for that symbol:
|
for that symbol:
|
||||||
|
|
||||||
@@ -4334,11 +4334,11 @@ let:
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
@node Mid-Rule Conflicts
|
@node Midrule Conflicts
|
||||||
@subsubsection Conflicts due to Mid-Rule Actions
|
@subsubsection Conflicts due to Midrule Actions
|
||||||
Taking action before a rule is completely recognized often leads to
|
Taking action before a rule is completely recognized often leads to
|
||||||
conflicts since the parser must commit to a parse in order to execute the
|
conflicts since the parser must commit to a parse in order to execute the
|
||||||
action. For example, the following two rules, without mid-rule actions,
|
action. For example, the following two rules, without midrule actions,
|
||||||
can coexist in a working parser because the parser can shift the open-brace
|
can coexist in a working parser because the parser can shift the open-brace
|
||||||
token and look at what follows before deciding whether there is a
|
token and look at what follows before deciding whether there is a
|
||||||
declaration or not:
|
declaration or not:
|
||||||
@@ -4353,7 +4353,7 @@ compound:
|
|||||||
@end example
|
@end example
|
||||||
|
|
||||||
@noindent
|
@noindent
|
||||||
But when we add a mid-rule action as follows, the rules become nonfunctional:
|
But when we add a midrule action as follows, the rules become nonfunctional:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
@group
|
@group
|
||||||
@@ -4368,7 +4368,7 @@ compound:
|
|||||||
@end example
|
@end example
|
||||||
|
|
||||||
@noindent
|
@noindent
|
||||||
Now the parser is forced to decide whether to run the mid-rule action
|
Now the parser is forced to decide whether to run the midrule action
|
||||||
when it has read no farther than the open-brace. In other words, it
|
when it has read no farther than the open-brace. In other words, it
|
||||||
must commit to using one rule or the other, without sufficient
|
must commit to using one rule or the other, without sufficient
|
||||||
information to do it correctly. (The open-brace token is what is called
|
information to do it correctly. (The open-brace token is what is called
|
||||||
@@ -4704,9 +4704,9 @@ exp[result]: exp[left] '/' exp[right]
|
|||||||
@end example
|
@end example
|
||||||
|
|
||||||
@noindent
|
@noindent
|
||||||
In order to access a semantic value generated by a mid-rule action, an
|
In order to access a semantic value generated by a midrule action, an
|
||||||
explicit name may also be declared by putting a bracketed name after the
|
explicit name may also be declared by putting a bracketed name after the
|
||||||
closing brace of the mid-rule action code:
|
closing brace of the midrule action code:
|
||||||
@example
|
@example
|
||||||
@group
|
@group
|
||||||
exp[res]: exp[x] '+' @{$left = $x;@}[left] exp[right]
|
exp[res]: exp[x] '+' @{$left = $x;@}[left] exp[right]
|
||||||
@@ -5091,16 +5091,16 @@ redefine it from @code{$end} to, for example, @code{END}:
|
|||||||
%token END 0
|
%token END 0
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
@cindex actions in mid-rule
|
@cindex actions in midrule
|
||||||
@cindex mid-rule actions
|
@cindex midrule actions
|
||||||
Finally, Bison will never invoke a @code{%destructor} for an unreferenced
|
Finally, Bison will never invoke a @code{%destructor} for an unreferenced
|
||||||
mid-rule semantic value (@pxref{Mid-Rule Actions,,Actions in Mid-Rule}).
|
midrule semantic value (@pxref{Midrule Actions,,Actions in Midrule}).
|
||||||
That is, Bison does not consider a mid-rule to have a semantic value if you
|
That is, Bison does not consider a midrule to have a semantic value if you
|
||||||
do not reference @code{$$} in the mid-rule's action or @code{$@var{n}}
|
do not reference @code{$$} in the midrule's action or @code{$@var{n}}
|
||||||
(where @var{n} is the right-hand side symbol position of the mid-rule) in
|
(where @var{n} is the right-hand side symbol position of the midrule) in
|
||||||
any later action in that rule. However, if you do reference either, the
|
any later action in that rule. However, if you do reference either, the
|
||||||
Bison-generated parser will invoke the @code{<>} @code{%destructor} whenever
|
Bison-generated parser will invoke the @code{<>} @code{%destructor} whenever
|
||||||
it discards the mid-rule symbol.
|
it discards the midrule symbol.
|
||||||
|
|
||||||
@ignore
|
@ignore
|
||||||
@noindent
|
@noindent
|
||||||
@@ -10028,7 +10028,7 @@ Output warnings falling in @var{category}. @var{category} can be one
|
|||||||
of:
|
of:
|
||||||
@table @code
|
@table @code
|
||||||
@item midrule-values
|
@item midrule-values
|
||||||
Warn about mid-rule values that are set but not used within any of the actions
|
Warn about midrule values that are set but not used within any of the actions
|
||||||
of the parent rule.
|
of the parent rule.
|
||||||
For example, warn about unused @code{$2} in:
|
For example, warn about unused @code{$2} in:
|
||||||
|
|
||||||
@@ -10036,8 +10036,8 @@ For example, warn about unused @code{$2} in:
|
|||||||
exp: '1' @{ $$ = 1; @} '+' exp @{ $$ = $1 + $4; @};
|
exp: '1' @{ $$ = 1; @} '+' exp @{ $$ = $1 + $4; @};
|
||||||
@end example
|
@end example
|
||||||
|
|
||||||
Also warn about mid-rule values that are used but not set.
|
Also warn about midrule values that are used but not set.
|
||||||
For example, warn about unset @code{$$} in the mid-rule action in:
|
For example, warn about unset @code{$$} in the midrule action in:
|
||||||
|
|
||||||
@example
|
@example
|
||||||
exp: '1' @{ $1 = 1; @} '+' exp @{ $$ = $2 + $4; @};
|
exp: '1' @{ $1 = 1; @} '+' exp @{ $$ = $2 + $4; @};
|
||||||
@@ -10608,8 +10608,8 @@ dirty game with @code{yylval}, say storing an @code{int}, reading a
|
|||||||
possible with variants: they must be initialized, then assigned to, and
|
possible with variants: they must be initialized, then assigned to, and
|
||||||
eventually, destroyed. As a matter of fact, Bison variants forbid the use
|
eventually, destroyed. As a matter of fact, Bison variants forbid the use
|
||||||
of alternative types such as @samp{$<int>2} or @samp{$<std::string>$}, even
|
of alternative types such as @samp{$<int>2} or @samp{$<std::string>$}, even
|
||||||
in mid-rule actions. It is mandatory to use typed mid-rule actions
|
in midrule actions. It is mandatory to use typed midrule actions
|
||||||
(@pxref{Typed Mid-Rule Actions}).
|
(@pxref{Typed Midrule Actions}).
|
||||||
|
|
||||||
@deftypemethod {semantic_type} {T&} build<T> ()
|
@deftypemethod {semantic_type} {T&} build<T> ()
|
||||||
Initialize, but leave empty. Returns the address where the actual value may
|
Initialize, but leave empty. Returns the address where the actual value may
|
||||||
@@ -12714,8 +12714,8 @@ In an action, the location of the left-hand side of the rule.
|
|||||||
In an action, the location of the @var{n}-th symbol of the right-hand side
|
In an action, the location of the @var{n}-th symbol of the right-hand side
|
||||||
of the rule. @xref{Tracking Locations}.
|
of the rule. @xref{Tracking Locations}.
|
||||||
|
|
||||||
In a grammar, the Bison-generated nonterminal symbol for a mid-rule action
|
In a grammar, the Bison-generated nonterminal symbol for a midrule action
|
||||||
with a semantical value. @xref{Mid-Rule Action Translation}.
|
with a semantical value. @xref{Midrule Action Translation}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Variable} @@@var{name}
|
@deffn {Variable} @@@var{name}
|
||||||
@@ -12725,8 +12725,8 @@ In an action, the location of a symbol addressed by @var{name}.
|
|||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Symbol} $@@@var{n}
|
@deffn {Symbol} $@@@var{n}
|
||||||
In a grammar, the Bison-generated nonterminal symbol for a mid-rule action
|
In a grammar, the Bison-generated nonterminal symbol for a midrule action
|
||||||
with no semantical value. @xref{Mid-Rule Action Translation}.
|
with no semantical value. @xref{Midrule Action Translation}.
|
||||||
@end deffn
|
@end deffn
|
||||||
|
|
||||||
@deffn {Variable} $$
|
@deffn {Variable} $$
|
||||||
|
|||||||
10
src/reader.c
10
src/reader.c
@@ -253,8 +253,8 @@ grammar_current_rule_begin (symbol *lhs, location loc,
|
|||||||
/*----------------------------------------------------------------------.
|
/*----------------------------------------------------------------------.
|
||||||
| A symbol should be used if either: |
|
| A symbol should be used if either: |
|
||||||
| 1. It has a destructor. |
|
| 1. It has a destructor. |
|
||||||
| 2. The symbol is a mid-rule symbol (i.e., the generated LHS |
|
| 2. The symbol is a midrule symbol (i.e., the generated LHS |
|
||||||
| replacing a mid-rule action) that was assigned to or used, as in |
|
| replacing a midrule action) that was assigned to or used, as in |
|
||||||
| "exp: { $$ = 1; } { $$ = $1; }". |
|
| "exp: { $$ = 1; } { $$ = $1; }". |
|
||||||
`----------------------------------------------------------------------*/
|
`----------------------------------------------------------------------*/
|
||||||
|
|
||||||
@@ -373,9 +373,9 @@ grammar_current_rule_end (location loc)
|
|||||||
|
|
||||||
|
|
||||||
/*-------------------------------------------------------------------.
|
/*-------------------------------------------------------------------.
|
||||||
| The previous action turns out to be a mid-rule action. Attach it |
|
| The previous action turns out to be a midrule action. Attach it |
|
||||||
| to the current rule, i.e., create a dummy symbol, attach it this |
|
| to the current rule, i.e., create a dummy symbol, attach it this |
|
||||||
| mid-rule action, and append this dummy nonterminal to the current |
|
| midrule action, and append this dummy nonterminal to the current |
|
||||||
| rule. |
|
| rule. |
|
||||||
`-------------------------------------------------------------------*/
|
`-------------------------------------------------------------------*/
|
||||||
|
|
||||||
@@ -512,7 +512,7 @@ grammar_current_rule_merge_set (uniqstr name, location loc)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* Attach SYM to the current rule. If needed, move the previous
|
/* Attach SYM to the current rule. If needed, move the previous
|
||||||
action as a mid-rule action. */
|
action as a midrule action. */
|
||||||
|
|
||||||
void
|
void
|
||||||
grammar_current_rule_symbol_append (symbol *sym, location loc,
|
grammar_current_rule_symbol_append (symbol *sym, location loc,
|
||||||
|
|||||||
@@ -85,7 +85,7 @@ typedef struct code_props {
|
|||||||
/** Named reference. */
|
/** Named reference. */
|
||||||
named_ref *named_ref;
|
named_ref *named_ref;
|
||||||
|
|
||||||
/** Type, for mid-rule actions. */
|
/** Type, for midrule actions. */
|
||||||
uniqstr type;
|
uniqstr type;
|
||||||
} code_props;
|
} code_props;
|
||||||
|
|
||||||
|
|||||||
@@ -379,7 +379,7 @@ show_sub_message (warnings warning,
|
|||||||
|
|
||||||
if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
|
if (var->err & VARIANT_NOT_VISIBLE_FROM_MIDRULE)
|
||||||
obstack_printf (&msg_buf,
|
obstack_printf (&msg_buf,
|
||||||
_(", cannot be accessed from mid-rule action at $%d"),
|
_(", cannot be accessed from midrule action at $%d"),
|
||||||
midrule_rhs_index);
|
midrule_rhs_index);
|
||||||
|
|
||||||
complain_indent (&id_loc, warning, &indent, "%s",
|
complain_indent (&id_loc, warning, &indent, "%s",
|
||||||
@@ -494,7 +494,7 @@ parse_ref (char *cp, symbol_list *rule, int rule_length,
|
|||||||
variant *var = &variant_table[i];
|
variant *var = &variant_table[i];
|
||||||
unsigned symbol_index = var->symbol_index;
|
unsigned symbol_index = var->symbol_index;
|
||||||
|
|
||||||
/* Check visibility from mid-rule actions. */
|
/* Check visibility from midrule actions. */
|
||||||
if (midrule_rhs_index != 0
|
if (midrule_rhs_index != 0
|
||||||
&& (symbol_index == 0 || midrule_rhs_index < symbol_index))
|
&& (symbol_index == 0 || midrule_rhs_index < symbol_index))
|
||||||
var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
|
var->err |= VARIANT_NOT_VISIBLE_FROM_MIDRULE;
|
||||||
|
|||||||
@@ -17,14 +17,14 @@
|
|||||||
|
|
||||||
AT_BANNER([[User Actions.]])
|
AT_BANNER([[User Actions.]])
|
||||||
|
|
||||||
## ------------------ ##
|
## ----------------- ##
|
||||||
## Mid-rule actions. ##
|
## Midrule actions. ##
|
||||||
## ------------------ ##
|
## ----------------- ##
|
||||||
|
|
||||||
AT_SETUP([Mid-rule actions])
|
AT_SETUP([Midrule actions])
|
||||||
|
|
||||||
# Bison once forgot the mid-rule actions. It was because the action
|
# Bison once forgot the midrule actions. It was because the action
|
||||||
# was attached to the host rule (the one with the mid-rule action),
|
# was attached to the host rule (the one with the midrule action),
|
||||||
# instead of being attached to the empty rule dedicated to this
|
# instead of being attached to the empty rule dedicated to this
|
||||||
# action.
|
# action.
|
||||||
|
|
||||||
@@ -64,11 +64,11 @@ AT_PARSER_CHECK([./input], 0,
|
|||||||
|
|
||||||
AT_CLEANUP
|
AT_CLEANUP
|
||||||
|
|
||||||
## ------------------------ ##
|
## ----------------------- ##
|
||||||
## Typed mid-rule actions. ##
|
## Typed midrule actions. ##
|
||||||
## ------------------------ ##
|
## ----------------------- ##
|
||||||
|
|
||||||
AT_SETUP([Typed mid-rule actions])
|
AT_SETUP([Typed midrule actions])
|
||||||
|
|
||||||
AT_BISON_OPTION_PUSHDEFS
|
AT_BISON_OPTION_PUSHDEFS
|
||||||
AT_DATA_GRAMMAR([[input.y]],
|
AT_DATA_GRAMMAR([[input.y]],
|
||||||
@@ -123,7 +123,7 @@ AT_DATA_GRAMMAR([[1.y]],
|
|||||||
[[%%
|
[[%%
|
||||||
exp: a b;
|
exp: a b;
|
||||||
a: /* empty. */ {};
|
a: /* empty. */ {};
|
||||||
// A mid-rule action does not count as an empty rule.
|
// A midrule action does not count as an empty rule.
|
||||||
b: {} {};
|
b: {} {};
|
||||||
]])
|
]])
|
||||||
|
|
||||||
@@ -1463,11 +1463,11 @@ AT_CLEANUP
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
## ------------------------------------------------------ ##
|
## ----------------------------------------------------- ##
|
||||||
## Default %printer and %destructor for mid-rule values. ##
|
## Default %printer and %destructor for midrule values. ##
|
||||||
## ------------------------------------------------------ ##
|
## ----------------------------------------------------- ##
|
||||||
|
|
||||||
AT_SETUP([Default %printer and %destructor for mid-rule values])
|
AT_SETUP([Default %printer and %destructor for midrule values])
|
||||||
|
|
||||||
AT_BISON_OPTION_PUSHDEFS([%debug])
|
AT_BISON_OPTION_PUSHDEFS([%debug])
|
||||||
AT_DATA_GRAMMAR([[input.y]],
|
AT_DATA_GRAMMAR([[input.y]],
|
||||||
|
|||||||
10
tests/c++.at
10
tests/c++.at
@@ -409,15 +409,15 @@ m4_popdef([AT_TEST])
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
## ------------------------------------- ##
|
## ------------------------------------ ##
|
||||||
## Variants and Typed Mid-rule Actions. ##
|
## Variants and Typed Midrule Actions. ##
|
||||||
## ------------------------------------- ##
|
## ------------------------------------ ##
|
||||||
|
|
||||||
AT_SETUP([Variants and Typed Mid-rule Actions])
|
AT_SETUP([Variants and Typed Midrule Actions])
|
||||||
|
|
||||||
# See http://lists.gnu.org/archive/html/bug-bison/2017-06/msg00000.html.
|
# See http://lists.gnu.org/archive/html/bug-bison/2017-06/msg00000.html.
|
||||||
#
|
#
|
||||||
# Check that typed mid-rule actions behave properly (pre-construction
|
# Check that typed midrule actions behave properly (pre-construction
|
||||||
# of $$ before the user action, support of %printer and %destructor,
|
# of $$ before the user action, support of %printer and %destructor,
|
||||||
# etc.).
|
# etc.).
|
||||||
|
|
||||||
|
|||||||
@@ -71,7 +71,7 @@ m4_popdef([AT_LALR1_DIFF_CHECK])
|
|||||||
## ----------------------- ##
|
## ----------------------- ##
|
||||||
|
|
||||||
# We have been careful to strip all the actions excepts the
|
# We have been careful to strip all the actions excepts the
|
||||||
# mid-rule actions.
|
# midrule actions.
|
||||||
#
|
#
|
||||||
# There are 65 SR conflicts. Bison was once wrong, due to an incorrect
|
# There are 65 SR conflicts. Bison was once wrong, due to an incorrect
|
||||||
# computation of nullable. It reported 485 SR conflicts!
|
# computation of nullable. It reported 485 SR conflicts!
|
||||||
|
|||||||
Reference in New Issue
Block a user