In Java.g, we can find the following rule (whitespace removed for clarity):
statement
: block
| ('assert') expression (':' expression)? ';'
| 'assert' expression (':' expression)? ';'
| 'if' parExpression statement ('else' statement)?
(...)
;
I’ve two questions:
- Are two ‘assert’ subrules really needed?
- If not, why doesn’t ANTLR complain of ambiguity, since they seem to define exactly the same input?
Thanks in Advance.
No, they’re equivalent. However, by looking at the way the grammar Java.g is formatted:
I suspect (by looking at the empty spaces) that the grammar originally had embedded code in it:
and that perhaps more rules/tokens were stripped (all guesswork, of course!).
Because global backtracking (the
backtrack=true;in the options) is enabled.