SO Stinks
SO Stinks

Reputation: 3406

What are the "$@n" variables in the ".output" files?

My ".output" file from bison has many dozens of variables named like $@1, $@2, ..., $@38. These are somehow associated with mid-actions rules, which the grammar I'm examining has a lot of, but in a way I have yet to fully discern. They are representing non-terminal symbols, somehow related to the mid-action rules. That's about all I can tell.

EDIT:


input.y (cartoon version):

%start  statement

%%

statement:  /* empty */ { print("testA"); }
    |   ';'         { print("testB"); }
    |       statement   { print("testC"); } thing1 thing2 { print("testD"); }
    ;

input.output (cartoon version):

0 $accept: statement $end

1 statement: /* empty */
2      | ';'

3 $@1: /* empty */

4 statement: statement $@1 thing1 thing2

10 thing1: /*empty*/
20 thing2: /*empty*/

Upvotes: 0

Views: 249

Answers (2)

rici
rici

Reputation: 241861

Putting a mid-rule action in a rule is almost exactly the same as adding a non-terminal with an empty right-hand-side and the same action (now a final action), except for the numbering of the semantic values in the action. (bison lets you refer to semantic values before the beginning of the production with negative indices; this is the mechanism used for mid-rule actions, except that bison computes the stack offsets for you.)

The inserted empty non-terminals are "named" @1, @2, .... You'll see these in the state descriptions; it will look like every mid-rule action has been replaced by an @i placeholder (which is in fact what has happened).

Edit

It turns out that mid-rule actions might be named $@n or @n. According to a helpful comment in reader.c at line 527 (in v2.6.5, which is the latest release as I write):

  /* If the midrule's $$ is set or its $n is used, remove the `$' from the
     symbol name so that it's a user-defined symbol so that the default
     %destructor and %printer apply.  */

In other words, a mid-rule action will have a $ in its name precisely if it does not have any value (it neither sets the value nor does anyone use the value); if it has a value (and I guess mine almost always do), then it will not have a $ in its name.

Mystery solved, I believe.

Upvotes: 2

akim
akim

Reputation: 8769

Up to Bison 2.3, the mid-rule action's anonymous symbol was named @N, but it was changed in Bison 2.4 into $@N. See http://lists.gnu.org/archive/html/bison-patches/2006-10/msg00075.html

Bottom line is: if you see @N, it's high time to update your copy of Bison :)

Upvotes: 1

Related Questions