我正在尝试使用ANTLR4进行项目,并且正在关注此tutorial和此question。
以下是我的Drink.java文件
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import static org.antlr.v4.runtime.CharStreams.fromFileName;
public class Drink {
public static void main (String [] args) {
CharStream cs = fromFileName("test.txt"); //load the file
DrinkLexer lexer = new DrinkLexer(cs); //instantiate a lexer
CommonTokenStream tokens = new CommonTokenStream(lexer); //scan stream for tokens
DrinkParser parser = new DrinkParser(tokens); //parse the tokens
ParseTree tree = parser.drinkSentence(); // parse the content and get the tree
DrinkWalker listener = new DrinkWalker();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(listener,tree);
}
}
以下是我的DrinkWalker.java文件
public class DrinkWalker extends DrinkBaseListener {
@Override public void enterEveryRule(ParserRuleContext ctx) {
System.out.println("rule entered: " + ctx.getText());//code that executes per rule
}
public void enterR(DrinkParser.RContext ctx ) {
System.out.println( "Entering R : " + ctx.ID().getText() );
}
public void exitR(DrinkParser.RContext ctx ) {
System.out.println( "Exiting R" );
}
}
我手动编译它们,但出现以下错误:
Override public void enterEveryRule(ParserRuleContext ctx) {
^
symbol: class ParserRuleContext
location: class DrinkWalker
DrinkWalker.java:5: error: cannot find symbol
public void enterR(DrinkParser.RContext ctx ) {
^
symbol: class RContext
location: class DrinkParser
DrinkWalker.java:9: error: cannot find symbol
public void exitR(DrinkParser.RContext ctx ) {
^
symbol: class RContext
location: class DrinkParser
上面提到的RContext和ParserRuleContext函数是什么?