[antlr-interest] Rule-local variables?
Vaclav Barta
vbar at comp.cz
Tue Jul 3 02:55:24 PDT 2007
Gavin Lambert wrote:
> At 18:58 3/07/2007, Vaclav Barta wrote:
> >That works, but I'd also like to have conditions depending on how
> >ANTLR got to a rule and its action. Negation is a typical example:
> [...]
> >How do I know in (B) and (C) that I've passed through (A) (and
> >therefore have to generate a NOT LIKE operator and a negated
> >expression)?
> Probably the easiest way is to declare a local boolean variable
> initialised to false at the top of your rule, and then set it to true if
> the NOT clause gets matched. Then you can test that boolean later on.
>
> For most targets you can do this with @init blocks -- for C I think you
Yes, much better - thanks.
> need a @declarations block too. Alternatively, if you want the variable
> to be accessible outside the rule itself, then you can use scopes.
Well, I certainly don't want recursive rules using a single variable,
and considering I also have no idea what an ANTLR scope is, I think I'll
go with a local variable in an @init block...
Bye
Vasek
More information about the antlr-interest
mailing list