1

好吧,这可能很简单,但无论我涂抹多少咖啡,我的大脑都不会激活。

我需要将以下模式与 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

杰森

4

1 回答 1

1

下面是一个简单的语法,可以解析您描述的日期。请注意,换行符是重要的/不被跳过,因为“4 YEARS\n4 MONTHS”应该明确地解析为两个日期。

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
于 2012-11-20T16:20:06.413 回答