[antlr-interest] All except...

Johannes Luber jaluber at gmx.de
Tue Jun 5 04:13:33 PDT 2007


Phil Oliver wrote:
> This might be a terribly simple question though I've looked pretty hard
> and can't find an obvious solution nor examples in sample grammars: how
> does one create an ANTLR v3 rule (either lexer or parser) that easily
> matches any character EXCEPT a set of other characters? e.g. let's say I
> have:
> 
> Char : '\u0009' | '\u000A' | '\u000D' | '\u0020'..'\uD7FF' |
> '\uE000'..'\uFFFD';
> 
> and I want to define a rule that matches any Char except another list of
> characters. In the EBNF grammar used in the XQuery spec, for example, it
> would be:
> 
> Char2: Char - ('<' | '>');
> 
> which would cause Char2 to match any character in Char except for '<' or
> '>'.  But that operator isn't part of ANTLR (evidently). I've looked at
> the ~ unary operator but that doesn't handle this job, unless I'm
> overlooking something.
> 
> I understand that one could explicitly define Char2 to be an allowable
> sequence from Char that already excludes '<' and '>' but that's tedious
> and hides the abstract meaning of the definition.

The correct syntax for Char2 is

Char2: ~Char;

Why shouldn't the ~ work in your case?

Best regards,
Johannes Luber




More information about the antlr-interest mailing list