在 Antlr4 中找不到符号

Cannot find symbol in Antlr4

这是我的 grammar file 这是我的 SimpleBaseListner.java 文件

// Generated from Simple.g4 by ANTLR 4.7.1
import org.antlr.v4.runtime.tree.ParseTreeListener;

/**
 * This interface defines a complete listener for a parse tree produced by
 * {@link SimpleParser}.
 */
public interface SimpleListener extends ParseTreeListener {
/**
 * Enter a parse tree produced by {@link SimpleParser#file}.
 * @param ctx the parse tree
 */
void enterFile(SimpleParser.FileContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#file}.
 * @param ctx the parse tree
 */
void exitFile(SimpleParser.FileContext ctx);
/**
 * Enter a parse tree produced by {@link SimpleParser#func}.
 * @param ctx the parse tree
 */
void enterFunc(SimpleParser.FuncContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#func}.
 * @param ctx the parse tree
 */
void exitFunc(SimpleParser.FuncContext ctx);
/**
 * Enter a parse tree produced by {@link SimpleParser#arg}.
 * @param ctx the parse tree
 */
void enterArg(SimpleParser.ArgContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#arg}.
 * @param ctx the parse tree
 */
void exitArg(SimpleParser.ArgContext ctx);
/**
 * Enter a parse tree produced by {@link SimpleParser#body}.
 * @param ctx the parse tree
 */
void enterBody(SimpleParser.BodyContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#body}.
 * @param ctx the parse tree
 */
void exitBody(SimpleParser.BodyContext ctx);
/**
 * Enter a parse tree produced by {@link SimpleParser#block}.
 * @param ctx the parse tree
 */
void enterBlock(SimpleParser.BlockContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#block}.
 * @param ctx the parse tree
 */
void exitBlock(SimpleParser.BlockContext ctx);
/**
 * Enter a parse tree produced by {@link SimpleParser#var}.
 * @param ctx the parse tree
 */
void enterVar(SimpleParser.VarContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#var}.
 * @param ctx the parse tree
 */
void exitVar(SimpleParser.VarContext ctx);
/**
 * Enter a parse tree produced by {@link SimpleParser#stat}.
 * @param ctx the parse tree
 */
void enterStat(SimpleParser.StatContext ctx);
/**
 * Exit a parse tree produced by {@link SimpleParser#stat}.
 * @param ctx the parse tree
 */
void exitStat(SimpleParser.StatContext ctx);
}

当我使用 javac SimpleBaseListner.java 编译它时,我得到以下内容
错误:

-bash-4.1$ javac SimpleListener.java
./SimpleParser.java:84: error: cannot find symbol
                public Scope scope;
                       ^
symbol:   class Scope
location: class FileContext
./SimpleParser.java:163: error: cannot find symbol
                public Scope scope;
                       ^
symbol:   class Scope
location: class FuncContext
./SimpleParser.java:353: error: cannot find symbol
                public Scope scope;
                       ^
symbol:   class Scope
location: class BlockContext
3 errors

我是Antlr4新手,不知道这里有什么问题。虽然说找不到符号,但语法文件中定义了此类型范围。因此据我理解应该在Antlr4编译语法文件的时候定义。

有人可以帮助我吗?

您缺少 Scope 源文件。在获取语法文件的存储库中找到它:https://github.com/parrt/cs652/search?q=Scope.java&unscoped_q=Scope.java(有多个 Scope 实现)

这一个,例如:

// source: https://github.com/parrt/cs652/blob/master/labs/symtab-mono/src/symtab/BasicScope.java
package symtab;

import java.util.HashMap;
import java.util.Map;

public class BasicScope implements Scope {
    public Map<String,Symbol> symbols = new HashMap<>();

    @Override
    public String getScopeName() {
        return "<unknown>";
    }

    @Override
    public void define(Symbol s) {
        symbols.put(s.name, s);
    }

    @Override
    public Symbol resolve(String name) {
        Symbol s = symbols.get(name);
        if ( s!=null ) return s;
        if ( getEnclosingScope()!=null ) {
            return getEnclosingScope().resolve(name);
        }
        return null;
    }

    @Override
    public Scope getEnclosingScope() {
        return null;
    }
}

属于接口:

// source: https://github.com/parrt/cs652/blob/master/labs/symtab-mono/src/symtab/Scope.java
public interface Scope {
    String getScopeName();
    void define(Symbol s);
    Symbol resolve(String name); // bind or lookup
    Scope getEnclosingScope();
}

您可能需要的其他 类 可以在这里找到:https://github.com/parrt/cs652/tree/master/labs/symtab-mono/src/symtab