[antlr-interest] How to do "not" in a syntactic predicate?
Gavin Lambert
antlr at mirality.co.nz
Fri Oct 16 16:42:25 PDT 2009
At 00:39 17/10/2009, Naveen Chawla wrote:
>So, does anybody have a way of doing "Take *a* IF not followed by
>*b* (both syntactic constructs)" ?
>
>i.e.
>q: (a !b)=> a; //("!" or "not" doesn't
>exist in ANTLR)
Actually there is a negation operator, ~ -- but this operates on
sets, not on sequences. So in the lexer you can use "any single
character not in this set" and in the parser you can use "any
single token not in this set", but you can't get it to match on
longer sequences without splitting them up into single-char/token
decisions.
Without knowing more about what your a and b are, it's hard to
give any more specific advice; for example, if b is a fixed
sequence, such as DOT STAR, then you can do the above like so:
q : (a (~DOT | DOT ~STAR)) => a
| /* nothing */
;
(I'm fairly sure you have to explicitly provide an alternative,
either explicitly like this or via ?; if you don't then I think
ANTLR will discard the predicate. With no alternatives a
predicate is useless.)
If b is not a fixed sequence, then you still need to provide the
alternative choice, but by reversing the predicate:
q : (a b) => /* nothing */
| a
;
Note that I haven't actually tested either of these, but in
principle they ought to work. :)
More information about the antlr-interest
mailing list