[antlr-interest] optional rule refs

Johannes Luber jaluber at gmx.de
Thu Aug 23 09:39:55 PDT 2007


Terence Parr wrote:
> Howdy,
> 
> When obtained results from optional rule refs in template generation,
> you can get a null ptr:
> 
>         ^('if' completeExpression s=statement (el=statement)?)
>         -> if(cond={$completeExpression.st}, stat={$s.st},
> elsestat={$el.st})
> 
> here, el is optional and hence null sometimes.  $el.st will cause an
> exception.  There is no way to test $el by itself as it needs a
> property.  There is no other token/node to test either.
> 
> I propose that all refs to rule properties become the equivalent of:
> 
> $rr!=null?$rr.property:null
> 
> It causes a lot of branching unnecessarily when rr is not null, but
> makes life much easier.  A number of people have run into this.  I
> suppose if it's too "inefficient" for some, they can tweak the templates
> to remove the ?: operation.
> 
> Comments?
> 
> Ter
> 

I like this. I've looked over my uses and I used either your pattern or
implemented my own. For the first your proposal simplifies things and
the other cases don't seem do be affected negatively.

Best regards,
Johannes Luber


More information about the antlr-interest mailing list