好的,這可能很簡單,但無論我申請多少咖啡,我的大腦都不會激活。ANTLR匹配日期範圍
我需要與ANTLR匹配以下模式:
5 YEARS
5 YEARS 2 MONTHS
5 YEARS 2 MONTHS 3 DAYS
2 MONTHS 3 DAYS
5 YEARS 3 DAYS
etc
於是,我開始了以下規則:
atom returns [Object value]
// start w/ a duration. Returned value will be a Joda Period object
: (INTEGER ('YEAR'|'YEARS'))? (INTEGER ('MONTH'|'MONTHS'))? (INTEGER ('DAY'|'DAYS')?
;
顯然是行不通的。我知道這很簡單,但我現在有一個主要的大腦404。
(一旦我得到了統治權,我就倒在詞法定義移動的定義)
更新: 以下規則集的作品,由於輸入先前提供的。再次感謝。
datePeriod returns [Object value]
: year month? week? day? EOF
{
$value = new Period($year.num, $month.num, $week.num, $day.num,0,0,0,0);
}
| month week? day? EOF
{
$value = new Period(0, $month.num, $week.num, $day.num,0,0,0,0);
}
| week day? EOF
{
$value = new Period(0,0, $week.num, $day.num,0,0,0,0);
}
| day EOF
{
$value = new Period(0, 0, 0, $day.num,0,0,0,0);
}
;
year returns [int num]
: INTEGER YEAR
{
$num = $INTEGER.int;
}
;
month returns [int num]
: INTEGER MONTH
{
$num = $INTEGER.int;
}
;
week returns [int num]
: INTEGER WEEK
{
$num = $INTEGER.int;
}
;
day returns [int num]
: INTEGER DAY
{
$num = $INTEGER.int;
}
;
YEAR: ('YEAR'|'YEARS');
MONTH: ('MONTH'|'MONTHS');
WEEK: ('WEEK'|'WEEKS');
DAY: ('DAY'|'DAYS');
不幸的是,我ANTLR想法正折騰出警告,如:
Decision can match input such as "INTEGER MONTH" using multiple alternatives: 1, 2
傑森
這是一個錯字,我固定它。事實是,它必須是0或1年/年,0或1個月/月,和/或0或1天/天。它必須至少有一個令牌,但可以有三個。 – Jason