开发者

How to match a fixed number of characters in ANTLR 3?

开发者 https://www.devze.com 2023-03-02 01:17 出处:网络
I want to parse ISO 8601 dates in my ANTLR grammar. 2001-05-03 I have the following entries in my grammar file:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消