Reputation: 410
In Antlr3, I have the following grammar:
ruleA:
(ruleBStart) => ruleB
| ruleC
;
For the sake of simplicity let's assume ruleB is the grammar for SELECT statement in SQL, but could be nested in an arbitrary number of LPARENs. This is easy to represent in old grammar by simply saying:
ruleBStart:
(LPAREN)* SELECT
;
In Antlr4, if I wanted to do the same thing, I would write a semantic predicate isRuleBStart()
which may look like this (pseudocode):
@parser::members{
public boolean isRuleBStart(int tokenNum)
{
int token = _input.LA(tokenNum);
if (token == EOF) return false; // handling EOF probably needs more work
if (token == SELECT) return true;
if (token == LPAREN) return isRuleBStart(tokenNum++);
return false;
}
}
And then in my grammar, I would do:
ruleA:
{isRuleBStart(1)}? ruleB
| ruleC
;
This appears problematic to me since:
ruleBStart
could get much more complicated if the ruleBStart rule had an arbitrary set of different tokens to check instead of just repeating LPARENSo I want to ask the community if there is a right Antlr4 way to achieve the same result.
Upvotes: 4
Views: 728
Reputation: 53502
There's no need for a semantic predicate with ANTLR4. The ALL(*) algorithm will do an unlimited lookahead, if needed and hence doesn't need semantic predicates or any comparable hack.
So, just remove that predicate and everything should just work.
Upvotes: 1