gpt4 book ai didi

java - Antlr4.6 BaseVisitor 和 Visitor 错误 : Visitor (Interface) is abstract; cannot be instantiated BaseVisitor (Class) & super() error

转载 作者:行者123 更新时间:2023-12-02 13:14:23 29 4
gpt4 key购买 nike

我正在尝试制作antrl4 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 -> YocoVisitor 是接口(interface),并且接口(interface)无法实例化。代替 YocoVisitor eval = new YocoVisitor(dictInt, dictProcD);必须在 YocoBaseVisitor 中编写并创建相应的构造函数YocoVisitor eval = new YocoBaseVisitor(dictInt, dictProcD);

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

错误 3 -> 方法 VisitR 被正确检查语法覆盖。

希望能够解决所有问题。

关于java - Antlr4.6 BaseVisitor 和 Visitor 错误 : Visitor (Interface) is abstract; cannot be instantiated BaseVisitor (Class) & super() error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43828878/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com