[antlr-interest] optional rule refs

Terence Parr parrt at cs.usfca.edu
Thu Aug 23 09:19:39 PDT 2007


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


More information about the antlr-interest mailing list