[antlr-interest] turning rules on/off?

Jim Idle jimi at temporal-wave.com
Fri Jul 15 21:22:12 PDT 2011


You want gated semantic predicates to turn on rules. However, that is the
wrong approach in this case. You should accept and parse both sides even
though only one is legal, then reject the code with a semantic error
rather than a syntax error, which would confuse the user.  You get "cannot
both precede and follow a dfdd with a gffgfgf" instead of "Unexpected
'gffgfgf'"

Jim

> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Roy Metzger
> Sent: Thursday, July 14, 2011 4:12 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] turning rules on/off?
>
> Hi all,
>
> Let's say I have something like this:
>
> rule: ( 'stuff' e1=expression)?
> statement
> ('stuff' e2=expression)? ;
>
> Now, I would like to know is there a way to dynamically flip rules
> on/off. For example, when
> e1 is present e2 is not, and when e2 exists, e1 does not.
>
>
> Based on my research I found that predicates may be of help to me(of
> course, aside from refactoring), but
>
> I'm really not sure how to use them, as I'm still very new to antlr.
>
>
> Could something like this work:
> rule
> @init{boolean b=false;}:
>
> ( 'stuff' e1=expression{b=true;})?   {b}?=>expression statement (
> 'stuff' e2=expression{b=false;})?   {!b}?=>expression
>
> I would really appreciate if some of more experienced users would give
> me hints on this one.
>
> Thanks for any help
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> email-address


More information about the antlr-interest mailing list