我正在阅读一些用于解析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));
};
答案 0 :(得分:1)
编写规则时,令牌和其他规则的编号从$1
开始。任何嵌入的代码块也都有一个数字。因此,代码中的第一个替代方案,我们(概述):
enum_name:
enum_key gcc_type_attribute_opt { ... } '{' enumerator_list '}'
gcc_type_attribute_opt { ... }
此处enum_key
为$1
,gcc_type_attribute_opt
为$2
,第一个代码区{ ... }
为$3
,{{1} }是'{'
,$4
是enumerator_list
,$5
是}
,$6
是gcc_type_attribute_opt
,最后一个代码块是$7
。如果规则返回值,则可以使用$8
表示法找到这些值。
因此,在最后一个代码块的操作中,$n
是第一个$2
,gcc_type_attribute_opt
是第二个,$7
是{{1} }}
第二种选择的分析是类似的。
讨论的关键点是嵌入的代码块也被分配了一个数字;因为你不知道,你会被计数弄糊涂。