好吧,这可能很简单,但无论我申请多少咖啡,我的大脑都没有激活。
我需要将以下模式与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。
(一旦我得到正确的规则,我会将定义下移到Lexer定义中)
更新 由于先前提供的输入,以下规则集有效。再次感谢。
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 IDEA现在正在抛出警告,例如:
Decision can match input such as "INTEGER MONTH" using multiple alternatives: 1, 2
杰森
答案 0 :(得分:1)
下面是一个简单的语法,可以解析日期,就像你描述的那样。请注意,新行字符非常重要/不会被跳过,因为“4年/ n4个月”应该明确地解析为两个日期。
grammar dates;
options {
language = Java;
output = AST;
}
parse
: '\n'* date ('\n'+ date)* '\n'* EOF
;
date
// start w/ a duration. Returned value will be a Joda Period object
: year month? day?
{System.out.println(String.format("\%dy \%dm \%dd", $year.num, $month.num, $day.num));}
| month day?
{System.out.println(String.format("0y \%dm \%dd", $month.num, $day.num));}
| day
{System.out.println(String.format("0y 0m \%dd", $day.num));}
;
year returns [int num]
: INTEGER YEAR
{$num = $INTEGER.int;}
;
month returns [int num]
: INTEGER MONTH
{$num = $INTEGER.int;}
;
day returns [int num]
: INTEGER DAY
{$num = $INTEGER.int;}
;
DAY : 'DAY' | 'DAYS'
;
MONTH
: 'MONTH' | 'MONTHS'
;
YEAR: 'YEAR' | 'YEARS'
;
INTEGER
: '0'
| ('1'..'9')('0'..'9')*
;
WS
: ('\t' | ' ' | '\r') {skip();}
;
测试输入:
5 YEARS
5 YEARS 2 MONTHS
5 YEARS 2 MONTHS 3 DAYS
2 MONTHS 3 DAYS
5 YEARS 3 DAYS
7 DAYS
1 MONTH
测试输出:
5y 0m 0d
5y 2m 0d
5y 2m 3d
0y 2m 3d
5y 0m 3d
0y 0m 7d
0y 1m 0d