ANTLR: Could not find character

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

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

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 the following 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!

+4
source share
2 answers

TestLexer.javaand TestParser.javamust also be compiled using Test.javathe same command, otherwise the compiler will not know where to look for their binaries. Try calling javacas follows:

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

:

javac -classpath /path/java2/antlr-4.4-complete.jar Test.java TestLexer.java TestParser.java
+1

, ANTLR. :

  • , ANTLR , import
  • ,
0

Source: https://habr.com/ru/post/1570757/


All Articles