简体   繁体   中英

Cannot find symbol in Antlr4

This is my grammar file and this is my SimpleBaseListner.java file

// 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);
}

When I compile this using javac SimpleBaseListner.java I get the following
errors:

-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

I am a newbie to Antlr4, and I don't know what is wrong here. Although is says cannot find symbol, this type Scope is defined in the grammar file. Hence to my understanding it should be defined when Antlr4 compiles the grammar file.

Can someone help me?

You're missing a Scope source file. Find it in the repository where you got the grammar file: https://github.com/parrt/cs652/search?q=Scope.java&unscoped_q=Scope.java (there are multiple Scope implementations)

This one, for example:

// 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;
    }
}

belonging to the interface:

// 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();
}

Other classes you might need can be found here: https://github.com/parrt/cs652/tree/master/labs/symtab-mono/src/symtab

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM