Left recursive rules that do not conform to a pattern ANTLR can handle

142 views Asked by At

VarsDecl.g4 describes the syntax of variable declarations, such as int a, b, c.

grammar VarsDecl;

decl : type vars ;
type : 'int'        # IntType
     | 'float'      # FloatType
     ;
vars : left = vars ',' ID  # VarsList
     | ID                  # VarsID
     ;

ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;

VarsDeclAG.g4 is the version of VarsDeclAG.g4 with parameters of rules and and embedded actions.

grammar VarsDeclAG;

decl : type vars[$type.text] ;
type : 'int'      
     | 'float'    
     ;
vars[String typeStr]
     : left = vars[$typeStr] ',' ID { System.out.println($ID.text + " : " + $typeStr); }
     | ID { System.out.println($ID.text + " : " + $typeStr); }
     ;

ID : [a-z]+ ;
WS : [ \t\r\n]+ -> skip ;

However, the ANTLR 4 IntelliJ plugin (version 4.1.11) reports that in VarsDeclAG.g4: rule vars is left recursive but doesn't conform to a pattern ANTLR can handle (no errors in VarsDecl.g4).

Why is it? And how to fix it?

By the way, I do not want to re-write vars as vars : ID (',' ID)*.

0

There are 0 answers