2
votes

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

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.

1

1 Answers

2
votes

I suppose this should work. date parse rule for dates, integer rule for integers.

date
    : year=FOUR_DIGITS SLASH month=TWO_DIGITS SLASH day=TWO_DIGITS
    ;

integer
    : INTEGER
    | FOUR_DIGITS
    | TWO_DIGITS
    ;

FOUR_DIGITS: DIGIT DIGIT DIGIT DIGIT;
TWO_DIGITS:  DIGIT DIGIT;
INTEGER:     DIGIT+;
SLASH:       '/';

fragment     DIGIT: [0-9];