[antlr-interest] ClassCastException with custom node type

Ale Strooisma alestrooisma at gmx.net
Thu Jul 5 02:22:55 PDT 2012


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


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
>    



More information about the antlr-interest mailing list