[antlr-interest] optional rule refs

Gavin Lambert antlr at mirality.co.nz
Thu Aug 23 12:41:37 PDT 2007


At 04:19 24/08/2007, Terence Parr wrote:
 >		^('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.

To remove some of the branching, ANTLR could remember whether a 
given label is used within an optional construct or not.  (Where 
"optional construct" means a ? block, * block, or an alt that 
doesn't assign it -- basically whether any path on the DFA exists 
between rule start and the point of label use where the label may 
not be assigned.)  That way it would only need to inject the null 
check if it knows that null is a possibility.

This is probably going a little overboard, though -- and the 
difference will most likely be negligible unless you're processing 
megabytes of source files.



More information about the antlr-interest mailing list