Left recursive rules that do not conform to a pattern ANTLR can handle
hengxin opened this issue · comments
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, ANTLR 4 (version antlr 'org.antlr:antlr4:4.13.1') 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
).
The comments on StackOverflow (thanks to Bart Kiers) suggest that it may be a bug in translateLeftRecursiveRule.
I suspect it's related to #564 and similar issues. This is because ANTLR expects only simple terms in alternatives of left-recursive rule. In your case ANTLR can't handle left = vars[$typeStr]
because it has a label (left
) or [$typeStr]
syntax.