I'm trying to get a simple grammar to work using ANTLR4. Basically a list of keywords separated by ;
that can be negated using Not
. Something like this, for example:
Not negative keyword;positive
I wrote the following grammar:
grammar input;
input : clauses;
keyword : NOT? WORD;
clauses : keyword (SEPARATOR clauses)?;
fragment N : ('N'|'n') ;
fragment O : ('O'|'o') ;
fragment T : ('T'|'t') ;
fragment SPACE : ' ' ;
SEPARATOR : ';';
NOT : N O T SPACE;
WORD : ~[;]+;
My issue is that in the keyword
rule, WORD
seems to have more priority than NOT
. Not something
is recognized as the Not something
word instead of a negated something
.
For instance, the parse tree I get is this
.
What I'm trying to achieve is something like this
How can you give an expression more priority over another on ANTLR4? Any tip on fixing this?
Please note that while this grammar is very simple and ANTLR4 can seem unecessary here, the true grammar I want to make is more complex and I have just simplified it here to demonstrate my issue.
Thank you for your time!
~[';']
is equivalent to~[';]
and matches any character other than'
or;
(repeating a character in a character class does nothing). I assume you want just~[;]
or~';'
(both of which mean "any character other than;
"). – sepp2k