How to create a antlr4 grammar which will parse date

后端 未结 3 1910
一生所求
一生所求 2020-12-21 15:58

I want to parse few date format using following ANTLR4 grammar.

grammar Variables;
//varTable : tableNameFormat dateFormat? ;
//tableNameFormat: (ID SEPERATO         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-21 16:19

    In my case it works. I am getting a correct parsetree with the input: 2016-01-01

    grammar date;
    
    dateFormat : year UNDERSCORE month UNDERSCORE today
           | year
           ;
    
    year : DIGIT DIGIT DIGIT DIGIT
         ;
    
    month : DIGIT DIGIT
          ;
    
    today : DIGIT DIGIT 
          ;
    
    UNDERSCORE: ('_' | '-' );
    DIGIT : [0-9] ;
    

    But I would use for month something like (0 [1-9] | 1 [0-2]) because there are only 12 months.

提交回复
热议问题