[antlr-interest] Fwd: "Exotic" grammar rule

Jim Idle jimi at temporal-wave.com
Tue Apr 24 12:32:21 PDT 2012


It is a semantic predicate, not a gated semantic predicate (though it
probably should be a gated version). It is [possibly] better written:

onestar
     :    { input.LA(2) != STAR }?=>  STAR?
     |
     ;

However it smacks very much of a weakness in the grammar itself in that it
is trying to force context on to a syntax rule. Without seeing the rest of
the grammar, it is difficult to see - can you point to the original
grammar?

Jim



> -----Original Message-----
> From: antlr-interest-bounces at antlr.org [mailto:antlr-interest-
> bounces at antlr.org] On Behalf Of Kevin Cummings
> Sent: Tuesday, April 24, 2012 11:02 AM
> To: Christian Lerch
> Cc: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] Fwd: "Exotic" grammar rule
>
> Yes, the code in the {}? Is a gated semantic predicate.  If the
> expression inside evaluates to true, then the alternative is attempted
> to be matched.  If the expression is false, then the alternative is
> never tried.
>
> So, for the code you are looking at, if the 2nd look ahead character is
> *not* a STAR, then it is OK to match an optional STAR character as the
> next input character.  This guarantees that if you *do* match a STAR,
> there is only one to match (and not two of them).
>
> --
> Kevin J. Cummings
> kjchome at verizon.net
> cummings at kjchome.homeip.net
> cummings at kjc386.framingham.ma.us
> Registered Linux User #1232
> (http://www.linuxcounter.net/)
>
>
> On Apr 24, 2012, at 4:53, Christian Lerch <christian.lerch at km-works.eu>
> wrote:
>
> >
> > On some other website I found the following interesting ANTRL grammar
> rule:
> >
> > onestar
> >     :    ( { input.LA(2) != STAR }?  ( STAR )?)
> >     |
> >     ;
> >
> > Can anybody explain its precise meaning to me?
> >
> > Cheers,
> > Chris
> >
> >
> >
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe: http://www.antlr.org/mailman/options/antlr-
> interest/your-email-address
>
> 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