我有一个名为" 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
}
}
当我尝试用"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
谢谢!
答案 0 :(得分:2)
TestLexer.java
和TestParser.java
也应该在同一个命令中使用Test.java
进行编译,否则编译器将不知道在哪里查找其二进制文件。尝试按以下方式致电javac
:
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 :(得分:0)
您需要构建和导入由ANTLR生成的词法分析器和解析器。要做到这一点,你需要: