[antlr-interest] ClassCastException with custom node type

Stefan Mätje Stefan.Maetje at esd-electronics.com
Thu Jul 5 01:05:01 PDT 2012


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);


}


More information about the antlr-interest mailing list