1

First of all, i am completely new to antlr4, DSL & EBNF so it might be the case, that i'm not using antlr4 the right way, but right now i'm not aware of an alternative way for what i'm trying to do. Here it is:

I want to have a rule, which should consist of three upperCases but it shouldn't be allowed to have an reserved String as an Result.

Example:

parse: threeUpperCase;

threeUpperCase: UPPER UPPER UPPER;

UPPER:[A-Z]

I want to avoid that threeUpperCase matches 'ABC' & 'DEF (BCA, CAB,BAC, FED, EDF etc. are allowed). Is there a way to do this ?

1 Answers1

0

You shouldn't match these tokens in a parser rule. Making it a lexer rule, and placing it after the rules that match your keywords, should produce the proper tokens:

KEYWORD          : 'ABC' | 'DEF';
THREE_UPPER_CASE : [A-Z] [A-Z] [A-Z];

or:

K_ABC            : 'ABC';
K_DEF            : 'DEF';
THREE_UPPER_CASE : [A-Z] [A-Z] [A-Z];
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288
  • I had expected something like ~(ABC DEF) , but after rearranging my grammar to this construct, your solution seems to work out for me. I really don't understand why this is happening, but thank you for your quick reply ! – user3041315 Nov 28 '13 at 09:45
  • @user3041315, negation, whether inside a parser- or lexer-rule, can only be used on sets (containing single chars or tokens). Trying to negate two or more tokens, like `~(ABC DEF)` (or more than 1 char) is not possible. You must also realize that the lexer is not *driven* by the parser: no matter what the parser "needs" at a particular time, the lexer just constructs tokens based on its own rule priority. More info: http://stackoverflow.com/questions/8284919/negating-inside-lexer-and-parser-rules and: http://stackoverflow.com/questions/9251048/antlr-v3-error-with-parser-lexer-rules – Bart Kiers Nov 28 '13 at 09:54