0

I have been looking at the already existing questions about this problem but still cannot seem to understand it. I have a part of my grammar

/* 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 ;    

And it tells me "(201): The following alternatives can never be matched: 2" which me messing with things have gotten it to say something specific about FLOAT and INT. the antlr railraoad view doesnt help much. the full grammar is

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 ')'';' ;    
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
McSick
  • 116
  • 1
  • 1
  • 6
  • 2
    The error message you posted tells very little: it contains information about the line number where the error occurred in your grammar. But the grammar you posted is not valid: it contains a left recursive rule: `var_decl_tail`. ANTLR v3 does not support left recursion. – Bart Kiers Dec 07 '12 at 19:01
  • Your source grammar ([this?](https://engineering.purdue.edu/~eigenman/ECE495S/projects/grammar.txt)) isn't designed for a parser generator like ANTLR, e.g., it has left-recursive rules as Bart mentions. You may want to get familiar with ANTLR first before converting a grammar over to it. – user1201210 Dec 07 '12 at 20:48
  • Thanks for the input everyone. I decided to research hardcore how to remove all these left recursive rules. After many hours I finally got the grammar to comply with out any warnings or errors!!! – McSick Dec 09 '12 at 17:18
  • @McSick, cool, in that case, feel free to remove this question. – Bart Kiers Dec 09 '12 at 20:43

0 Answers0