[antlr-interest] Rule-local variables?

Gavin Lambert antlr at mirality.co.nz
Tue Jul 3 01:39:35 PDT 2007


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 need a @declarations block too.  Alternatively, if you 
want the variable to be accessible outside the rule itself, then 
you can use scopes.



More information about the antlr-interest mailing list