Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
hengxin opened this issue Apr 22, 2024 · 1 comment
Open

Comments

@hengxin
Copy link

hengxin commented Apr 22, 2024

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.

@KvanTTT
Copy link
Member

KvanTTT commented Apr 22, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants