[antlr-interest] Custom AST Types

Robert Colquhoun rjc at trump.net.au
Fri Jun 13 00:54:33 PDT 2003


At 09:41 AM 12/06/2003 -0700, Terence Parr wrote:
>Hi Robert.  Can u summarize the basic problem in the code for me?

Protected rules that legally can match an empty AST ie ASTNULL in 
combination with the tree walker using a custom ast type will throw a 
ClassCastException when run.

Looking a genRule() at line 2510 in JavaCodeGenerator.java the passed in 
AST _t is saved in a variable #statement_in which is defined to the custom 
AST type specified.

Most of the time for ordinary ASTs this works fine, occasionally ASTNULL 
gets passed to the method ASTNULL is of type ASTNULLType and cannot be 
assigned to your custom ast type and the whole thing explodes with a 
ClassCastException

The 1 line patch just checks to see if ASTNULL is about to be assigned and 
substitutes null to #statement_in

ie
         MyAST #statement_in = (MyAST)_t;
changes to:
         MyAST #statement_in = (_t == ASTNULL) ? null: (MyAST)_t;

- Robert


>Thanks,
>Ter
>
>On Thursday, June 12, 2003, at 08:05  AM, Robert Colquhoun wrote:
>
> > Hello,
> >
> > Following up my own post, i have a patch of antlr-2.7.2 which fixes the
> > problem:
> >          http://www.trump.net.au/~rjc/antlr/
> > It(customast.diff) was a one line patch to JavaCodeGenerator.java, the
> > 2nd
> > solution outlined below was used, possibly the same problem is in the
> > C++
> > and C# generators, but it is late and i couldn't be bothered checking
> > ;-)
> >
> > Also at the bottom of the page is a compiled antlr.jar which has the 3
> > listed patches on the page.
> >
> >   - Robert
> >
> > At 12:25 PM 12/06/2003 +1000, Robert Colquhoun wrote:
> >> Hi Everyone,
> >>
> >> Is anyone else having problems in tree walkers using custom AST types
> >> in
> >> conjunction with protected rules?
> >>
> >> I get ClassCastExceptions whenever the rule tries to match nothing ie
> >> ASTNULL
> >>
> >> ie
> >>
> >> options{
> >>      ASTLabelType=MyAST;
> >> }
> >> ....
> >> protected elsest: (#(ELSE statementblock))?
> >>
> >> The code generated has something like:
> >>
> >>   protected final void elsest(antlr.collections.AST _t) throws
> >> antlr.RecognitionException {
> >>
> >>       MyAST elsest_AST_in = (MyAST)_t;  //<---ClassCastException here!
> >>       .....
> >>
> >> What appears to happen is that the elsest() rule is called passing
> >> ASTNULL
> >> when there is no else clause as part of a if/then/else statement and
> >> it
> >> trys to cast ASTNULL to MyAST type which breaks things.
> >>
> >> To fix
> >>         - either somehow change ASTNULL to be of MyAST type.
> >>         - as in other parts of generated code use something like:
> >>                 MyAST elsest_AST_in = (_t == ASTNULL) ? null :
> >> (MyAST)_t;
> >>
> >> Both require changes to antlr to solve though, otherwise i can hack my
> >> grammar to work around this i think.....just want to know if it is
> >> affecting other people at all, and thus whether i should fix antlr?
> >>
> >>   - Robert


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 




More information about the antlr-interest mailing list