@larsmans already provided the answer, I just would like to give an example of legal denials in the ANTLR rules (since this happens quite often that errors happen to them).
The negation operator in ANTLR is ~ (tilde). Inside the rules, lexer ~ cancels one character:
NOT_A : ~'A';
matches any character except 'A' and:
NOT_LOWER_CASE : ~('a'..'z');
matches any character except lowercase ASCII. An example lats can also be written as:
NOT_LOWER_CASE : ~LOWER_CASE; LOWER_CASE : 'a'..'z';
As long as you deny only one character, it is acceptable to use ~ . It is not valid to do something like this:
INVALID : ~('a' | 'aa');
because you cannot cancel the string 'aa' .
Inside the parser rules, negation does not work with symbols, but on tokens. Thus, the parse rule:
parse : ~B ; A : 'a'; B : 'b'; C : 'c';
matches not any character other than 'b' , but matches any token other than token B Thus, it will correspond to either token A (symbol 'A' ) or token C (symbol 'c' ).
The same logic applies to the operator . (DOT):
- inside the lexer rule, it matches any character from the set
\u0000..\uFFFF ; - inside the parser rule, it matches any token (any lexer rule).
source share