'Repeat a token specific number of times in ANTLR4

I want to design a rule that expresses date format types like :

  • 'ddmmyyyy'
  • 'dd/mm/yyyy'
  • 'mm/dd/yyyy'
  • 'dd-mm-yyyy'
  • 'yyyymmdd'

I tried this in Lexer, but it didn't work (token recognition error)

fragment DATE_SEPARATOR: ('/'|',');

FORMATE
: '\'' (MONTH{1,2} (DATE_SEPARATOR)? DAY{1,2}  (DATE_SEPARATOR)? YEAR{1,4}
| DAY{1,2}  (DATE_SEPARATOR)? MONTH{1,3}  (DATE_SEPARATOR)? YEAR{1,4}
| YEAR{1,4}  (DATE_SEPARATOR)? MONTH{1,3}  (DATE_SEPARATOR)? DAY{1,2})
;
fragment DAY : ('d'|'D'|'day'|'DAY') ;
fragment MONTH : ('m'|'M'|'Mon'|'Month'|'mon'|'month') ;
fragment YEAR : ('y'|'Y'|'Year'|'year'|'Yr'|'yr') ;

I only called FORMATE in parser with SINGLE_QUOTE after it which refer to ' in Lexer



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source