Antlr: The easiest way to find out dates and numbers?

What is the simplest (shortest, smallest rule and no warning) way to analyze both real dates and numbers in one grammar? My problem is that the lexer rule to match a valid month (1-12) will match any occurrence of 1-12. Therefore, if I just want to match the number, I need a parsing rule, for example:

number: (MONTH|INT);

It only gets complicated when I add lexer rules for day and year. I want a date syntax rule:

date: month '/' day ( '/' year )? -> ^('DATE' year month day);

I don’t care if the month, day and year are the rules of parsing or lexer, only as long as I get the same tree structure. I should also be able to recognize numbers elsewhere, for example:

foo: STRING OP number -> ^(OP STRING number);
STRING: ('a'..'z')+;
OP: ('<'|'>');
+5
2

, , , / . -, .

, , , . , , , , : 1) INT, 2) DATENUM , , INT. , ( INTs , . :

INT: '0'..'9'+;

:

DATENUM: '0' '1'..'9';
INT: '0' | SIGN? '1'..'9' '0'..'9'*;

:

date: INT '/' INT ( '/' INT )?

date: (INT | DATENUM) '/' (INT | DATENUM) ('/' (INT | DATENUM) )?

, , .

, , ANTLR , , :

date: month=INT '/' day=INT ( year='/' INT )? { year==null ? (/* First check /*) : (/* Second check */)}

, , , .

+5

ANTLR4, , . , .

// parser rules

date 
    : INT SEPARATOR month SEPARATOR INT
    | INT SEPARATOR month SEPARATOR INT4
    | INT SEPARATOR INT SEPARATOR INT4;

month : JAN | FEB | MAR | APR | MAY | JUN | JUL | AUG | SEP | OCT | NOV | DEC ;

number : FLOAT | INT | INT4 ;

// lexer rules

FLOAT : DIGIT+ '.' DIGIT+ ;

INT4 : DIGIT DIGIT DIGIT DIGIT;
INT : DIGIT+;

JAN : [Jj][Aa][Nn] ;
FEB : [Ff][Ee][Bb] ;
MAR : [Mm][Aa][Rr] ;
APR : [Aa][Pp][Rr] ;
MAY : [Mm][Aa][Yy] ; 
JUN : [Jj][Uu][Nn] ;
JUL : [Jj][Uu][Ll] ;
AUG : [Aa][Uu][Gg] ;
SEP : [Ss][Ee][Pp] ; 
OCT : [Oo][Cc][Tt] ; 
NOV : [Nn][Oo][Vv] ;
DEC : [Dd][Ee][Cc] ;

SEPARATOR : [/\\\-] ;

fragment DIGIT : [0-9];
0

All Articles