[antlr-interest] [SPAM] [SPAM] Re: [SPAM] [SPAM] Optional expression rule alternative

Bart Kiers bkiers at gmail.com
Fri Feb 11 10:34:05 PST 2011


On Fri, Feb 11, 2011 at 7:04 PM, Olivier Lefevre <lefevrol at yahoo.com> wrote:

> Great! Not to abuse but would you know in which section of
> "The Definitive ANTLR Reference" this mentioned? I couldn't
> find it. What is this syntax called?
>

It's called a "validating semantic predicate" which is explained in chapter
12 and 13.


But in my real-world problem it was actually not a choice
> between rule1 and rule2 but a question of whether to allow
> an extra alternative in the z case without too much
> verbatim repetition, i.e. (again in pseudo-code):
>
> rule[boolean z] :
>   rule1 | rule2 | ... | rule23 | (z ? rule24 : nothing) ;
>
> That is, allow rule24 if z otherwise stop at rule23.


You can still do that:

rule [boolean z]
  :  {z}? (rule1 | rule2 | rule3 | ... | rule23 | rule24)
  |       (rule1 | rule2 | rule3 | ... | rule23)
  ;


Regards,

Bart.


More information about the antlr-interest mailing list