In ANTLR, there are logical AND and NOT?

Is there no logic in ANTLR? I basically try to nullify the rule that I have, and wondered if it is also possible, is there an AND logic?

+4
source share
2 answers

@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).
+10
source

ANTLR creates parsers for context-free languages (CFLs). In this context, not will translate to complement and and to intersect. However, CFLs do not close under padding and intersection, i.e. not(rule) not necessarily a CFG rule.

In other words, it is not possible to implement not and and reasonable way, so they are not supported.

+6
source

Source: https://habr.com/ru/post/1346476/


All Articles