[antlr-interest] ClassCastException with custom node type

Ale Strooisma alestrooisma at gmx.net
Thu Jul 5 07:44:25 PDT 2012


Hi,

my teacher found the problem: I didn't override the dupNode method in the CommonTree-subclass, only in the adaptor.

Thanks for your help anyways.

Kind regards, Ale

-------- Original-Nachricht --------
> Datum: Thu, 05 Jul 2012 12:18:31 +0200
> Von: "Stefan Mätje \\"(d)\\"" <Stefan.Maetje at esd-electronics.com>
> An: antlr-interest at antlr.org
> Betreff: Re: [antlr-interest] ClassCastException with custom node type

> Am 05.07.2012 11:22:55 schrieb(en) Ale Strooisma:
> > Thanks for your reply,
> > 
> > I didn't see much difference between your and my code, and when I added 
> > the line "nodes.setTokenStream(tokens);" the problem was still there. 
> > Please see my code below.
> > 
> > Kind Regards, Ale
> 
> I don't see much difference also. Perhaps the difference comes from the
> tree 
> adaptor. I'll paste my adaptor below. Maybe that helps?
> 
> -----8<-----------8<-----------8<-----------8<---------8<---------8<--------
> import org.antlr.runtime.*;
> import org.antlr.runtime.tree.CommonTreeAdaptor;
> 
> /** A tree adaptor that tells ANTLR to build Pearl90Tree nodes. Derived
> from
>     http://www.antlr.org/wiki/display/ANTLR3/Tree
> +construction#Treeconstruction-UsingcustomASTnodetypes
>     */
> public class Pearl90TreeAdaptor extends CommonTreeAdaptor {
> 
>     public Object create(Token token) {
>         return new Pearl90Tree(token);
>     }
> 
>     /** Of the following create() methods only the third is used atm. by
> the
>         program. The apparently unused ones are added to the
> implementation 
> but
>         print some information. All are used but 1 & 2 triggered by hand
> from
>         parser only.
>         */
>     public Object create(int tokenType, Token fromToken) {
>         fromToken = createToken(fromToken);
>         fromToken.setType(tokenType);
>         // System.err.println("1 Create node"+fromToken.toString());
>         return this.create(fromToken);
>     }
> 
>     public Object create(int tokenType, Token fromToken, String text) {
>         if (fromToken == null) return create(tokenType, text);
>         fromToken = createToken(fromToken);
>         fromToken.setType(tokenType);
>         fromToken.setText(text);
>         // System.err.println("2 Create node"+fromToken.toString());
>         return this.create(fromToken);
>     }
> 
>     public Object create(int tokenType, String text) {
>         Token fromToken = createToken(tokenType, text);
>         // System.err.println("3 Create node"+fromToken.toString()+text
> +tokenType);
>         return this.create(fromToken);
>     }
> 
>     public Object dupNode(Object t) {
>         if (null == t) {
>             return null;
>         }
>         return create(((Pearl90Tree)t).token);
>     }
> 
>     public Object errorNode(TokenStream input, Token start, Token stop,
>         RecognitionException e)
>     {
>         Pearl90ErrorNode t = new Pearl90ErrorNode(input, start, stop, e);
>         System.out.println("returning error node '"+t+"'
> @index="+input.index
> ());
>         return t;
>     }
> 
> }
> -----8<-----------8<-----------8<-----------8<---------8<---------8<--------
> 
> > TreeAdaptor adaptor = new ASTNodeAdaptor();
> > 
> > System.out.println("Starting Lexer... ");
> > WcLexer lexer = new WcLexer(new ANTLRInputStream(fileInputStream));
> > TokenStream tokens = new CommonTokenStream(lexer);
> > 
> > System.out.println("Starting Parser... ");
> > WcParser parser = new WcParser(tokens);
> > parser.setTreeAdaptor(adaptor);
> > WcParser.program_return result = parser.program();
> > ASTNode tree = (ASTNode) result.getTree();
> > 
> > System.out.println("Checking Context... ");
> > CommonTreeNodeStream nodes = new CommonTreeNodeStream(adaptor, tree);
> > nodes.setTokenStream(tokens);
> > WcChecker checker = new WcChecker(nodes,f);
> > checker.program();
> > 
> > 
> > On 07/05/2012 10:05 AM, Stefan Mätje wrote:
> > > Hi,
> > >
> > > the attachment seems to be stripped by the mailing list. I'll put it
> now
> > > directly in the message.
> > >
> > > My classes are
> > >       Pearl90Tree        a tree node
> > >       Pearl90TreeAdaptor    the node factory?
> > >       Pearl90ErrorNode    the error node object
> > >
> > > Code excerpt follows, sorry for line wraps:
> > > {
> > >       // ==== Prepare tree node adaptor for later use =========
> > >       Pearl90TreeAdaptor pearl90Adaptor = new Pearl90TreeAdaptor();
> > >
> > >       // SNIP-SNAP
> > >
> > >       // ==== PARSER STAGE ====================================
> > >       // Create a stream of tokens fed by the lexer
> > >       // But use TokenRewriteStream if we need to change the AST
> > >       // with a tree grammar later. See ANTLR Ref p. 224
> > >       CommonTokenStream tokens = new CommonTokenStream(lexer);
> > >
> > >       // Create a parser that feeds off the token stream
> > >       parser = new Pearl90Parser(tokens);
> > >
> > >       // Use my own tree adaptor to build Pearl90Tree nodes in the
> AST.
> > >       parser.setTreeAdaptor(pearl90Adaptor);
> > >
> > >       // Begin parsing at rule "file" and get return value
> > >       Pearl90Parser.file_return r = parser.file();
> > >       // Change the returned value to an AST
> > >       Pearl90Tree ast = (Pearl90Tree)r.getTree();
> > >
> > >       // SNIP-SNAP
> > >
> > >       // ---------------------------------------
> > >       // Do error accounting ...
> > >       // SNIP-SNAP
> > >
> > >       // ==== Symbol Table Generation =========================
> > >       // This stage fills the symbol table and may rewrite the AST
> > >       // based on its findings.
> > >       // CommonTreeNodeStream nodes = new CommonTreeNodeStream(ast);
> > >       CommonTreeNodeStream nodes = new
> > > CommonTreeNodeStream(pearl90Adaptor, ast);
> > >       nodes.setTokenStream(tokens);
> > >       // This line fixed crash on spurious CommonToken insertions.
> > > Already done
> > >       // now by constructor above.
> > >       //nodes.setTreeAdaptor(pearl90Adaptor);
> > >
> > >       SymbolTable symtab = new SymbolTable();
> > >       // Use my custom constructor for AST tree parser
> > >       // Use workaround with new for missing debug output stream...
> > >       Pearl90Sym syms = new Pearl90Sym(nodes, symtab, new
> > > PrintWriter(System.out,true));
> > >
> > >       // Use my own tree adaptor to build Pearl90Tree nodes in the
> AST.
> > >       syms.setTreeAdaptor(pearl90Adaptor);
> > >
> > >       // Trigger symtab actions upon certain subtrees,
> > >       // return the rewritten AST as directed by Pearl90Sym.g
> > >       ast = (Pearl90Tree)syms.downup(ast);
> > >
> > >
> > > }
> > >
> > > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > > Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-
> > email-address
> > >    
> > 
> > 
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-
> > address
> > 
> > 
> 
> 
> 
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list