Min Gao
Min Gao

Reputation: 403

Meaning of $7 and $8 in a bison code

I'm reading some Bison code for parsing a C program. Can anyone tell me the meaning of $7 and $8 in the following code because I only find 6 and 7 descriptions in two cases to describe a enum type.

enum_name:
      enum_key
      gcc_type_attribute_opt
      {
        init($$);
        PARSER.new_declaration(stack($1), symbol, stack($$), true);
        PARSER.copy_item(to_ansi_c_declaration(stack($$)));
      }
      '{' enumerator_list '}'
      gcc_type_attribute_opt
    {

      // throw in the gcc attributes
      merge_types($$, $2);
      merge_types($$, $7);

      do_enum_members((const typet &)stack($$), stack($5));
    }
    | enum_key
      gcc_type_attribute_opt
      identifier_or_typedef_name
      {
        init($$);
        PARSER.new_declaration(stack($1), stack($3), stack($$), true);
        PARSER.copy_item(to_ansi_c_declaration(stack($$)));
      }
      '{' enumerator_list '}'
      gcc_type_attribute_opt
    {
      // throw in the gcc attributes
      merge_types($$, $2);
      merge_types($$, $8);

      do_enum_members((const typet &)stack($$), stack($6));
    };

Upvotes: 0

Views: 134

Answers (1)

Jonathan Leffler
Jonathan Leffler

Reputation: 754530

When you write a rule, the tokens and other rules are given numbers starting from $1. Any embedded code blocks are also given a number. Thus, the first alternative in your code, we have (in outline):

enum_name:
        enum_key gcc_type_attribute_opt { ... } '{' enumerator_list '}'
        gcc_type_attribute_opt { ... }

Here, enum_key is $1, gcc_type_attribute_opt is $2, the first code block { ... } is $3, the '{' is $4, enumerator_list is $5, } is $6, gcc_type_attribute_opt is $7, and the last code block is $8. If the rules return values, you can find those values using the $n notation.

So, in the action that is the last code block, $2 is the first gcc_type_attribute_opt, $7 is the second one, and $5 is the enumerator_list.

The analysis for the second alternative is similar.

The key point in the discussion is that the embedded code blocks are assigned a number too; since you didn't know that, you would be confused by the counting.

Upvotes: 1

Related Questions