2

我确实找到了我的问题的答案:Barts 的答案正是我需要的原因,但它不起作用(见下文)。

请有人可以给我一个工作示例或告诉我实施 Barts答案的问题所在吗?

这是我得到的另一个答案,我从“第 4 部分”中得到以下错误

Lexer lexer = (Lexer)Class.forName(grammarName + "Lexer").newInstance();

我有完整的 antlr3.4 和 JDK 库。我创建了它,package createclass;但我发现答案很奇怪,因为要创建的类没有包???所以我尝试添加到字符串:

"@lexer::header {\n" +
"   package createclass;\n" +
"}        \n" +
"@parser::header {\n" +
"   package createclass;\n" +
"}\n" +

但仍然没有任何变化。

这是输出:

debug:
Exception in thread "main" java.lang.ClassNotFoundException: TLexer
    at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:169)
    at createclass.Createclass.main(Createclass.java:61)
Java Result: 1
BUILD SUCCESSFUL (total time: 31 seconds)

给出的示例代码是:

import java.io.*;
import javax.tools.*;
import java.lang.reflect.*;
import org.antlr.runtime.*;
import org.antlr.Tool;

public class Main {

    public static void main(String[] args) throws Exception {

        // The grammar which echos the parsed characters to theconsole,
        // skipping any white space chars.
        final String grammar =
                "grammar T;                                                  \n" +
                "                                                            \n" +
                "parse                                                       \n" +
                "  :  (ANY {System.out.println(\"ANY=\" + $ANY.text);})* EOF \n" +
                "  ;                                                         \n" +
                "                                                            \n" +
                "SPACE                                                       \n" +
                "  :  (' ' | '\\t' | '\\r' | '\\n') {skip();}                \n" +
                "  ;                                                         \n" +
                "                                                            \n" +
                "ANY                                                         \n" +
                "  :  .                                                      \n" +
                "  ;                                                           ";
        final String grammarName = "T";
        final String entryPoint = "parse";

        // 1 - Write the `.g` grammar file to disk.
        Writer out = new BufferedWriter(new FileWriter(new File(grammarName + ".g")));
        out.write(grammar);
        out.close();

        // 2 - Generate the lexer and parser.
        Tool tool = new Tool(new String[]{grammarName + ".g"});
        tool.process();

        // 3 - Compile the lexer and parser.
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        compiler.run(null, System.out, System.err, "-sourcepath", "", grammarName + "Lexer.java");
        compiler.run(null, System.out, System.err, "-sourcepath", "", grammarName + "Parser.java");

        // 4 - Parse the command line parameter using the dynamically created lexer and 
        //     parser with a bit of reflection Voodoo :)
        Lexer lexer = (Lexer)Class.forName(grammarName + "Lexer").newInstance();
        lexer.setCharStream(new ANTLRStringStream(args[0]));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        Class<?> parserClass = Class.forName(grammarName + "Parser");
        Constructor parserCTor = parserClass.getConstructor(TokenStream.class);
        Parser parser = (Parser)parserCTor.newInstance(tokens);
        Method entryPointMethod = parserClass.getMethod(entryPoint);
        entryPointMethod.invoke(parser);
    }
}
4

1 回答 1

2

我的猜测是您为此使用了 IDE,并且没有正确设置类路径。我的答案中的演示有效(也使用 v3.4)。这是我刚刚做的:

我将文件复制到Main.java一个目录中(~/Temp/demo在我的例子中是 )并在这个目录中打开了一个 shell 并执行了以下操作:

bart@hades:~/Temp/demo$ java -version
> java 版本“1.6.0_24”
> OpenJDK 运行环境 (IcedTea6 1.11.4) (6b24-1.11.4-1ubuntu0.12.04.1)
> OpenJDK 64-Bit Server VM(build 20.0-b12,混合模式)

bart@hades:~/Temp/demo$ wget http://www.antlr.org/download/antlr-3.4-complete.jar
> --2012-10-17 19:26:01-- http://www.antlr.org/download/antlr-3.4-complete.jar
> 解决www.antlr.org (www.antlr.org)... 138.202.170.10
> 连接到 www.antlr.org (www.antlr.org)|138.202.170.10|:80... 已连接。
> HTTP 请求已发送,等待响应... 200 OK
> 长度:2388361 (2.3M) [应用程序/java-archive]
> 保存到:`antlr-3.4-complete.jar'
>
> 100%[============================================== ==================================================== ===============>] 2,388,361 317K/s 在 8.9s    
>
> 最后修改的标头无效——时间戳被忽略。
> 2012-10-17 19:26:11 (261 KB/s) - `antlr-3.4-complete.jar' 已保存 [2388361/2388361]

bart@hades:~/Temp/demo$ javac -cp antlr-3.4-complete.jar *.java

bart@hades:~/Temp/demo$ java -cp .:antlr-3.4-complete.jar Main "ab c"
> 任何=一个
> 任何=b
> 任何=c

>输出打印到控制台)

于 2012-10-17T17:33:16.720 回答