how to check an ANTLR token is only used once or less in the parser

Posted by Simon Kenyon Shepard on Stack Overflow See other posts from Stack Overflow or by Simon Kenyon Shepard
Published on 2010-06-15T10:32:04Z Indexed on 2010/06/15 10:32 UTC
Read the original article Hit count: 275

Filed under:
|
|
|

In Antlr, if I have a rule for example:

someRule : TOKENA TOKENB;

it would accept : "tokena tokenb"

if I would like TOKENA to be optional, I can say,

someRule : TOKENA* TOKENB;

then I can have : "tokena tokenb" or "tokenb" or "tokena tokena tokenb"

but this also means it can be repeated more that once. Is there anyway I can say this token can be there 1 or less times but not more than one? so it would accept:

"tokena tokenb" or "tokenb" BUT NOT "tokena tokena tokenb"?

Many thanks

© Stack Overflow or respective owner

Related posts about parser

Related posts about antlr