[antlr-interest] Understanding ANTLR's 'multiple alternatives'
Gavin Lambert
antlr at mirality.co.nz
Tue Oct 14 13:13:53 PDT 2008
At 07:54 15/10/2008, `VL wrote:
>Hmm. This looks better. But it doesn't match construction
>like this: $(VAR)/some/path.
That's a different form than what you were showing before :)
To match that, you can treat them all as equals:
PATH : ('/' | ENVAR | ID | INT)+;
Of course, this no longer guarantees that there is a '/' present
at all.
Another option specifically for the leading-ENVAR case (basically
assuming that all paths must be absolute, but the base path may be
contained in an ENVAR):
PATH : ENVAR? ('/' (ID | INT)*)+;
You do need to be careful you're not doing too much work in the
lexer, though. Remember that the lexer is context-free, and all
top-level rules are always candidates to match the input. You may
therefore end up with a different token type than you're
expecting, if there is overlap between the token rules.
More information about the antlr-interest
mailing list