Antlr4.6 BaseVisitor和Visitor错误:访问者(接口)是抽象的;无法实例化BaseVisitor(Class)& super()错误

时间:2017-05-07 07:22:58

标签: java interface antlr visitor

我正在尝试制作atrl4 4.6。工作,但我得到了4个错误,这些错误是先进的,我无法通过它们。有人可以帮我吗?也许需要匿名课。

同样是super()错误,我把它放在正确的位置但它拒绝工作。

Yoco.java:34: error: YocoVisitor is abstract; cannot be instantiated
        YocoVisitor eval = new YocoVisitor(dictInt, dictProcD);
                           ^ YocoBaseVisitor.java:6: error: YocoBaseVisitor is not abstract and does not override abstract method visitR(RContext) in YocoVisitor public class YocoBaseVisitor extends AbstractParseTreeVisitor<Integer> implements YocoVisitor<Integer> {
        ^ YocoBaseVisitor.java:12: error: method does not override or implement a method from a supertype
        @Override public Integer visitR(HashMap<String, Integer> _dictInt, HashMap<String, YocoParser.ProcDefContext> _dictProcD) {
        ^ YocoBaseVisitor.java:13: error: call to super must be first statement in constructor
>                 super();
                    ^ 4 errors

YocoBaseVisitor.java:


// Generated from Yoco.g4 by ANTLR 4.6
import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
import org.antlr.v4.runtime.tree.ParseTreeVisitor;
import java.util.HashMap;
public class YocoBaseVisitor extends AbstractParseTreeVisitor<Integer> implements YocoVisitor<Integer> {
    HashMap<String, Integer> dictInt = new HashMap<String, Integer>();
    HashMap<String, YocoParser.ProcDefContext> dictProcD = new HashMap<String, YocoParser.ProcDefContext>();
    @Override public Integer visitR(HashMap<String, Integer> _dictInt, HashMap<String, YocoParser.ProcDefContext> _dictProcD) { 
        super();
        dictInt = _dictInt;
        dictProcD = _dictProcD;
    }
}

YocoVisitor.java:
{

import org.antlr.v4.runtime.tree.ParseTreeVisitor;
public interface YocoVisitor<Integer> extends ParseTreeVisitor<Integer> {
}


Yoco.java:

public class Yoco {
    public static void main(String[] args) throws Exception {
        String inputFile = null; 
        if(args.length > 0)
        inputFile = args[0];
        InputStream is = System.in;
        if (inputFile != null)
        is = new FileInputStream(inputFile);

        ANTLRInputStream input = new ANTLRInputStream(is);
        YocoLexer lexer = new YocoLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        YocoParser parser = new YocoParser(tokens);

        ParseTree tree = parser.r();
        System.out.println(tree.toStringTree(parser));

    // Hash table for integer variables.
    HashMap<String, Integer> dictInt = new HashMap<String, Integer>();

    // Hash table for procedure expressions with parameters.
    HashMap<String, YocoParser.ProcDefContext> dictProcD = new HashMap<String, YocoParser.ProcDefContext>();

    YocoVisitor eval = new YocoVisitor(dictInt, dictProcD);
    System.out.println();
    eval.visit(tree);
    }
}
}  

1 个答案:

答案 0 :(得分:1)

错误1 - &gt; YocoVisitor是接口,接口无法实例化。 代替  YocoVisitor eval = new YocoVisitor(dictInt,dictProcD); 必须在YocoBaseVisitor中编写和创建相应的构造函数 YocoVisitor eval = new YocoBaseVisitor(dictInt,dictProcD);

错误2 - &gt; super()只能在类的构造函数中作为第一行调用。你已经调用了方法。

错误3 - &gt;方法visitR被覆盖正确检查语法。

希望能解决所有问题。