[antlr-interest] ANTLR NUB

Jan Nielsen jan.sture.nielsen at gmail.com
Tue Jan 29 10:11:47 PST 2008


So I rewrote the grammar with return values after implementing the
underlying APIs. I'm still having some basic issues with recognition,
however. I think the excludeClause and includeClause lists are not
being handled correctly - perhaps because of the comma?

I have included my test cases and grammar below. If you have any
insights about what I've done wrong, I'd be delighted to entertain
them. :)

Many thanks,

-Jan


test cases and std & stderr output:

available 1/January/2008
available 1/January/2008 to 1/January/2009
available 1/January/2008 to 1/January/2009, exclude 1/January/2008
available 1/January/2008 to 1/January/2009, exclude 21/January/2008
available 1/January/2008 to 1/January/2009, exclude Thursday to Sunday
available 1/January/2008 to 1/January/2009, exclude Monday, Wednesday, Friday
available 1/January/2008 to 1/January/2009, exclude Thursday(fourth)/November
available 1/January/2008 to 1/January/2009, exclude Thursday to
Sunday, include June to July
available 1/January/2008 to 1/January/2009, exclude Monday to
Thursday, include 21/January/2008
available 1/January/2008 to 1/January/2009, exclude
Tuesday(first)/November, Monday, 1/November/2008, include
Monday(third)/January, 12/February, 1/January
line 1:80 no viable alternative at input 'June'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
BR.recoverFromMismatchedToken
available 1/January/2008, exclude Monday(last)/May
available 1/January, exclude 1/January
available 1/January, exclude 21/January
line 1:19 mismatched input ',' expecting '/'
available 1/January, exclude Thursday to Sunday
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
available 1/January, exclude Thursday to Sunday, include
Thursday(fourth)/November to Sunday(fourth)/November
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
available 1/January, exclude Monday, Wednesday,
FridayBR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
line 1:57 no viable alternative at input 'June'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'
BR.recoverFromMismatchedToken
line 1:19 mismatched input ',' expecting '/'

available 1/January, exclude Thursday(fourth)/November
available 1/January, exclude Thursday to Sunday, include June to July
available 1/January, exclude Monday to Thursday, include 21/January/2008
available 1/January, exclude Monday to Thursday, include 21/January/2008
available 1/January, exclude Tuesday(first)/November, include
Tuesday(first)/November/2009
available 1/January, exclude Monday(third), include Monday(third)/January

grammar:

grammar Availability;

options{
    superClass = AvailabilityBaseParser;
}

program
    : availabilityClause
    ;

availabilityClause
    : 'available' durationClause (excludeClause includeClause?)?
    ;

durationClause
    : startDate endDateClause?
    ;

endDateClause
    : 'to' endDate
    ;

startDate
    : p=dayOfMonthInYearClause   { setStartDate($p.value); }
    ;

endDate
    : p=dayOfMonthInYearClause   { setEndDate($p.value); }
    ;

excludeClause
    : ',' 'exclude' p=period     { addExclude($p.value); }
        (',' p=period            { addExclude($p.value); })*
    ;

includeClause
    : ',' 'include' p=period     { addInclude($p.value); }
        (',' p=period            { addInclude($p.value); })*
    ;

period returns [adc.util.Date value]
    : r=range                    { $value = $r.value; }
    | d=date                     { $value = $d.value; }
    ;

range returns [adc.util.DateRange value]
    : s=date 'to' e=date         { $value = new
adc.util.DateRange($s.value,$e.value); }
    ;

date returns [adc.util.Date value]
    : d=dayOfMonthDateClause     { $value = $d.value; }
    | w=dayOfWeekDateClause      { $value = $w.value; }
    | m=monthDateClause          { $value = $m.value; }
    ;

dayOfWeekDateClause returns [adc.util.Date value]
    : d=dayOfWeek                { $value = new adc.util.Date($d.value); }
        (o=occurrenceClause      { $value.getDay().setSelector($o.value); })?
        (m=monthClause           { $value.setMonth($m.value); }
            (y=yearClause        { $value.setYear($y.value); })?)?
    ;

dayOfMonthDateClause returns [adc.util.Date value]
    : d=dayOfMonth m=monthClause { $value = new
adc.util.Date($d.value,$m.value); }
        (y=yearClause            { $value.setYear($y.value); })?
    ;

monthDateClause returns [adc.util.Date value]
    : m=monthClause              { $value = new adc.util.Date($m.value); }
        (y=yearClause            { $value.setYear($y.value); })?
    ;

dayOfMonthInYearClause returns [adc.util.Date value]
    : d=dayOfMonth m=monthClause y=yearClause
                                 { $value = new
adc.util.Date($d.value,$m.value,$y.value); }
    ;

dayClause returns [adc.util.Day value]
    : d=dayOfMonth               { $value = adc.util.Day.ANY;
$value.setSelector($d.value); }
    | w=dayOfWeekClause          { $value = $w.value; }
    ;

monthClause returns [adc.util.Month value]
    : '/' m=month                { $value = $m.value; }
    ;

yearClause returns [int value]
    : '/' y=year                 { $value = Integer.parseInt($y.text); }
    ;

dayOfWeekClause returns [adc.util.Day value]
    : d=dayOfWeek                { $value = $d.value; }
        (o=occurrenceClause      { $value.setSelector($o.value); })?
    ;

dayOfWeek returns [adc.util.Day value]
    : DAY_OF_WEEK                { $value = adc.util.Day.MONDAY; }
    ;
/*
    : 'Monday'                   { $value = adc.util.Day.MONDAY; }
    | 'Tuesday'                  { $value = adc.util.Day.TUESDAY; }
    | 'Wednesday'                { $value = adc.util.Day.WEDNESDAY; }
    | 'Thursday'                 { $value = adc.util.Day.THURSDAY; }
    | 'Friday'                   { $value = adc.util.Day.FRIDAY; }
    | 'Saturday'                 { $value = adc.util.Day.SATURDAY; }
    | 'Sunday'                   { $value = adc.util.Day.SUNDAY; }
    ;
*/

occurrenceClause returns [int value]
    : '(' o=occurrence ')'       { $value = $o.value; }
    ;

occurrence returns [int value]
    : OCCURRENCE                 { $value = 1; }
    ;
/*
    : 'first'                    { $value = 1; }
    | 'second'                   { $value = 2; }
    | 'third'                    { $value = 3; }
    | 'fourth'                   { $value = 4; }
    ;
*/

dayOfMonth returns [int value]
    : NUMBER                     { $value = Integer.parseInt($NUMBER.text); }
    ;

month returns [adc.util.Month value]
    : MONTH                      { $value = adc.util.Month.JANUARY; }
    ;
/*
    : 'January'                  { $value = adc.util.Month.JANUARY; }
    | 'February'                 { $value = adc.util.Month.FEBRUARY; }
    | 'March'                    { $value = adc.util.Month.MARCH; }
    | 'April'                    { $value = adc.util.Month.APRIL; }
    | 'May'                      { $value = adc.util.Month.MAY; }
    | 'June'                     { $value = adc.util.Month.JUNE; }
    | 'July'                     { $value = adc.util.Month.JULY; }
    | 'August'                   { $value = adc.util.Month.AUGUST; }
    | 'September'                { $value = adc.util.Month.SEPTEMBER; }
    | 'October'                  { $value = adc.util.Month.OCTOBER; }
    | 'November'                 { $value = adc.util.Month.NOVEMBER; }
    | 'December'                 { $value = adc.util.Month.DECEMBER; }
    ;
*/

year returns [int year]
    : NUMBER                     { $year = Integer.parseInt($NUMBER.text); }
    ;

DAY_OF_WEEK returns [adc.util.Day value]
    : 'Monday'                   { $value = adc.util.Day.MONDAY; }
    | 'Tuesday'                  { $value = adc.util.Day.TUESDAY; }
    | 'Wednesday'                { $value = adc.util.Day.WEDNESDAY; }
    | 'Thursday'                 { $value = adc.util.Day.THURSDAY; }
    | 'Friday'                   { $value = adc.util.Day.FRIDAY; }
    | 'Saturday'                 { $value = adc.util.Day.SATURDAY; }
    | 'Sunday'                   { $value = adc.util.Day.SUNDAY; }
    ;

MONTH returns [adc.util.Month value]
    : 'January'                  { $value = adc.util.Month.JANUARY; }
    | 'February'                 { $value = adc.util.Month.FEBRUARY; }
    | 'March'                    { $value = adc.util.Month.MARCH; }
    | 'April'                    { $value = adc.util.Month.APRIL; }
    | 'May'                      { $value = adc.util.Month.MAY; }
    | 'June'                     { $value = adc.util.Month.JUNE; }
    | 'July'                     { $value = adc.util.Month.JULY; }
    | 'August'                   { $value = adc.util.Month.AUGUST; }
    | 'September'                { $value = adc.util.Month.SEPTEMBER; }
    | 'October'                  { $value = adc.util.Month.OCTOBER; }
    | 'November'                 { $value = adc.util.Month.NOVEMBER; }
    | 'December'                 { $value = adc.util.Month.DECEMBER; }
    ;

OCCURRENCE returns [int value]
    : 'last'                     { $value = -1; }
    | 'first'                    { $value =  1; }
    | 'second'                   { $value =  2; }
    | 'third'                    { $value =  3; }
    | 'fourth'                   { $value =  4; }
    ;

NUMBER
    : DIGIT+
    ;

fragment DIGIT
    : '0'..'9'
    ;

WS  :  (' '|'\r'|'\t'|'\u000C'|'\n')
                                 { $channel=HIDDEN; }
    ;

COMMENT
    :   '/*' ( options {greedy=false;} : . )* '*/'
                                 { $channel=HIDDEN; }
    ;

LINE_COMMENT
    : '//' ~('\n'|'\r')* '\r'? '\n'
                                 { $channel=HIDDEN; }
    ;



On Jan 22, 2008 12:41 AM, Gavin Lambert <antlr at mirality.co.nz> wrote:
> At 19:25 22/01/2008, Jan Nielsen wrote:
>  >
>  >I initially envisioned having repeated exclusion and inclusion
>  >clauses but I don't think I need to support it now
>
> If you want them, it's pretty straightforward.  Just change the
> prog rule to this:
>
> prog
>      : 'from' date ('to' date)? (exclude_clause | include_clause)*
> EOF
>      ;
>
> This will permit any number of include and exclude clauses in any
> order.
>
>  >day_of_week_period
>  >    : DAY_OF_WEEK ('[' occurrence ']')? ('-' DAY_OF_WEEK)?
>  >    ;
>
> If "Monday[3]-Wednesday" doesn't make sense (and I'm not sure how
> it could), you could possibly change this to:
>
> day_of_week_period
>      : DAY_OF_WEEK ('[' occurrence ']' | '-' DAY_OF_WEEK)?
>      ;
>
> This will permit only one (or neither) of the two options to be
> specified.
>
>


More information about the antlr-interest mailing list