Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 我可以吗;转换;ANTLR中的ParseTree到CommonTree?_Java_Antlr4_Parse Tree - Fatal编程技术网

Java 我可以吗;转换;ANTLR中的ParseTree到CommonTree?

Java 我可以吗;转换;ANTLR中的ParseTree到CommonTree?,java,antlr4,parse-tree,Java,Antlr4,Parse Tree,我想构造ParseTree的子树,但唯一包含insertChild(index,Object)方法的类似乎是CommonTree,我无法传递RuleContext类型的对象,因为它尝试将其转换为Tree对象,我得到了“java.lang.ClassCastException:Java8Parser$ClassDeclarationContext无法强制转换为org.antlr.runtime.tree.tree”异常 有什么想法吗?谢谢。(编辑:添加代码) private void getSub

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

有什么想法吗?谢谢。(编辑:添加代码)

private void getSubtrees(ParseTree树){
CommonTree CommonTree=新建CommonTree();
SetRecursiveSubjects(commonTree、tree);
}
私有CommonTree集合递归子体(CommonTree CommonTree,ParseTree ParseTree){
int n=parseTree.getChildCount();
对于(int i=0;i
如果您有异常,您显然有一些代码。请编辑您的问题以包含到目前为止的内容,好吗?我已经添加了codeCommonTree是ANTLR3的一个类。您的语法是ANTLR3吗?我使用的是ANTLR4,而我的语法是ANTLR4。我在ANTLR4中找不到公共树。您能给出一个关于哪个包/工件的提示吗?
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;
}