Actions
Previous: <Multiple Types=>MultipleTy> * Next: <Action Types=>ActionType> * Up: <Semantics=>Semantics>

#Wrap on
{fH4}Actions{f}

An action accompanies a syntactic rule and contains C code to be executed
each time an instance of that rule is recognized.  The task of most actions
is to compute a semantic value for the grouping built by the rule from the
semantic values associated with tokens or smaller groupings.

An action consists of C statements surrounded by braces, much like a
compound statement in C.  It can be placed at any position in the rule; 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 a rule
are tricky and used only for special purposes (\*Note <Mid-Rule Actions=>MidRuleAct>: Actions in Mid-Rule).

The C code in an action can refer to the semantic values of the components
matched by the rule with the construct {fCode}${fStrong}n{f}{f}, which stands for
the value of the {fStrong}n{f}th component.  The semantic value for the grouping
being constructed is {fCode}$${f}.  (Bison translates both of these constructs
into array element references when it copies the actions into the parser
file.)

Here is a typical example:

#Wrap off
#fCode
exp:    
        | exp '+' exp
            \{ $$ = $1 + $3; \}
#f
#Wrap on

This rule constructs an {fCode}exp{f} from two smaller {fCode}exp{f} groupings
connected by a plus-sign token.  In the action, {fCode}$1{f} and {fCode}$3{f}
refer to the semantic values of the two component {fCode}exp{f} groupings,
which are the first and third symbols on the right hand side of the rule.
The sum is stored into {fCode}$${f} so that it becomes the semantic value of
the addition-expression just recognized by the rule.  If there were a
useful semantic value associated with the {fEmphasis}+{f} token, it could be
referred to as {fCode}$2{f}.

If you don't specify an action for a rule, Bison supplies a default:
{fCode}$$ = $1{f}.  Thus, the value of the first symbol in the rule becomes
the value of the whole rule.  Of course, the default rule is valid only
if the two data types match.  There is no meaningful default action for
an empty rule; every empty rule must have an explicit action unless the
rule's value does not matter.

{fCode}${fStrong}n{f}{f} with {fStrong}n{f} zero or negative is allowed for reference
to tokens and groupings on the stack {fEmphasis}before{f} those that match the
current rule.  This is a very risky practice, and to use it reliably
you must be certain of the context in which the rule is applied.  Here
is a case in which you can use this reliably:

#Wrap off
#fCode
foo:      expr bar '+' expr  \{  \}
        | expr bar '-' expr  \{  \}
        ;

bar:      \/\* empty \*\/
        \{ previous\_expr = $0; \}
        ;
#f
#Wrap on

As long as {fCode}bar{f} is used only in the fashion shown here, {fCode}$0{f}
always refers to the {fCode}expr{f} which precedes {fCode}bar{f} in the
definition of {fCode}foo{f}.

