导入多个语法时错误的解析树

时间:2016-11-02 19:16:07

标签: antlr antlr4

我有关于数字和日期的语法:

数字语法:

    grammar numbers;
    import DateLexer;
    spelled_first_to_thirty_first:
        int_01_to_31_optional_prefix (TH | ND |RD|ST)
        ;
    int_01_to_31_optional_prefix
        : int_01_to_12
        | int_1_to_5
        | int_6_to_9
        | int_13_to_23
        | int_24_to_31
        ;
    int_13_to_23
        :(('1')('3'|'4'|'5'|int_6_to_9))|(('2')('0'|'1'|'2'|'3'))
        ;
    int_01_to_12 
        :(('0')(int_1_to_5|int_6_to_9))|(('1')('0'|'1'|'2'))
        ;
    int_1_to_5
        : '1'|'2'|'3'|'4'|'5'
        ;
    int_6_to_9
        :'6' | '7' | '8' | '9'
        ;
    int_24_to_31
        :(('2')('4'|'5'|int_6_to_9))|(('3')('0'|'1'|'2'))
        ;

日期词法分析器

        lexer grammar datelex;
        JANUARY   : 'january'   's'?  | 'jan' DOT? ;
        FEBRUARY  : 'february'  's'?  | 'feb' DOT?;
        MARCH     : 'march'     'es'? | 'mar' DOT?;
        APRIL     : 'april'     's'?  | 'apr' DOT?;
        MAY       : 'may'       's'?;
        JUNE      : 'june'      's'?  | 'jun' DOT?;
        JULY      : 'july'      's'?  | 'jul' DOT?;
        AUGUST    : 'august'    's'?  | 'aug' DOT?;
        SEPTEMBER : 'september' 's'?  | 'sep' DOT? | 'sept' DOT?;
        OCTOBER   : 'october'   's'?  | 'oct' DOT?;
        NOVEMBER  : 'november'  's'?  | 'nov' DOT?;
        DECEMBER  : 'december'  's'?  | 'dec' DOT?;

日期语法:

        grammar Date;
        import dateLex,numbers;
        d:day_of_month;

        month
        : JANUARY
        | FEBRUARY
        | MARCH
        | APRIL
        | MAY
        | JUNE
        | JULY
        | AUGUST
        | SEPTEMBER
        | OCTOBER
        | NOVEMBER
        | DECEMBER
        ;

        day_of_month
        : spelled_first_to_thirty_first 'of'? month;

首先我用15th测试数字语法,然后我得到了这个解析树:

(r (spelled_first_to_thirty_first (int_01_to_31_optional_prefix (int_13_to_23 1 5)) th))

似乎没错,但是当我用15th of sep测试日期语法时,我得到了这个解析树:

(d (day_of_month (spelled_first_to_thirty_first (int_01_to_31_optional_prefix (int_1_to_5 1)) 5) of (month sep)))

这是错误的,因为它将15中的1和5分开并将其识别为数字1。  为什么会这样?我该怎么做才能解决这个问题?

0 个答案:

没有答案