[antlr-interest] Generated code causes InvalidCastException

Johannes Luber jaluber at gmx.de
Sun Dec 30 07:39:30 PST 2007


Adam Connelly schrieb:
> Hi,
> 
> I'm trying to write a simple language to use for code generation.  So
> far I've got a working recogniser, and am working on a tree parser.  My
> parser grammar simply builds an AST and doesn't have any actions - I was
> going to do the work in the tree parser.  The problem I'm having is that
> I have a rule as follows:
> 
> project:    ^(PROJECT ID template+) { Console.WriteLine("project " +
> $ID.text + " {"); }
>     ;
> 
> PROJECT and ID are tokens from my parser grammar.  The grammar generates
> fine, but when I try to use the tree parser to parse an AST created by
> my parser, I get an InvalidCastException which says that it can't cast a
> CommonTree to a CommonToken.  The offending line is in project() and is
> as follows:
> 
> ID1 = (CommonToken)input.LT(1);
> 
> I'm using the runtime from the Antlr 3.0.1 Source Distribution and the
> C# target, and I created and generated both my parsers using ANTLRWorks
> 1.1.5.
> 
> Any help would be appreciated.

Did you add to the options in the tree grammar "ASTLabelType=CommonTree;"?

Johannes


More information about the antlr-interest mailing list