[antlr-interest] Problems with semantic predicates

Haralambi Haralambiev hharalambiev at gmail.com
Fri Apr 18 07:21:03 PDT 2008


Hi Thomas,

Thanks a bunch!

That helped me solve my problem. I
feel like a total newbie. I didn't consider that rewrite rules could
have alternatives.

Best Regards,
Hari

On 4/18/08, Thomas Brandon <tbrandonau at gmail.com> wrote:
>
> http://fisheye2.cenqua.com/changelog/antlr?cs=4048 It's not in 3.0.1
> but is in the latest 3.1 daily builds (I can only assume it is fully
> implemented now, I haven't tried it).
> But if as your example suggests you just want to alter the AST
> construction not the actual parsing then I think you want predicated
> rewrite rules not semantic predicates (as I believe Jim was suggesting
> though I think he gave the wrong syntax). Like:
>
> r2[boolean flag]
>   : Token1
>    -> {$flag}? ^(NODE Token1)
>    -> Token1
>
> ;
> I imagine these won't be hoisted and should only be run after any
> backtracking so you shoudln't have any of your other troubles.
>
> Tom.
>
> On Fri, Apr 18, 2008 at 10:12 PM, Haralambi Haralambiev
> <hharalambiev at gmail.com> wrote:
> > Hello,
> >
> > Is there a way to lure Ter's interest to answer whether this braces
> (sorry
> > for the mistake ;-) ) functionality is in place?
> >
> > I am using the latest version, 3.0.1, that is downloadable from the
> site, so
> > I guess it's not implemented :-(
> >
> > Hari
> >
> >
> >
> > On 4/18/08, Johannes Luber <jaluber at gmx.de> wrote:
> > > Haralambi Haralambiev schrieb:
> > >
> > > > Hi Johannes,
> > > >
> > > > I don't understand what's the benefit of the two brackets.
> > > >
> > > > When I write {{$r::flag = false;}} the code that ANTLR produces is
> > "{((r_scope)r_stack.peek()).flag = false;}", i.e. the brackets are added
> > also, but they are still in "if (backtracking == 0)" statement, which is
> the
> > check I want to skip somehow.
> > > >
> > > > Hari
> > > >
> > >
> > > The two braces (brackets are "[]") should be the syntax which enables
> an
> > action to be executed always, even during backtracking. But either you
> are
> > using an too old version of ANTLR or Ter didn't implement this variant
> after
> > all.
> > >
> > > Johannes
> > >
> > >
> > > >
> > > > On 4/17/08, *Johannes Luber* <jaluber at gmx.de <mailto:jaluber at gmx.de
> >>
> > wrote:
> > > >
> > > >    Haralambi Haralambiev schrieb:
> > > >
> > > >        Hello Johannes,
> > > >
> > > >        So, if I understand correctly, you suggest to have the
> following
> > > >        (using a scope):
> > > >
> > > >        r2
> > > >        : {$r::flag}? Token1 -> ^(NODE Token1)
> > > >        | Token1
> > > >        ;
> > > >
> > > >        If the above is a correct assumption, then how can I set the
> > > >        scope variable, as I could only think of the following way:
> > > >
> > > >        r
> > > >        scope {
> > > >         boolean flag;
> > > >        }
> > > >        : Token1^ Token2!
> > > >           {$r::flag = true;}    r2
> > > >        | Token1^
> > > >         {$r::flag = false;}
> > > >         r2
> > > >        ;
> > > >
> > > >        This solution, however, will fail, as the action for setting
> the
> > > >        scope variable will not be executed until the backtracking is
> > > >        over. But I need this variable set, as it will be used as a
> > > >        predicate for the parsing. Thus, we have a catch-22
> situation.
> > > >
> > > >        A solution I could think of is not to have two alternatives
> for
> > > >        r2, but rather construct the CommonTree manually with java
> code
> > > >        and within to use the knowledge of the boolean flag. But I
> > > >        dislike this solution and am open to suggestions.
> > > >
> > > >
> > > >    Try using {{$r::flag = true;}}.
> > > >
> > > >    Johannes
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20080418/252426c3/attachment.html 


More information about the antlr-interest mailing list