ANTLR4语法,规则只接受句子的一部分

时间:2016-09-01 19:55:51

标签: parsing antlr grammar antlr4 context-free-grammar

所有。我已经创建了语法(它是更大语法的一部分)来发现我的问题

当我解析字符串

  

00 * / 3,5 * * 5 America / New_York

我有以下异常

  

第1:4行输入'/'

没有可行的选择

正如我所发现的,问题是substring / 3,5使用“with_step_value”规则完全解析,而不是解析器只获取第一个sybmbol。但为什么?据我所知,antlr尝试解析尽可能长的字符串并在我的视图中将子字符串“ / 3,5”置于满足规则“with_step_value”

那么,为什么会发生这种情况以及如何解决它?

此致 弗拉基米尔

请参阅下面的语法和图片

/*File trigger validator lexer */
lexer grammar CronPartLexer;


INT_LIST: INTEGER (COMMA INTEGER)* ;
INTERVAL
:
    INTEGER DASH INTEGER
;

INTEGER
:
    [0-9]+
;

DASH
:
    '-'
;


SLASH
:
    '/'
;

COMMA
:
    ','
;

UNDERSCORE
:
    '_'
;


ID
:
    [a-zA-Z] [a-zA-Z0-9]*
;

ASTERISK:'*';

WS
:
    [ \t\r\n]+ -> skip
; 


grammar CronPartValidator;

options
   {
    tokenVocab = CronPartLexer;
}

cron_part
:
    minutes hours days_of_month month week_days time_zone?;

    minutes
:
    with_step_value
;


time_zone
:
    timezone_part
    (
        SLASH timezone_part
    )?
;

timezone_part
:
    ID
    (
        UNDERSCORE ID
    )?
;


hours
:
    with_step_value
;
//

//

days_of_month
:
    with_step_value
;
//

month
:
    with_step_value
;
//

week_days
:
    with_step_value
;


with_step_value:
    INT_LIST|ASTERISK|INTERVAL ((SLASH INT_LIST)?)  
;

Parse Tree of the full string

Parse Tree of "with_step_value" "*/3,5"

1 个答案:

答案 0 :(得分:0)

规则

with_step_value: INT_LIST|ASTERISK|INTERVAL ((SLASH INT_LIST)?) ;

仅匹配INT_LISTASTERISKINTERVAL (SLASH INT_LIST)?

也许这就是预期的目的:

with_step_value
    : ( INT_LIST
      | ASTERISK
      | INTERVAL
      ) (SLASH INT_LIST)?
    ;

enter image description here

相关问题