[antlr-interest] Non-greedy lexer rule in ANTLR4
Bart Kiers
bkiers at gmail.com
Fri Oct 12 10:17:41 PDT 2012
> Is it possible to write a non-greedy lexer rule that does not contain a
> dot in ANTLR4? For example:
>
> ANCHOR
> : '<<' ( ~( '>>' | '\r' | '\n' ) )* '>>'
> ;
>
Hi Michel,
You can't negate two (or more) characters: negation only works for
char-sets that match a single character.
> I'm using 4.0b2 and the above seems to be greedy.
>
> Alternatively, would it be possible to rewrite this rule so it includes
> everything from '<<' to the first '>>' but not more?
>
Try something like this:
ANCHOR
: '<<' ( ~( '>' | '\r' | '\n' ) | '>' ~'>' )* '>>'
;
regards,
Bart.
More information about the antlr-interest
mailing list