[antlr-interest] should finally actions be put in the try-finally section?

Terence Parr parrt at cs.usfca.edu
Wed Jan 3 13:37:01 PST 2007


Hi,

  check out the following rule that generates a template

prog
@finally {
// number of elements on stack is roughly number of operations + 1
// and add one for the address of the System.out object
%{$st}.maxStackDepth = new Integer(numOps+1+1);
}
     :   (s+=stat)+ -> jasminFile(instructions={$s})
     ;

note that there is an action that I want to be done after the  
template has been created by the rewrite rule so I put it in the  
finally action.  However, it actually goes into the finally{} of the  
try block for the rule, meaning that it will always be executed even  
if an exception is thrown in the rule.  If an exception is thrown in  
the rule, the template will not be generated and this action will  
throw a null pointer exception.  By a quirk of Java, the original  
exception is thrown away and you get the new null pointer exception,  
hiding the old one.  Very very mysterious and difficult to track down.

I am suggesting that we move where @finally actions are placed in the  
output to simply "after code for all alternatives has been generated"  
rather than in the actual finally clause of the rule.  The only  
negative is that you cannot then specify something that must be  
executed regardless of exceptions in the rule. We could split this  
into a @finally action and an @after action or something like that.  
Anybody have an opinion?

Ter


More information about the antlr-interest mailing list