有谁知道在 ANTLRWorks 中调试树语法的方法 [英] Does anyone know of a way to debug tree grammars in ANTLRWorks

查看:23
本文介绍了有谁知道在 ANTLRWorks 中调试树语法的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ANTLR 使用的推荐模式是让解析器构建一个抽象语法树,然后构建树遍历器(又名树语法)来处理它们.

The recommended pattern for ANTLR usage is to have the Parser construct an Abstract Syntax Tree, and then build Tree walkers (AKA tree grammars) to process them.

我试图找出为什么我的树语法不起作用的原因,并且很想使用 ANTLRWorks 的调试器,就像我将它用于解析器本身一样.解析器的输入是源代码",但树解析器的输入是解析器的 AST 结果.我不知道如何将其用作测试树语法的输入.

I'm trying to get to the bottom of why my tree grammar isn't working and would love to use ANTLRWorks' debugger the same way I used it for the parser itself. The input to the parser is the "source code", but the input to a tree parser is the AST result of the parser. I don't see how to make that available as input to test the tree grammar.

不清楚在ANTLRWorks中有没有测试树语法的方法.如果可以的话,真的很感激能提供一个正确方向的指针.

It's not clear that there is a way to test a tree grammar in ANTLRWorks. If it can be done, a pointer in the right direction would really be appreciated.

推荐答案

ANTLRWorks 调试器应该可以很好地处理您的树语法.如果我没记错的话,您需要使用带有-debug"标志的 ANTLR 代码生成工具(我使用的是 Java 目标),然后,在创建树解析器实例的地方,使用带有端口的调试构造函数一个论点.就我而言,默认端口不起作用,所以我随意选择了35505.

The ANTLRWorks debugger should work fine with your tree grammar. If I recall correctly, you need to use the ANTLR code generation tool with the "-debug" flag (I'm using the Java target), then, where you create your tree parser instance, use the debug constructor that takes a port as an argument. In my case, the default port didn't work, so I arbitrarily picked 35505.

启动 ANTLRWorks,打开树语法,单击运行"->调试远程...",将端口设置为树解析器构造函数中使用的相同值,您应该能够连接调试器到您正在运行的应用程序.有关详细信息,请参阅 ANTLR 3 调试常见问题解答.

Fire up ANTLRWorks, open your tree grammar, click "Run"->"Debug Remote...", set the port to the same value used in the constructor for your tree parser, and you should be able to connect the debugger to your running application. See the ANTLR 3 Debugging FAQ for details.

[更新] 假设您使用的是 Java 目标(如果不是这种情况,请告诉我们),以下是有关入门的更多详细信息:

[Update] Assuming you're using the Java target (let us know if that's not the case), here's more detailed information on getting started:

当您在 ANTLRWorks 中测试非树解析器时,有一个幕后过程会从您的语法文件生成 Java 代码,然后使用该代码来解析您的输入.当您在自己的应用程序中使用解析器时,您必须使用 ANTLR(特别是 org.antlr.Tool 类)来生成 Java 代码,然后您可以将其包含在您的应用程序中.ANTLRWorks 有一个菜单选项,它应该可以帮助您入门.就我而言,我的 ant 构建文件中有一个目标,它根据我的语法生成 Java 代码,并将这些 Java 源文件放在我的应用程序的其余部分可以找到它们的地方.我的蚂蚁目标看起来像这样:

When you're testing your non-tree parser in ANTLRWorks, there's a behind-the-scenes process that generates Java code from your grammar file, then uses that code to parse your input. When you use your parser in your own application, you have to use ANTLR (specifically, the class org.antlr.Tool) to generate Java code that you can then include in your application. ANTLRWorks has a menu option for this, which should get you started. In my case, I have a target in my ant build file that generates the Java code from my grammars and puts those Java source files in a place where the rest of my application can find them. My ant target looks something like this:

<java classpath="${antlr.tool.classpath}" classname="org.antlr.Tool" failonerror="true">
    <arg value="-o" />
    <arg value="${antlr.out.dir}" />
    <arg value="${grammar.dir}/GrammarName.g" />
</java>

属性antlr.tool.classpath需要包含stringtemplate.jarantlr.jar,以及antlr.out.dir 需要指向您希望生成的源代码所在的目录(例如,build/antlr/src/org/myorg/antlr/parser,如果您的解析器语法指定包 org.myorg.antlr.parser).

The property antlr.tool.classpath needs to contain stringtemplate.jar and antlr.jar, and antlr.out.dir needs to point to the directory where you want the generated source code to go (e.g., build/antlr/src/org/myorg/antlr/parser, if your parser grammars specify the package org.myorg.antlr.parser).

然后,当您编译应用程序的其余部分时,您可以使用以下内容:

Then, when you compile the rest of your application, you can use something like:

<javac destdir="${build.classes.dir}" debug="on" optimize="on" deprecation="${javac.deprecation}" source="${javac.source}" target="${javac.target}">
    <classpath refid="stdclasspath"/>
    <src path="${src.dir}" />
    <src path="${antlr.src.dir}" />
</javac>

在这里,我们编译应用程序源代码(在 src.dir 中)以及生成的 ANTLR 代码(在 antlr.src.dir 中,在本例中为build/antlr/src).

Here, we compile our application sources (in src.dir) along with the generated ANTLR code (in antlr.src.dir, which in this example would be build/antlr/src).

至于在您的应用程序中(即在 ANTLRWorks 之外)使用生成的代码,您需要执行以下操作:

As far as using the generated code in your application (i.e., outside ANTLRWorks), you'll need to do something like:

String sourceText = "a + b = foo";
ANTLRStringStream inStream = new ANTLRStringStream(sourceText);

// your generated lexer class
MyLexer lexer = new MyLexer(inStream);
CommonTokenStream tokens = new CommonTokenStream(lexer);

// your generated parser class
MyParser parser = new MyParser(tokens);

// run the toplevel rule (in this case, `program`)
MyParser.program_return prog = parser.program();

// get the resulting AST (a CommonTree instance, in this case)
CommonTree tree = (CommonTree) prog.getTree();

// run a tree parser rule on the AST
MyTreeParser treeParser = new MyTreeParser(new CommonTreeNodeStream(tree));
treeParser.program();

我强烈建议您获取一份权威 ANTLR 参考 如果您打算使用 ANTLR.所有这些都涵盖得非常彻底,并提供了大量示例来帮助您入门.

I strongly recommend getting a copy of The Definitive ANTLR Reference if you're going to be using ANTLR. All of this is covered pretty thoroughly, with plenty of examples to get you started.

这篇关于有谁知道在 ANTLRWorks 中调试树语法的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆