1

I am trying out a project using ANTLR4 and I'm following this tutorial and this question.

The following is my Drink.java file

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

and the following is my DrinkWalker.java file

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

I compiled them manually, and I get the following errors:

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

What are the RContext and ParserRuleContext functions referred above?

Joel Murphy
  • 845
  • 1
  • 6
  • 13
Nht_e0
  • 140
  • 4
  • 15
  • 1
    This is not a duplicate question. The previous referenced question was regarding Java in general. This question is about ANTLR. – Joel Murphy Oct 10 '18 at 19:16
  • These particular errors are happening because you need to add the antlr JAR file to your classpath. The example on the homepage of http://www.antlr.org/ shows one example. You'll have to adjust the path based on where yours is installed. – Joel Murphy Oct 10 '18 at 19:30

0 Answers0