| 1 |
Often an action accompanies a production rule. It contains bf(C++) code that |
| 2 |
is executed once that production rule is recognized. The task of most actions |
| 3 |
is to compute a semantic value for the nonterminal recognized by the |
| 4 |
production rule, often using the semantic values made available by components |
| 5 |
of the production rule. |
| 6 |
|
| 7 |
An action consists of bf(C++) statements surrounded by braces, much like a |
| 8 |
compound statement in bf(C++). Most rules have just one action at the end of |
| 9 |
the rule, following all the components. Actions in the middle of a rule are |
| 10 |
tricky and should be used only for special purposes (see section |
| 11 |
ref(MIDACTIONS)). |
| 12 |
|
| 13 |
The components of production rules are numbered, the first component having |
| 14 |
number 1. E.g., in a production rule |
| 15 |
verb( |
| 16 |
nonterm: |
| 17 |
first second third |
| 18 |
) |
| 19 |
tt(first) is component #1, tt(second) is component #2, tt(third) is |
| 20 |
component #3. bf(C++) code in action blocks may refer to semantic values of |
| 21 |
these components using em(dollar-notations), where tt($i) refers to |
| 22 |
the semantic value of the i^sups(th) component. |
| 23 |
|
| 24 |
Likewise, the semantic value of the rule's nonterminal is represented by |
| 25 |
tt($$). Here is a typical example: |
| 26 |
verb( |
| 27 |
exp: |
| 28 |
... |
| 29 |
| |
| 30 |
exp '+' exp |
| 31 |
{ |
| 32 |
$$ = $1 + $3; |
| 33 |
} |
| 34 |
| |
| 35 |
... |
| 36 |
) |
| 37 |
This rule constructs tt(exp) from two exp nonterminals connected by a |
| 38 |
plus-sign token. In the action, tt($1) and tt($3) represent the semantic |
| 39 |
values of, respectively, the first (left-hand side) tt(exp) component, and the |
| 40 |
second (right-hand side) tt(exp) component. |
| 41 |
|
| 42 |
The sum is assigned to tt($$), which becomes the semantic value of the tt(exp) |
| 43 |
nonterminal represented by the production rule. |
| 44 |
|
| 45 |
Depending on the specification of the tt(default-actions) option/directive |
| 46 |
(cf. section ref(DEFAULTACTION)) b() may supply non-empty production rules |
| 47 |
with default action blocks containing the statement tt($$ = $1): the semantic |
| 48 |
value of the first component of the production rule is returned as the |
| 49 |
nonterminal's semantic value. Of course, the default action only is valid if |
| 50 |
the two data types match. Empty production rules are not provided with |
| 51 |
default action blocks. |
| 52 |
|
| 53 |
Negative dollar indices (e.g., $-1) are allowed, and refer to semantic |
| 54 |
values of elements in rules before the component naming the current rule's |
| 55 |
nonterminal. This is a risky practice, and you should use it only when you |
| 56 |
know what you're doing. Here is a situation where you can use this reliably: |
| 57 |
verb( |
| 58 |
vardef: |
| 59 |
type varlist ';' |
| 60 |
; |
| 61 |
|
| 62 |
varlist: |
| 63 |
varlist ',' variable |
| 64 |
{ |
| 65 |
defineVar($-1, $3); |
| 66 |
} |
| 67 |
| |
| 68 |
variable |
| 69 |
{ |
| 70 |
defineVar($-1, $1); |
| 71 |
} |
| 72 |
; |
| 73 |
) |
| 74 |
|
| 75 |
As long as tt(varlist) is only used in the tt(vardef) rule tt(varlist) can |
| 76 |
be sure that the type of the variable is available as the semantic value of |
| 77 |
the component immediately before (hence: $-1) the tt(varlist) component in the |
| 78 |
tt(vardef) rule. See also section ref(SPECIAL). |
| 79 |
|
| 80 |
In addition to the dollar-notations shown here, b() supports several more |
| 81 |
dollar-notations. The next three subsections describe the dollar-notations |
| 82 |
that are available after specifying, respectively, the tt(%stype, %union) or |
| 83 |
tt(%polymorphic) directives. |