use*_*837 5 java parse-tree antlr4
我想构造一个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;
}
Run Code Online (Sandbox Code Playgroud)