2016-03-20 106 views
0

我正試圖用BNFC編寫一個編譯器。我將使用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 ::=; 

這似乎是我現在用的是_.標籤不正確。但手冊只有一兩行描述它的用途。我在這裏做錯了什麼?

+2

無法幫助文檔。建議你在嘗試一些大的事情之前,先用1-2條規則來獲得一個帶有「bnfc」的語法,並將你的結果與你可以找到的任何語法例子進行比較。 (意見:如果你有一個記錄不好的工具,我會選擇另外一個工具,爲了方便起見,你選擇了一個解析器生成器,以節省你的時間,對吧?)。 –

回答

1

documentation

下劃線當然僅作爲一個參數構造其中值類型是相同的參數類型替換有意義的。

這意味着如果您的規則右側只有一個非終端,並且非終端與左側網站上的終端相同,則只能使用_。所以你可以做一些像_. A ::= "(" A ")" ; 但不是_. A ::= "(" B ")" ;_. A ::= "(" A A ")" ;

在你的例子中,我建議你給每個規則一個標籤,_主要用於簡化在某些角落情況下的AST。

順便說一句,還有一個lists of things的語法快捷方式。