[antlr-interest] problem with semantic predicates, local declarations, and hoisting

Thomas Brandon tbrandonau at gmail.com
Wed Aug 8 08:22:51 PDT 2007


On 8/9/07, Madden, Warren <warren.madden at lmco.com> wrote:
>
>
>
>
> Greetings!
>
>
>
> I'm trying to write a grammar that recognizes a particular data format that
> includes some fixed length and some variable length fields.  I'm running
> into problems with my semantic predicates being hoisted up, and moving out
> of the context of my local variables.  Hoping someone can point me in the
> right direction to solve my problem.
>
This is a known bug\issue (not sure which, i.e. whether it will be
fixed). Use a field as you suggest, or (probably cleaner) a dynamic
scope instead. Something like:
> mandatory_child
scope {
    int n;
}
@init { mandatory_child::n = 0; }
>
>             :           ( { mandatory_child::n <= 17 }?=>
>
>                         ~(CR|LF)
>
>                         { mandatory_child::n++; })+
>
>                         {System.out.println("text is " + $text + ", n is " +
> mandatory_child::n );}
>
>                         {mandatory_child::n == 12 || mandatory_child::n == 18}?;
>
> It appears that ANTLR is taking my predicate and moving it into a DFA, but
> not carrying the declaration of n along with it.  Any ideas how I can fix
> this?  Should I make n a class variable using @members, rather than using
> @init as shown in the example?
>
>
>
> Thanks!
>
>
>
> Warren Madden


More information about the antlr-interest mailing list