我正在尝试用BNFC编写一个编译器。我将使用BNFC生成抽象语法树。但我得到的错误,我似乎无法找出原因。似乎没有太多关于它的文档。
以下是我遇到的错误:
Bad coercion in rule _. Prog ::= Block
Bad coercion in rule _. Declarations ::= Declaration ";" Declarations
Bad coercion in rule _. Declarations ::=
Bad coercion in rule _. Declaration ::= Var_declaration
Bad coercion in rule _. Declaration ::= Fun_declaration
Bad coercion in rule _. Type ::= "int"
Bad coercion in rule _. Type ::= "real"
Bad coercion in rule _. Type ::= "bool"
Bad coercion in rule _. Array_dimensions ::= "[" Expr "]" Array_dimensions
Bad coercion in rule _. Array_dimensions ::=
Bad coercion in rule _. Fun_block ::= Declarations Fun_body
Bad coercion in rule _. Param_list ::= "(" Parameters ")"
Bad coercion in rule _. Parameters ::= Basic_declaration More_parameters
Bad coercion in rule _. Parameters ::=
Bad coercion in rule _. More_parameters ::= "," Basic_declaration More_parameters
Bad coercion in rule _. More_parameters ::=
Bad coercion in rule _. Basic_declaration ::= Ident Basic_array_dimensions ":" Type
Bad coercion in rule _. Basic_array_dimensions ::=
Bad coercion in rule _. Program_body ::= "begin" Prog_stmts "end"
Bad coercion in rule _. Fun_body ::= "begin" Prog_stmts "return" Expr ";" "end"
Bad coercion in rule _. Prog_stmts ::= Prog_stmt ";" Prog_stmts
Bad coercion in rule _. Prog_stmts ::=
Bad coercion in rule _. Identifier ::= Ident Array_dimensions
Bad coercion in rule _. Expr ::= Bint_term
Bad coercion in rule _. Bint_term ::= Bint_factor
Bad coercion in rule _. Bint_factor ::= Int_expr Compare_op Int_expr
Bad coercion in rule _. Bint_factor ::= Int_expr
Bad coercion in rule _. Int_expr ::= Int_expr Addop Int_term
Bad coercion in rule _. Int_expr ::= Int_term
Bad coercion in rule _. Int_term ::= Int_term Mulop Int_factor
Bad coercion in rule _. Int_term ::= Int_factor
Bad coercion in rule _. Int_factor ::= "(" Expr ")"
Bad coercion in rule _. Modifier_list ::= "(" Arguments ")"
Bad coercion in rule _. Modifier_list ::= Array_dimensions
Bad coercion in rule _. Arguments ::= Expr More_arguments
Bad coercion in rule _. Arguments ::=
Bad coercion in rule _. More_arguments ::= "," Expr More_arguments
Bad coercion in rule _. More_arguments ::=
以下是BNFC文件的示例:
_.Prog ::= Block;
M_Prog. Block ::= Declarations Program_body;
_.Declarations ::= Declaration ";" Declarations;
_. Declarations ::= ;
_. Declaration ::= Var_declaration;
_. Declaration ::= Fun_declaration;
M_Var. Var_declaration ::= "var" Ident Array_dimensions ":" Type;
_. Type ::= "int";
_. Type ::= "real";
_. Type ::= "bool";
_. Array_dimensions ::= "[" Expr "]" Array_dimensions;
_. Array_dimensions ::=;
M_Fun. Fun_declaration ::= "fun" Ident Param_list ":" Type "{" Fun_block "}";
_. Fun_block ::= Declarations Fun_body;
_. Param_list ::= "(" Parameters ")";
_. Parameters ::= Basic_declaration More_parameters;
_. Parameters ::= ;
_. More_parameters ::= "," Basic_declaration More_parameters;
_. More_parameters ::= ;
_. Basic_declaration ::= Ident Basic_array_dimensions ":" Type;
_. Basic_array_dimensions ::= "[" "]" Basic_array_dimensions;
_. Basic_array_dimensions ::=;
似乎我错误地使用了_.
标签。但是手册中只有一两行描述它的使用。我在这里做错了什么?
答案 0 :(得分:1)
下划线当然仅作为单参数构造函数的替换而有意义,其中值类型与参数类型相同。
这意味着只有在规则右侧只有一个非终端且非终端与左侧站点上的非终端相同时,才能使用_
。所以你可以做_. A ::= "(" A ")" ;
之类的事情
但不是_. A ::= "(" B ")" ;
也不是_. A ::= "(" A A ")" ;
。
在您的示例中,我建议您为每个规则添加标签,_
主要用于在某些极端情况下简化AST。
BTW还有lists of things的语法快捷方式。