The following grammar works fine:
grammar DBParser;
statement: expr EOF;
expr: expr ' ' expr | Atom;
Atom: [a-z] | [0-9] ;
However, neither of the following do:
grammar DBParser;
statement: expr EOF;
expr: (expr ' ' expr) | Atom;
Atom: [a-z] | [0-9] ;
grammar DBParser;
statement: expr EOF;
expr: (expr ' ' expr | Atom);
Atom: [a-z] | [0-9] ;
Why does antlr4 raise an error when adding in parentheticals, does that somehow change the meaning of the production that is being parsed?
CodePudding user response:
Parentheses create a subrule, and subrules are handled internally by treating them as though they were new productions (in effect anonymous, which is why the mutual recursion error message only lists one non-terminal).
In these particular examples, the subrule is pointless; the parentheses could simply be removed without altering the grammar. But apparently Antlr doesn't attempt to decide which subrules are actually serving a purpose. (I suppose it could, but I wonder if it's a common enough usage to make justify the additional code complexity. But it's certainly not up to me to decide.)