Okay, this is probably simple, but no matter how much coffee I apply, my brain isn't activating.
I need to match the following patterns with ANTLR:
5 YEARS
5 YEARS 2 MONTHS
5 YEARS 2 MONTHS 3 DAYS
2 MONTHS 3 DAYS
5 YEARS 3 DAYS
etc
So, I started out with the following rule:
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')?
;
Obviously that isn't going to work. I know this is simple, but I'm having a major brain 404 at the moment.
(Once I get the rule right, I'll move the definition down to the Lexer definitions)
Update: The following ruleset works, thanks to input provided previously. Thanks again.
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');
Unfortunately, I'm ANTLR IDEA is now tossing out warnings such as:
Decision can match input such as "INTEGER MONTH" using multiple alternatives: 1, 2
Jason
Below is a simple grammar that can parse the dates as you've described them. Note that the new-line character is non-trivial/not skipped because "4 YEARS\\n4 MONTHS" should parse as two dates unambiguously.
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();}
;
Test input:
5 YEARS
5 YEARS 2 MONTHS
5 YEARS 2 MONTHS 3 DAYS
2 MONTHS 3 DAYS
5 YEARS 3 DAYS
7 DAYS
1 MONTH
Test output:
5y 0m 0d
5y 2m 0d
5y 2m 3d
0y 2m 3d
5y 0m 3d
0y 0m 7d
0y 1m 0d
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.