[英]How to match a fixed number of characters in ANTLR 3?
I want to parse ISO 8601 dates in my ANTLR grammar.我想在我的 ANTLR 语法中解析 ISO 8601 日期。
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 match one or more with DIGIT+
and zero or more with DIGIT*
我知道我可以用
DIGIT+
匹配一个或多个,用DIGIT*
匹配零个或多个
While this works, is there a simpler syntax to specify I want to match exactly 2 DIGIT
?虽然这可行,但是否有更简单的语法来指定我想完全匹配 2
DIGIT
?
Jarrod Roberson wrote:
贾罗德·罗伯逊写道:
While this works, is there a simpler syntax to specify I want to match exactly 2 DIGIT?
虽然这可行,但是否有更简单的语法来指定我想完全匹配 2 DIGIT?
No, DIGIT DIGIT
is the only way to match exactly two digits.不,
DIGIT DIGIT
是精确匹配两位数字的唯一方法。 ANTLR does not support something like DIGIT{2}
, unfortunately.不幸的是,ANTLR不支持
DIGIT{2}
类的东西。
I'm pretty sure ANTLR 3 has no quantifiers besides *
, +
and ?
我很确定 ANTLR 3 除了
*
、 +
和?
之外没有量词. .
DIGIT DIGIT DIGIT DIGIT
seems like the most reasonable way to get the behavior you want. DIGIT DIGIT DIGIT DIGIT
似乎是获得所需行为的最合理方法。
See http://www.antlr.org/wiki/display/ANTLR3/Grammars见http://www.antlr.org/wiki/display/ANTLR3/Grammars
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.