BNFC 规则中的不良强制
BNFC Bad Coercion in rule
我正在尝试使用 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 ::=;
看来是我用错了_.
标签。但是手册只有一两行描述它的用途。我在这里做错了什么?
Underscores are of course only meaningful as replacements of one-argument constructors where the value type is the same as the argument type.
这意味着如果您的规则右侧恰好有一个非终结符并且该非终结符与左侧站点上的非终结符相同,则您只能使用 _
。所以你可以做类似 _. A ::= "(" A ")" ;
但不是 _. A ::= "(" B ")" ;
也不是 _. A ::= "(" A A ")" ;
.
在你的例子中,我建议你给每条规则都贴上标签,_
主要用于在一些极端情况下简化AST。
顺便说一句,lists of things.
还有一个语法快捷方式
我正在尝试使用 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 ::=;
看来是我用错了_.
标签。但是手册只有一两行描述它的用途。我在这里做错了什么?
Underscores are of course only meaningful as replacements of one-argument constructors where the value type is the same as the argument type.
这意味着如果您的规则右侧恰好有一个非终结符并且该非终结符与左侧站点上的非终结符相同,则您只能使用 _
。所以你可以做类似 _. A ::= "(" A ")" ;
但不是 _. A ::= "(" B ")" ;
也不是 _. A ::= "(" A A ")" ;
.
在你的例子中,我建议你给每条规则都贴上标签,_
主要用于在一些极端情况下简化AST。
顺便说一句,lists of things.
还有一个语法快捷方式