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

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

问题描述

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

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调试器应该可以很好地处理您的树语法.如果我没记错的话,您需要将ANTLR代码生成工具与"-debug"标志一起使用(我正在使用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天全站免登陆