在Antlr4中找不到符号

时间:2018-09-13 05:21:13

标签: java 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编译语法文件时对其进行定义。

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:1)

您缺少Scope源文件。在获得语法文件的存储库中找到它:String#matches(有多个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/search?q=Scope.java&unscoped_q=Scope.java