[antlr-interest] Non-greedy lexer rule in ANTLR4
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:
> : '<<' ( ~( '>>' | '\r' | '\n' ) )* '>>'
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:
: '<<' ( ~( '>' | '\r' | '\n' ) | '>' ~'>' )* '>>'
More information about the antlr-interest