I want to use a syntactic predicate, but only with one alternative.
Created Sep 3, 1999
Monty Zukowski
But ANTLR complained that the predicate was meaningless because there was no
other alternative. So I wrote it like this:
Sometimes you use rules with an empty alternative with syntactic predicates. In R/BASIC a COLON could be used as an operator in an expression, or it could follow an expression to end a PRINT statment, so I needed a syntactic predicate to distinguish between
PRINT A:B PRINT A:B:
What I wanted to do was:
concatenationExpr
: additiveExpr
(COLON ~(NEWLINE)) =>
((COLON | TRICOLON) concatenationExpr)
;
concatenationExpr
: additiveExpr
( (COLON ~(NEWLINE)) =>
((COLON | TRICOLON) concatenationExpr)
|
)
;
This works fine, but you do have to be very aware of putting anything with an empty alternative into a loop because you might end up with an infinite loop. As a matter of fact the next thing I thought was that most concatenationExprs are right after another, so I could do this:
concatenationExpr
: additiveExpr
( (COLON ~(NEWLINE)) =>
((COLON | TRICOLON) concatenationExpr)
|
)*
;
This gave me the infinite loop when the syntactic predicate failed, and matched the empty alt over and over.