[antlr-interest] Request for Change regarding Lexer (?)

Terence Parr parrt at cs.usfca.edu
Tue Feb 16 12:31:26 PST 2010


On Feb 16, 2010, at 12:27 PM, Gavin Lambert wrote:

> At 08:13 17/02/2010, Terence Parr wrote:
> >} and what I need is loop escaping dependent on previous match, so
> >some constrained context passing would be nice.
> >
> >try predicates. :)
> 
> While it's usually possible to frame the contents of a looping construct in a positive sense (which is what ANTLR currently requires), it'd be nice if there were a language construct that would let you do specific negative matches too (maybe a feature request for v4?).

Been thinking about that.

> For (a completely made up) example, consider a case where you might want to match any identifier except one starting with "foo".  In current ANTLR, you'd have to do one of these:
>  FOOLIST: 'foo[' NON_FOO_ID+ ']';
>  FOOLIST: 'foo[' ({!next_id_starts_with_foo()}? => ID)+ ']';
>  FOOLIST: 'foo[' ((~'f' | 'f' ~'o' | 'fo' ~'o') => ID)+ ']';
> 
> It'd be nice if there was some way to express a negative match via a syntactic predicate, eg:
>  FOOLIST: 'foo[' (('foo') => ~ | ID)+ ']';
> (where '~' in an alt basically means "break", ie. match nothing and terminate the innermost loop.)
> Or, perhaps better:
>  FOOLIST: 'foo[' (('foo') ~=> ID)+ ']';
> (where '~=>' means "only take this path if the predicate *fails*")
> 
> Granted, this sort of requirement doesn't come up often, but when it does it'd be nice to have a tidier way of expressing it; and it'd be fairly simple to implement... :)

happens only in lexer as far as i can tell but would be nice to have.

updated

http://www.antlr.org/wiki/display/~admin/2010/01/19/ANTLR+v4+lexers

Ter



More information about the antlr-interest mailing list