编译 ANTLRWorks 生成的类文件

发布于 2024-07-09 09:37:47 字数 261 浏览 9 评论 0原文

我正在使用 ANTLRWorks 创建 ANTLR 语法。 我有一个有效的语法,并且还生成了解析器和词法分析器源文件。 我还尝试调试生成的代码,并且输出与调试器输出中的预期一致。

但是,当我尝试调用调试器生成的 __Test__ 类时,控制台中没有出现任何内容。 我已经正确设置了类路径,因为我可以使用相同的类路径成功编译 __Test__.java 。

会有什么问题呢? 有没有关于使用antlr和antlrworks编写和编译示例解析器的明确教程?

I am using ANTLRWorks to create ANTLR grammars. I have a valid grammar and the parser and lexer source files are generated as well. I have also tried debugging the generated code and the output is as expected in the debugger output.

But when I try to invoke the __Test__ class generated by the debugger nothing is coming up in the console. I have properly set up the classpath as I can successfully compile the __Test__.java with the same classpath.

What would be the problem? Is there any clear tutorial for writing and compiling a sample parser with antlr and antlrworks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

君勿笑 2024-07-16 09:37:47

您期望控制台上出现什么?

看看这个项目。 ANTLRWorks 生成的解析器位于此处。 从 POM 中的依赖关系可以看出,您需要确保 antlr 在类路径中。 然后使用解析器,如 此类

final DriftLexer lexer = new DriftLexer(new ANTLRInputStream(inputStream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);        
final DriftParser parser = new DriftParser(tokens);
parser.file();

这应该足以让你的东西正常工作。

What do you expect on the console to come up?

Have a look at this project. The ANTLRWorks generated parser is here. As you can see from the dependencies in the POM you need to make sure antlr is in the classpath. Then you use the parser as shown in this class.

final DriftLexer lexer = new DriftLexer(new ANTLRInputStream(inputStream));
final CommonTokenStream tokens = new CommonTokenStream(lexer);        
final DriftParser parser = new DriftParser(tokens);
parser.file();

That should be enough to get your stuff working as well.

蓝戈者 2024-07-16 09:37:47

ANTLRWorks 生成测试类,用于创建返回 ANTLRWorks 的套接字连接,因此它们无法从控制台使用。 您可以编辑生成的测试类以不使用调试端口(套接字连接)选项。

要编辑的行是:

FormalSpecParser g = new FormalSpecParser(tokens, 49100, null);

您可以将其更改为:

FormalSpecParser g = new FormalSpecParser(tokens, null);

它使用调试侦听器对象而不是端口,“null”意味着您没有为其提供调试侦听器,因此调试输出将被忽略。 您可以编写自己的调试侦听器以将消息打印到控制台。

有关详细信息,请参阅 ANTLR 文档:http://www.antlr.org/api/ Java/命名空间.html

ANTLRWorks generates test classes that create a socket connection back to ANTLRWorks, so they aren't usable from the console. You can edit the generated test class to not use the debug port (socket connection) option.

The line to edit is:

FormalSpecParser g = new FormalSpecParser(tokens, 49100, null);

You can change it to:

FormalSpecParser g = new FormalSpecParser(tokens, null);

which uses a debug listener object instead of a port, and the "null" means you're not giving it a debug listener, so debug output is ignored. You could write your own debug listener to print out messages to the console.

See the ANTLR documentation for more information: http://www.antlr.org/api/Java/namespaces.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文