I'm trying to solve a pretty simple grammar problem (I just started learning about using ANTLR
to develop grammars; I'm a bit new so just bear with me) which is to define a signed even number using ANTLR
.
A '+' or '-'
for the start token is optional, and the number can be 1 or more digits but the last digit must be even (for example,+4394
would be a valid signed even number).
The best grammar I have so far is as follows:
grammar SignedEvenNumber;
DIGIT : '0'..'9';
EVEN_DIGIT : '0' | '2' | '4' | '6' | '8';
signedEvenNumber : ('+' | '-' | ) NUMBER+ EVEN_NUMBER;
My issue is defining digits in a way that forces ANTLR
to check for an even digit as the last digit; i.e. it always sees the last digit as DIGIT
, regardless of it being even or odd (because any digit before the final can be either). There might be a really simple solution that I'm just not getting but any help would be appreciated.