野牛代码中$ 7和$ 8的含义

时间:2013-05-16 21:27:37

标签: bison

我正在阅读一些用于解析C程序的Bison代码。以下代码中有人能告诉我7美元和8美元的含义,因为在两种情况下我只能找到6和7描述来描述枚举类型。

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));
    };

1 个答案:

答案 0 :(得分:1)

编写规则时,令牌和其他规则的编号从$1开始。任何嵌入的代码块也都有一个数字。因此,代码中的第一个替代方案,我们(概述):

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

此处enum_key$1gcc_type_attribute_opt$2,第一个代码区{ ... }$3,{{1} }是'{'$4enumerator_list$5}$6gcc_type_attribute_opt,最后一个代码块是$7。如果规则返回值,则可以使用$8表示法找到这些值。

因此,在最后一个代码块的操作中,$n是第一个$2gcc_type_attribute_opt是第二个,$7是{{1} }}

第二种选择的分析是类似的。

讨论的关键点是嵌入的代码块也被分配了一个数字;因为你不知道,你会被计数弄糊涂。