Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ANTLR: Could not find symbol

I have a ANTLR project called "Test.g4" and with antlrworks2 I created without any problems the files: Test.tokens, TestBaseListner.java, TestLexer.java, TestLexer.tokens, TestListener.java and TestParser.java.

Now I want to use the grammer in my program Test.java:

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class Test {
    public static void main(String[] args) throws Exception {
        // create a CharStream that reads from standard input
        ANTLRInputStream input = new ANTLRInputStream(System.in);

        // create a lexer that feeds off of input CharStream
        TestLexer lexer = new TestLexer(input);

        // create a buffer of tokens pulled from the lexer
        CommonTokenStream tokens = new CommonTokenStream(lexer);

        // create a parser that feeds off the tokens buffer
        TestParser parser = new TestParser(tokens);

        ParseTree tree = parser.init(); // begin parsing at init rule
        System.out.println(tree.toStringTree(parser)); // print LISP-style tree
    }
}

When I try to compile it with "javac -classpath /path/java2/antlr-4.4-complete.jar Test.java" I get this errors:

Test.java:19: error: cannot find symbol
        TestLexer lexer = new TestLexer(input);
        ^
  symbol:   class TestLexer
  location: class Test
Test.java:19: error: cannot find symbol
        TestLexer lexer = new TestLexer(input);
                                        ^
  symbol:   class TestLexer
  location: class Test
Test.java:25: error: cannot find symbol
        TestParser parser = new TestParser(tokens);
        ^
  symbol:   class TestParser
  location: class Test
Test.java:25: error: cannot find symbol
        TestParser parser = new TestParser(tokens);
                                          ^
  symbol:   class TestParser
  location: class Test
4 errors

Thank you!

like image 690
who93 Avatar asked Nov 01 '22 11:11

who93


1 Answers

TestLexer.java and TestParser.java should also be compiled with Test.java in the same command, otherwise the compiler will not know where to look for their binaries. Try calling javac as follows:

javac -classpath /path/java2/antlr-4.4-complete.jar *java

Or manually pass all files:

javac -classpath /path/java2/antlr-4.4-complete.jar Test.java TestLexer.java TestParser.java
like image 189
M A Avatar answered Nov 04 '22 07:11

M A