Does logical AND and NOT exists in ANTLR?
Asked Answered
M

2

11

Is there NOT logic in ANTLR? Im basically trying to negate a rule that i have and was wondering if its possible, also is there AND logic?

Mayan answered 3/4, 2011 at 21:13 Comment(0)
G
18

@larsmans already supplied the answer, I just like to give an example of the legal negations in ANTLR rules (since it happens quite a lot that mistakes are made with them).

The negation operator in ANTLR is ~ (tilde). Inside lexer rules, the ~ negates a single character:

NOT_A : ~'A';

matches any character except 'A' and:

NOT_LOWER_CASE : ~('a'..'z');

matches any character except a lowercase ASCII letter. The lats example could also be written as:

NOT_LOWER_CASE : ~LOWER_CASE;
LOWER_CASE : 'a'..'z';

As long as you negate just a single character, it's valid to use ~. It is invalid to do something like this:

INVALID : ~('a' | 'aa');

because you can't negate the string 'aa'.

Inside parser rules, negation does not work with characters, but on tokens. So the parse rule:

parse
  :  ~B
  ;

A : 'a';
B : 'b';
C : 'c';

does not match any character other than 'b', but matches any token other than the B token. So it'd match either token A (character 'a') or token C (character 'c').

The same logic applies to the . (DOT) operator:

  • inside lexer rules it matches any character from the set \u0000..\uFFFF;
  • inside parser rules it matches any token (any lexer rule).
Genip answered 4/4, 2011 at 6:49 Comment(6)
Hi, do you know know to write a rule to match simple strings but not specific keywords? I tried to write rules like this one below (but it doesn't work at all) STRING: ~('\r' | '\n' | '\t' | ' ' | 'keywords')Thereabout
Your example, NOT_LOWER_CASE : ~LOWER_CASE; LOWER_CASE : 'a'..'z'; does not work in Antlr4. I get "rule reference LOWER_CASE is not currently supported in a set"Gooey
@baruchl my answer is for ANTLR3, not ANTLR4. So in v4 you just do this: NOT_LOWER_CASE : ~[a-z]; LOWER_CASE : [a-z];Genip
@Bart Kiers Hi, can you help me for this case: (.*? calendar ~'a' .*?), the problem is that if the sentence ends it is not matching the rule, when I enter (February), it is not matching, but when I enter (February.) with dot, it gives a match because we have (.) which is not (a), how can I say that if there is nothing after calendar it is a match, or if there is (a) after calendar then it is not a a match?Vaginate
@HamzaAlAjlouni Please create a question of your own: these small comment boxes are not suited for Q&A's. When you create a question, be sure to include enough information for others to understand what you mean (right now, there is not enough information).Genip
@Bart Kiers here you can find the question and details about it (stackoverflow.com/questions/78437221/using-not-in-antlr-rules)Vaginate
N
12

ANTLR produces parsers for context-free languages (CFLs). In that context, not would translate to complement and and to intersection. However, CFLs aren't closed under complement and intersection, i.e. not(rule) is not necessarily a CFG rule.

In other words, it's impossible to implement not and and in a sane way, so they're not supported.

Noway answered 3/4, 2011 at 21:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.