Hi for example I have this code for the g4 file:
a: [A-Z][A-Z];
b: [a-z]'3';
Now I want to add one line more, which recognizes all characters that do not belong to a or b
I tried:
a: [A-Z][A-Z];
b: [a-z]'3';
ALLOTHERCHARACTERS: ~[a]|~[b]
But i didn´t work.
For example the input 84209ddjio29 should now be in ALLOTHERCARACTERS, but i didn ´t work.
(The Lexer gives at the end a java file, but I think this is not important to know, for this "task")
CodePudding user response:
There are many things going wrong here: inside parser rules, you cannot use character sets. So a: [A-Z][A-Z];
is not possible. Only a lexer rule can use character sets, so A: [A-Z][A-Z];
is valid.
So, to define a valid (lexer) grammar, you'd need to do this:
A : [A-Z] [A-Z];
B : [a-z] '3';
Now for your second problem: how to negate rules A
and B
? Answer: you cannot. You can only negate single characters. So negating A : [A-Z];
would be NA: ~[A-Z];
(or NA : ~A;
is also valid). But you cannot negate a rule that matches 2 characters like A : [A-Z] [A-Z];
.
If you want a rule that matches anything other than upper case letters, lower case letters and the digit 3, then you can so this:
ALLOTHERCHARACTERS : ~[A-Za-z3];
CodePudding user response:
This is the proper syntax for "anything except":
[^ab]
so that will match any character that is not a or b.