我可以在ANTLR中将ParseTree“转换”为CommonTree吗?

时间:2015-05-08 06:33:00

标签: java antlr4 parse-tree

我想构造ParseTree的子树,但是唯一包含insertChild(index,Object)方法的类似乎是CommonTree,我无法传递RuleContext类型对象,因为它尝试将其转换为Tree对象,并且我得到“java.lang.ClassCastException:Java8Parser $ ClassDeclarationContext无法强制转换为org.antlr.runtime.tree.Tree”异常。

任何想法?谢谢。 (编辑:添加代码)

private void getSubtrees(ParseTree tree){
    CommonTree commonTree = new CommonTree();
    setRecursiveDescendants(commonTree, tree);
}

private CommonTree setRecursiveDescendants(CommonTree commonTree,ParseTree parseTree){
    int n = parseTree.getChildCount();
        for (int i = 0; i < n; i++) {
            commonTree.insertChild(i, parseTree.getChild(i);
            setRecursiveDescendants(commonTree, parseTree.getChild(i));
        }
        return commonTree;
}

0 个答案:

没有答案