【记录】使用Eclipse开发Antlr:如何调试antlr项目

【背景】

在Eclipse中创建并编译好antlr的项目后,接着就是去折腾如何调试此antlr了。


1.参考:

Testing ANTLR Grammar

去设置debug路径:

output build path use as source folder_thumb

注意到,此时java文件中,新增一个default package了:

note has added default package_thumb

但是还是没有搞懂,然后运行或调试。

2. 去试试Interpreter中的Run:

interpereter then select a rule then run_thumb

结果没有任何反应。

3.点击New Test Case:

try new test case_thumb

也没任何效果。

4.保存一个测试:

try save test case_thumb

indeed saved one test case but no use_thumb

虽然保存了一个测试,但是没任何用处。

5.尝试debug as选择java applet:

debug as java applet_thumb

结果出错:

selection does not contain an applet

selection does not contain an applet_thumb

6.再试试Debug as选java application:

debug as java application_thumb

选择:

ASTFrame – antlr.debug.misc

astframe antlr.debug.misc then ok_thumb

结果出来一个

AST JTree Example

show an ast Jtree example_thumb

7.Debug as选java application后,换成:

Interp – org.antlr.gunit

interp org antlr gunit_thumb

结果啥输出都没有。

8.想起来,去:

http://sourceforge.net/projects/antlrv3ide/

可以看到人家给出的一个截图,里面是可以调试的:

antlr eclipse plugin debug ui from official_thumb

但是此处,还是不知道如何弄出这个调试界面。

9.另外,想起来了,好像对于antlrworks中,也只是针对已有的lexer和parser的java文件,

然后另外写一个用于测试的java文件:

__Test__.java

内容为:

import java.io.*;
import org.antlr.runtime.*;
import org.antlr.runtime.debug.DebugEventSocketProxy;


public class __Test__ {

    public static void main(String args[]) throws Exception {
        HartEddlLexer lex = new HartEddlLexer(new ANTLRFileStream("D:\\DevRoot\\IndustrialMobileAutomation\\HandheldDataSetter\\ANTLR\\projects\\v1.5\\HartEddlParser_local_TFS\\hartEddlTestFile_pos.ddl", "UTF8"));
        CommonTokenStream tokens = new CommonTokenStream(lex);

        HartEddlParser g = new HartEddlParser(tokens, 49100, null);
        try {
            g.startParse();
        } catch (RecognitionException e) {
            e.printStackTrace();
        }
    }
}

很明显,指定对应的输入测试的文件,然后调用对应的起始的rule,即类似于main函数的startParse,然后就可以去调试了。

此处,就是相办法,把这部分的内容,弄到Eclipse中。

10.参考:

Eclipse ANTLR IDE Interpreter fails

->

Run configuration, interpreter view?

然后去Eclipse的help中搜ANTLR IDE:

help then help contents

然后找到了,关于调试antlr代码的说明:

find help for debug antlr grammar

不过刚发现,还是先去试试Run的部分:

running your grammmars for antlr

然后按照其解释去操作,结果在"Test Panel"中,只有我自己之前创建的那个antlrDebugTest:

test panel no simpleTest

根本没有那些"simpleTest",所以,看来还要去找,如何新建这个simpleTest的。

注意到其开始的提示说是:

This feature is only available for Java grammars (language=Java).

所以,再去在源码中添加:

add language java into g file

然后再回去看看,结果依旧还是没有出现simpleTest。

11.还是自己折腾:

选中Rule中的startParse,然后点击右边那个Save test case:

select rule startParse then save test case

然后输入名字:

input test case name then ok

可以看到刚新建好的startParseTest:

can see newly created test case startParseTest

然后在右边输入要测试的内容,然后点击保存:

copy test data into then save

然后才能看到对应的Run和Debug的选项是可用的:

then can see the run and debug are available

点击Debug(Java)后,会跳出对话框:

save and lunch for debug

点击OK后,结果等了几秒后,就又啥都没有出现,显示javaw运行停止了:

only see terminated startParse rule for javaw

切换会Interpreter后,也没任何变化:

switch to interperter is nothing

12.还是参考help中的"Debugging your grammars":

help for debugging your grammars for antlr

然后去试试加断点:

add breakpoint for startParse entry point rule

added ANTLR Breakpoints for type is location

然后添加好后,该行代码,果然出现断点记号了:

added note for this line for breakpoint

加完此断点后,然后再去试试Debug:

added breakpoint then still goto debug java

然后换成Run试试,结果还出错了:

【已解决】Eclipse中用Interpreter去运行Antlr代码出错:An internal error occurred during: "ANTLR Event Listener". For input string: "false"

13.虽然上面已经解决了Run的问题,可以生成树状的图形了。

但是还是无法单步Debug调试。、

试试,把所导入的3.5的antlr的库,换成3.3的试试:

change insatalled antlr jar from 3.5 to 3.3 version

然后重新build project,还是不行。

14.再参考:

ANTLR parser hanging at proxy.handshake call

取消刚才选上的-debug:

unselect the debug option for antlr

然后再重新build project后,再去试试,还是同样结果:

Run是可以生成树的;

Debug无任何输出。

15.再去看了help中的解释,如果Debug(Java)正常的话,应该跳转到Debug Perspective才对的。

此处是没有,只是停留在代码的Perspective:

stay on g file perspective

16.不知道何时,.g中的

    language = java;

没了。

现在再去加上。

结果突然又是一堆错了:

ANTLR Parser Generator  Version 3.5.

Using project classpath: No.

Grammar: D:\DevRoot\android\android_root\HartEddl\HartEddl.g

BUILD SUCCESSFUL

Total time: 1 second

ANTLR Parser Generator 3.5 ${buildNumber}.

Using project classpath: Yes.

Grammar: D:\DevRoot\android\android_root\HartEddl\HartEddl.g

BUILD SUCCESSFUL

Total time: 1 second

ANTLR Parser Generator 3.3 Nov 30, 2010 12:50:56.

Using project classpath: Yes.

Grammar: D:\DevRoot\android\android_root\HartEddl\HartEddl.g

error(10):  internal error: Exception expecting ‘*’, found ‘8’@org.antlr.grammar.v2.ANTLRLexer.nextToken(ANTLRLexer.java:346): unexpected stream error from parsing /HartEddl/HartEddl.g

error(150):  grammar file /HartEddl/HartEddl.g has no rules

error(10):  internal error: /HartEddl/HartEddl.g : java.lang.IllegalStateException: java.lang.NullPointerException

org.deved.antlride.runtime.AntlrErrorListener$DynamicToken.invokeMethod(AntlrErrorListener.java:59)

org.deved.antlride.runtime.AntlrErrorListener$DynamicToken.getLine(AntlrErrorListener.java:64)

org.deved.antlride.runtime.AntlrErrorListener.report(AntlrErrorListener.java:131)

org.deved.antlride.runtime.AntlrErrorListener.message(AntlrErrorListener.java:113)

org.deved.antlride.runtime.AntlrErrorListener.error(AntlrErrorListener.java:103)

org.antlr.tool.ErrorManager.syntaxError(ErrorManager.java:803)

org.antlr.grammar.v2.AssignTokenTypesWalker.reportError(AssignTokenTypesWalker.java:110)

org.antlr.grammar.v2.AssignTokenTypesWalker.grammar(AssignTokenTypesWalker.java:223)

org.antlr.tool.CompositeGrammar.assignTokenTypes(CompositeGrammar.java:335)

org.antlr.Tool.process(Tool.java:451)

org.deved.antlride.runtime.Tool2.main(Tool2.java:24)

3 errors

BUILD FAIL

Total time: 342 milliseconds

ANTLR Parser Generator 3.3 Nov 30, 2010 12:50:56.

Using project classpath: Yes.

Grammar: D:\DevRoot\android\android_root\HartEddl\HartEddl.g

error(10):  internal error: Exception expecting ‘*’, found ‘8’@org.antlr.grammar.v2.ANTLRLexer.nextToken(ANTLRLexer.java:346): unexpected stream error from parsing /HartEddl/HartEddl.g

error(150):  grammar file /HartEddl/HartEddl.g has no rules

error(10):  internal error: /HartEddl/HartEddl.g : java.lang.IllegalStateException: java.lang.NullPointerException

org.deved.antlride.runtime.AntlrErrorListener$DynamicToken.invokeMethod(AntlrErrorListener.java:59)

org.deved.antlride.runtime.AntlrErrorListener$DynamicToken.getLine(AntlrErrorListener.java:64)

org.deved.antlride.runtime.AntlrErrorListener.report(AntlrErrorListener.java:131)

org.deved.antlride.runtime.AntlrErrorListener.message(AntlrErrorListener.java:113)

org.deved.antlride.runtime.AntlrErrorListener.error(AntlrErrorListener.java:103)

org.antlr.tool.ErrorManager.syntaxError(ErrorManager.java:803)

org.antlr.grammar.v2.AssignTokenTypesWalker.reportError(AssignTokenTypesWalker.java:110)

org.antlr.grammar.v2.AssignTokenTypesWalker.grammar(AssignTokenTypesWalker.java:223)

org.antlr.tool.CompositeGrammar.assignTokenTypes(CompositeGrammar.java:335)

org.antlr.Tool.process(Tool.java:451)

org.deved.antlride.runtime.Tool2.main(Tool2.java:24)

3 errors

BUILD FAIL

Total time: 315 milliseconds

ANTLR Parser Generator 3.3 Nov 30, 2010 12:50:56.

Using project classpath: Yes.

Grammar: D:\DevRoot\android\android_root\HartEddl\HartEddl.g

error(10):  internal error: Exception expecting ‘*’, found ‘8’@org.antlr.grammar.v2.ANTLRLexer.nextToken(ANTLRLexer.java:346): unexpected stream error from parsing /HartEddl/HartEddl.g

error(150):  grammar file /HartEddl/HartEddl.g has no rules

error(10):  internal error: /HartEddl/HartEddl.g : java.lang.IllegalStateException: java.lang.NullPointerException

org.deved.antlride.runtime.AntlrErrorListener$DynamicToken.invokeMethod(AntlrErrorListener.java:59)

org.deved.antlride.runtime.AntlrErrorListener$DynamicToken.getLine(AntlrErrorListener.java:64)

org.deved.antlride.runtime.AntlrErrorListener.report(AntlrErrorListener.java:131)

org.deved.antlride.runtime.AntlrErrorListener.message(AntlrErrorListener.java:113)

org.deved.antlride.runtime.AntlrErrorListener.error(AntlrErrorListener.java:103)

org.antlr.tool.ErrorManager.syntaxError(ErrorManager.java:803)

org.antlr.grammar.v2.AssignTokenTypesWalker.reportError(AssignTokenTypesWalker.java:110)

org.antlr.grammar.v2.AssignTokenTypesWalker.grammar(AssignTokenTypesWalker.java:223)

org.antlr.tool.CompositeGrammar.assignTokenTypes(CompositeGrammar.java:335)

org.antlr.Tool.process(Tool.java:451)

org.deved.antlride.runtime.Tool2.main(Tool2.java:24)

3 errors

BUILD FAIL

Total time: 314 milliseconds

所以,再去换回3.5的jar。

再重启Eclipse。

17.然后重新打开Eclipse,重新Build后,去Debug(Java),这下出错了:

【已解决】Eclipse中对于antlr去Debug(Java)时出错:execution was cancelled reason code generation fails

18.解决了上述问题后,点击Debug(Java),也可以跳转到对应的Debug Perspective了,但是又出现其他错误:

【未解决】Eclipse中用Debug(Java)调试Antlr出错:unable to connect to ANTLR Debugger,Connection refused: connect


后续的折腾,参见:

【记录】继续折腾如何使用在Eclipse中调试Antlr项目



发表评论

电子邮件地址不会被公开。 必填项已用*标注

无觅相关文章插件,快速提升流量