[antlr-interest] Rule-local variables?
Vaclav Barta
vbar at comp.cz
Mon Jul 2 23:58:44 PDT 2007
Hi,
skimming http://www.antlr.org/works/help/tutorial/calculator.html, I've
noticed ANTLR rules can be declared to return a value, which can be set
in the rule's actions, often from results produced by lower-level rules
- say
whereClause returns [ IExpression value ] :
WHERE c = searchCondition { $value = $c.value; }
;
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:
predicate returns [ IExpression value ] :
( NOT { (A) } )?
( LIKE ... { (B) }
| e = expression { (C) }
)
;
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)? I can do
predicate returns [ IExpression value ] :
NOT {
$value = new Expression();
((Expression)$value).Operator = ExpressionOperator.Not;
} ( LIKE ... { if ($value == null) ... else ... }
| e = expression { if ($value == null) ... else ... }
)
;
but in more complicated cases, when nested branches store their internal
state into the same variable, it gets confusing and ugly pretty fast -
so I'd like to have a local flag meaning "this expression has been
negated" and nothing else. How can I do that in ANTLR?
Bye
Vasek
More information about the antlr-interest
mailing list