[antlr-interest] How to handle single and plural (i.e. 'day' or 'days')

Colin Yates colin.yates at gmail.com
Tue Oct 26 04:34:30 PDT 2010


Hi,

The user can type in '1 day' or '7 days'.  I have the following rule which
matches:

durationPeriod: ('day' | 'days') { // do something};

and this works fine.  I initially tried:

durationPeriod: 'day'('s')? { // do something};

but this never matched 'days'.

Based on the '100 monkeys' reality I tried the following:

durationPeriod: ('day''s')? { // do something};
and
durationPeriod: ('day')('s')? { // do something};

but none of them matched 'days'.  The exact error is [line 1:2 no viable
alternative at input 'days'']

What have I missed?  The (incredibly useful!) Syntax Diagram in antlworks
shows that the trailing 's' can be skipped, so I don't understand why it
doesn't work.

Many thanks,

Col


More information about the antlr-interest mailing list