I am trying to parse a date using Antlr4 with C# as a target. A valid date in my case should have the following
- be in
year / month / day
format - year MUST have only 4 digits
- month and day MUST have only 2 digits
I know that similar questions are already here, but their solutions does not seem to work for me
- How to create a antlr4 grammar which will parse date
- Antlr: Simplest way to recognize dates and numbers?
I have read somewhere that there is a priority-like parsing, where top-most rules based on how the grammar file is written are evaluated first. So consider that apart from dates my grammar should also be able to parse integers.
The grammar I have and works (but it does not follow the aforementioned rules is the following)
/*
* Parser Rules
*/
dateFormat : DECIMAL '/' DECIMAL '/' DECIMAL
;
/*
* Lexer Rules
*/
DECIMAL: DEC_DIGIT+;
fragment DEC_DIGIT: [0-9];
I tried to put something like
YEAR or year : DEC_DIGIT DEC_DIGIT DEC_DIGIT DEC_DIGIT;
in either lexer or parser rules but it did not work.
Any ideas / suggestions ?
Note: Please do not suggest alternatives of regex or argue on whenever I should use Antlr or not.