[antlr-interest] Rule-local variables?

Johannes Luber jaluber at gmx.de
Tue Jul 3 04:43:27 PDT 2007


Vaclav Barta wrote:
> Fine by me... A related question: is it better to say
> 
> subExpression returns [ Expression value ] :
>     { $value = new Expression(); }
>     ( o = unaryOperator { $value.Operator = $o.value; } )? (
>     ...
>     
> or
> 
> subExpression returns [ Expression value ]
> @init { $value = new Expression(); } :
>     ( o = unaryOperator { $value.Operator = $o.value; } )? (
>     ...
> 
> Both seem to work...
> 
>     Bye
>         Vasek
> 

In this particular case the compiled code works the same, but the latter
documents the intent clearer. Furthermore, if you have several
alternatives in a rule, the first variant would require also code
duplication - a violation of the DRY principle, which can cause
maintenance nightmares.

Best regards,
Johannes Luber


More information about the antlr-interest mailing list