[antlr-interest] C++ code, help with custom AST initialize() method

Bryan Ewbank ewbank at gmail.com
Tue Apr 11 12:59:16 PDT 2006


I've run into a problem that I can't seem to get my mind around...

    - ANTLR 2.7.5
    - C++ codegen
    - Custom AST class

I've defined a custom AST class, TreeNode, derived from
antlr::CommonAST, and am using the associated smart-pointer class in
all my grammars.

The problem comes when ANTLR generates code to initialize RefTreeNode objects:
     RefTreeNode id = RefTreeNode(antlr::nullAST);
Since antlr::nullAST is /not/ a TreeNode, this is effectively an
incorrect cast from a base class to a derived class.

Later on, when "id" is used in the TreeNode::initialize() function, it
dies because the object pointed to be antlr::nullAST does not have the
correct information from which to completely initialize a TreeNode. 
It only has the stuff for a CommonAST.

So, how am I supposed to handle the fact that something which is an
antlr::nullAST may be used where an object of type RefTreeNode is
expected?

If this sounds at all familiar, please let me know what you did to get
around this problem.

Thank you,
- Bryan Ewbank


More information about the antlr-interest mailing list