ANTLR以下替代方案无法匹配

时间:2012-12-07 18:46:30

标签: java compiler-construction antlr antlr3

我一直在研究已经存在的关于这个问题的问题,但似乎仍然无法理解。我有一部分语法

/* Variable Declaration */
var_decl_list     : var_decl (var_decl_tail) ;    
var_decl          : var_type id_list ';' | empty ; //error on this line    
var_type          : FLOAT | INT;    
any_type          : var_type | VOID ;     
id_list           : id id_tail ;    
id_tail           : ',' id id_tail | empty ;    
var_decl_tail     : var_decl var_decl_tail | var_decl_tail ;    

它告诉我“(201):以下替代方案永远无法匹配:2”我弄乱了事情已经让它说出一些关于FLOAT和INT的具体内容。 antlr railraoad视图并没有多大帮助。完整的语法是

empty             : ;

/* Program */    
program           : PROGRAM id BEGIN pgm_body END ;    
id                :  IDENTIFIER ;    
pgm_body          : decl func_declarations ;    
decl              : string_decl_list (decl) | var_decl_list (decl) | empty ;    

/* Global String Declaration */    
string_decl       : STRING id ':=' str ';' | empty ;    
string_decl_list  :  string_decl (string_decl_tail) ;    

str               : STRINGLITERAL ;    
string_decl_tail  : string_decl string_decl_tail | string_decl ;    

/* Variable Declaration */    
var_decl_list     : var_decl (var_decl_tail) ;    
var_decl          : var_type id_list ';' | empty ;    
var_type          : FLOAT | INT;    
any_type          : var_type | VOID ;     
id_list           : id id_tail ;    
id_tail           : ',' id id_tail | empty ;    
var_decl_tail     : var_decl var_decl_tail | var_decl_tail ;    

/* Function Paramater List */    
param_decl_list   : param_decl param_decl_tail ;    
param_decl        :var_type id ;    
param_decl_tail   : ',' param_decl param_decl_tail | empty ;    

/* Function Declarations */    
func_declarations : func_decl (func_decl_tail)  ;    
func_decl         : FUNCTION any_type id '('(param_decl_list)')' BEGIN func_body END | empty ;    
func_decl_tail    : func_decl func_decl_tail | func_decl ;    
func_body         : decl stmt_list ;    

/* Statement List */    
stmt_list         : stmt stmt_tail | empty ;    
stmt_tail         : stmt stmt_tail | empty ;    
stmt              : base_stmt | if_stmt | repeat_stmt ;    
base_stmt         : assign_stmt | read_stmt | write_stmt | return_stmt ;    

/* Basic Statements */    
assign_stmt       : assign_expr ';' ;    
assign_expr       : id ':=' expr ;    
read_stmt         : READ '(' id_list ')'';' ;    
write_stmt        : WRITE '(' id_list ')'';' ;    
return_stmt       : RETURN expr ';' ;    

/* Expressions */
expr              : factor expr_tail ;    
expr_tail         : addop factor expr_tail | empty ;    
factor            : postfix_expr factor_tail ;    
factor_tail       : mulop postfix_expr factor_tail | empty ;    
postfix_expr      : primary | call_expr ;    
call_expr         : id '(' expr_list ')' | id '(' ')' ;    
expr_list         : expr expr_list_tail ;    
expr_list_tail    : ',' expr expr_list_tail | empty ;    
primary           : '('expr')' | id | INTLITERAL | FLOATLITERAL ;    
addop             : '+' | '-' ;    
mulop             : '*' | '/' ;    

/* Complex Statements and Condition */     
if_stmt           : IF '(' cond ')' THEN (decl) stmt_list else_part ENDIF ;    
else_part         : 'ELSE' (decl) stmt_list | empty ;    
cond              : expr compop expr ;    
compop            : '<' | '>' | '=' | '!=' ;    
repeat_stmt       : REPEAT (decl) stmt_list UNTIL '(' cond ')'';' ;    

0 个答案:

没有答案