X-Git-Url: https://git.saurik.com/bison.git/blobdiff_plain/6f8bdce25df5669b0b200c2a3848a1c08a44eb79..7d31f0928907ccdd647479075d385b5a36c62611:/doc/bison.texinfo diff --git a/doc/bison.texinfo b/doc/bison.texinfo index e0d5aa5b..42ea8e28 100644 --- a/doc/bison.texinfo +++ b/doc/bison.texinfo @@ -186,6 +186,7 @@ Bison Grammar Files * Recursion:: Writing recursive rules. * Semantics:: Semantic values and actions. * Locations:: Locations and actions. +* Named References:: Using named references in actions. * Declarations:: All kinds of Bison declarations are described here. * Multiple Parsers:: Putting more than one Bison parser in one program. @@ -206,7 +207,6 @@ Defining Language Semantics * Mid-Rule Actions:: Most actions go at the end of a rule. This says when, why and how to use the exceptional action in the middle of a rule. -* Named References:: Using named references in actions. Tracking Locations @@ -2627,6 +2627,7 @@ The Bison grammar file conventionally has a name ending in @samp{.y}. * Recursion:: Writing recursive rules. * Semantics:: Semantic values and actions. * Locations:: Locations and actions. +* Named References:: Using named references in actions. * Declarations:: All kinds of Bison declarations are described here. * Multiple Parsers:: Putting more than one Bison parser in one program. @end menu @@ -3388,7 +3389,6 @@ the numbers associated with @var{x} and @var{y}. * Mid-Rule Actions:: Most actions go at the end of a rule. This says when, why and how to use the exceptional action in the middle of a rule. -* Named References:: Using named references in actions. @end menu @node Value Type @@ -3806,93 +3806,6 @@ compound: subroutine Now Bison can execute the action in the rule for @code{subroutine} without deciding which rule for @code{compound} it will eventually use. -@node Named References -@subsection Using Named References -@cindex named references - -While every semantic value can be accessed with positional references -@code{$@var{n}} and @code{$$}, it's often much more convenient to refer to -them by name. First of all, original symbol names may be used as named -references. For example: - -@example -@group -invocation: op '(' args ')' - @{ $invocation = new_invocation ($op, $args, @@invocation); @} -@end group -@end example - -@noindent -The positional @code{$$}, @code{@@$}, @code{$n}, and @code{@@n} can be -mixed with @code{$name} and @code{@@name} arbitrarily. For example: - -@example -@group -invocation: op '(' args ')' - @{ $$ = new_invocation ($op, $args, @@$); @} -@end group -@end example - -@noindent -However, sometimes regular symbol names are not sufficient due to -ambiguities: - -@example -@group -exp: exp '/' exp - @{ $exp = $exp / $exp; @} // $exp is ambiguous. - -exp: exp '/' exp - @{ $$ = $1 / $exp; @} // One usage is ambiguous. - -exp: exp '/' exp - @{ $$ = $1 / $3; @} // No error. -@end group -@end example - -@noindent -When ambiguity occurs, explicitly declared names may be used for values and -locations. Explicit names are declared as a bracketed name after a symbol -appearance in rule definitions. For example: -@example -@group -exp[result]: exp[left] '/' exp[right] - @{ $result = $left / $right; @} -@end group -@end example - -@noindent -Explicit names may be declared for RHS and for LHS symbols as well. In order -to access a semantic value generated by a mid-rule action, an explicit name -may also be declared by putting a bracketed name after the closing brace of -the mid-rule action code: -@example -@group -exp[res]: exp[x] '+' @{$left = $x;@}[left] exp[right] - @{ $res = $left + $right; @} -@end group -@end example - -@noindent - -In references, in order to specify names containing dots and dashes, an explicit -bracketed syntax @code{$[name]} and @code{@@[name]} must be used: -@example -@group -if-stmt: IF '(' expr ')' THEN then.stmt ';' - @{ $[if-stmt] = new_if_stmt ($expr, $[then.stmt]); @} -@end group -@end example - -It often happens that named references are followed by a dot, dash or other -C punctuation marks and operators. By default, Bison will read -@code{$name.suffix} as a reference to symbol value @code{$name} followed by -@samp{.suffix}, i.e., an access to the @samp{suffix} field of the semantic -value. In order to force Bison to recognize @code{name.suffix} in its entirety -as the name of a semantic value, bracketed syntax @code{$[name.suffix]} -must be used. - - @node Locations @section Tracking Locations @cindex location @@ -4101,6 +4014,97 @@ macro should expand to something that can be used as a single statement when it is followed by a semicolon. @end itemize +@node Named References +@section Using Named References +@cindex named references + +As described in the preceding sections, the traditional way to refer to any +semantic value or location is a @dfn{positional reference}, which takes the +form @code{$@var{n}}, @code{$$}, @code{@@@var{n}}, and @code{@@$}. However, +such a reference is not very descriptive. Moreover, if you later decide to +insert or remove symbols in the right-hand side of a grammar rule, the need +to renumber such references can be tedious and error-prone. + +To avoid these issues, you can also refer to a semantic value or location +using a @dfn{named reference}. First of all, original symbol names may be +used as named references. For example: + +@example +@group +invocation: op '(' args ')' + @{ $invocation = new_invocation ($op, $args, @@invocation); @} +@end group +@end example + +@noindent +Positional and named references can be mixed arbitrarily. For example: + +@example +@group +invocation: op '(' args ')' + @{ $$ = new_invocation ($op, $args, @@$); @} +@end group +@end example + +@noindent +However, sometimes regular symbol names are not sufficient due to +ambiguities: + +@example +@group +exp: exp '/' exp + @{ $exp = $exp / $exp; @} // $exp is ambiguous. + +exp: exp '/' exp + @{ $$ = $1 / $exp; @} // One usage is ambiguous. + +exp: exp '/' exp + @{ $$ = $1 / $3; @} // No error. +@end group +@end example + +@noindent +When ambiguity occurs, explicitly declared names may be used for values and +locations. Explicit names are declared as a bracketed name after a symbol +appearance in rule definitions. For example: +@example +@group +exp[result]: exp[left] '/' exp[right] + @{ $result = $left / $right; @} +@end group +@end example + +@noindent +Explicit names may be declared for RHS and for LHS symbols as well. In order +to access a semantic value generated by a mid-rule action, an explicit name +may also be declared by putting a bracketed name after the closing brace of +the mid-rule action code: +@example +@group +exp[res]: exp[x] '+' @{$left = $x;@}[left] exp[right] + @{ $res = $left + $right; @} +@end group +@end example + +@noindent + +In references, in order to specify names containing dots and dashes, an explicit +bracketed syntax @code{$[name]} and @code{@@[name]} must be used: +@example +@group +if-stmt: IF '(' expr ')' THEN then.stmt ';' + @{ $[if-stmt] = new_if_stmt ($expr, $[then.stmt]); @} +@end group +@end example + +It often happens that named references are followed by a dot, dash or other +C punctuation marks and operators. By default, Bison will read +@code{$name.suffix} as a reference to symbol value @code{$name} followed by +@samp{.suffix}, i.e., an access to the @samp{suffix} field of the semantic +value. In order to force Bison to recognize @code{name.suffix} in its entirety +as the name of a semantic value, bracketed syntax @code{$[name.suffix]} +must be used. + @node Declarations @section Bison Declarations @cindex declarations, Bison