ANTLR:找不到符号

who*_*o93 5 java parsing antlr javac

我有一个名为“Test.g4”的 ANTLR 项目,我使用 antlrworks2 创建的文件没有任何问题:Test.tokens、TestBaseListner.java、TestLexer.java、TestLexer.tokens、TestListener.java 和 TestParser.java。

现在我想在我的程序 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
    }
}
Run Code Online (Sandbox Code Playgroud)

当我尝试使用它进行编译时,"javac -classpath /path/java2/antlr-4.4-complete.jar Test.java"出现以下错误:

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
Run Code Online (Sandbox Code Playgroud)

谢谢!

man*_*uti 2

TestLexer.java并且TestParser.java还应该在同一命令中进行编译Test.java,否则编译器将不知道在哪里查找其二进制文件。尝试javac按如下方式调用:

javac -classpath /path/java2/antlr-4.4-complete.jar *java
Run Code Online (Sandbox Code Playgroud)

或者手动传递所有文件:

javac -classpath /path/java2/antlr-4.4-complete.jar Test.java TestLexer.java TestParser.java
Run Code Online (Sandbox Code Playgroud)