Antlr 导入多个语法时出现错误的分析树

Antlr 导入多个语法时出现错误的分析树,antlr,antlr4,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 |

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

数字语法:

    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;
日期词汇语法:

    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;
日期语法:

    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(拼写为_first _至_third _first(int _01 _至_31 _可选_前缀(int _13 _至_231 5))th))

这似乎是对的,但当我用9月15日的
15th
测试日期语法时,我得到了这个pars树:

(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

为什么会这样?我应该怎么做才能解决这个问题?

这似乎有点复杂。也许只要写下解析器应该解析的内容。我已经用antlr编写了一到两个解析器,需要解析日期,但并没有这么多…我觉得我一直在重复我自己的每一个antlr问题,但这里是这样的:让您的解析器尽可能简单。解析器需要处理语法规则。然后,与访问者一起实现验证过程,以检查语义规则。基本上:写一个
number
规则,在解析后检查数字范围。这看起来有点复杂。也许只要写下解析器应该解析的内容。我已经用antlr编写了一到两个解析器,需要解析日期,但并没有这么多…我觉得我一直在重复我自己的每一个antlr问题,但这里是这样的:让您的解析器尽可能简单。解析器需要处理语法规则。然后,与访问者一起实现验证过程,以检查语义规则。基本上:编写一条
number
规则,并在解析后检查数字范围。