I want to parse ISO 8601 dates in my ANTLR grammar.
2001-05-03
I have the following entries in my grammar file:
date : FOUR_DIGIT ('-')? TWO_DIGIT ('-')? TWO_DIGIT ;
FOUR_DIGIT
: TWO_DIGIT TWO_DIGIT ;
TWO_DIGIT
: DIGIT DIGIT ;
DIGIT : ('0'..'9') ;
I know I can 开发者_C百科match one or more with DIGIT+
and zero or more with DIGIT*
While this works, is there a simpler syntax to specify I want to match exactly 2 DIGIT
?
Jarrod Roberson wrote:
While this works, is there a simpler syntax to specify I want to match exactly 2 DIGIT?
No, DIGIT DIGIT
is the only way to match exactly two digits. ANTLR does not support something like DIGIT{2}
, unfortunately.
I'm pretty sure ANTLR 3 has no quantifiers besides *
, +
and ?
. DIGIT DIGIT DIGIT DIGIT
seems like the most reasonable way to get the behavior you want.
See http://www.antlr.org/wiki/display/ANTLR3/Grammars
精彩评论