开发者

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

开发者 https://www.devze.com 2023-01-04 00:26 出处:网络
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,

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 "tok开发者_Python百科ena tokena tokenb"?

Many thanks


... Is there anyway I can say this token can be there 1 or less times but not more than one? ...

Here's how:

someRule 
  :  TOKENA? TOKENB
  ;

or:

someRule 
  :  TOKENA TOKENB
  |  TOKENB
  ;
0

精彩评论

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