这是我的语法文件,这是我的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的新手,我不知道这里出了什么问题。虽然说找不到符号,但这种类型的 Scope 是在语法文件中定义的。因此,据我了解,应该在 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