如何使用 ANTLRWorks 创建/指定用于测试树语法的 AST 输入?
背景:我创建了一个 ANTLR 语法。我能够使用 ANTLRWorks 对其进行测试和调试,并验证解析器是否创建了我心中的 AST。现在,我想为 AST 编写一个树语法,解析该树并使用 ANTLRWorks 调试该树语法。
问题:我想使用 ANTLRWorks 测试和调试树语法。因此我想解析解析器创建的 AST。使用 ANTLRWorks 测试树语法时,如何指定 AST 作为输入?
PS 我已经研究了的问题/答案有谁知道在 ANTLRWorks 中调试树语法的方法,但它没有回答我的问题。尽管被OP接受,他也发表了类似的评论。
Background: I have created an ANTLR grammar. I am able to test and debug it with ANTLRWorks and have verified that the parser creates the AST that I had in my mind. Now, I want to write a tree grammar for the AST, parse the tree and debug the tree grammar with ANTLRWorks.
Question: I want to test and debug a tree grammar with ANTLRWorks. I thus want to parse the AST that has been created by the parser. How do I specify the AST as input when testing the tree grammar with ANTLRWorks?
P.S.
I have studied the question / answer at Does anyone know of a way to debug tree grammars in ANTLRWorks but it doesn't answer my question. Although accepted by the OP, he made a similar comment.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不需要自己提供 AST,只需提供生成 AST 的解析器即可。
给定生成 AST 的以下语法:
以下是由上述语法生成的 AST 的树语法:
请务必同时放置
ASTDemo.g
和ASTDemoWalker.g
在同一个文件夹中。在 ANTLRWorks 中打开这两个语法并生成词法分析器和语法分析器。首先按 CTRL+SHIFT+G 来自ASTDemo.g
的解析器,然后通过打开 < 生成树遍历器code>ASTDemoWalker.g 并按 CTRL+SHIFT+G。现在,在
ASTDemoWalker.g
编辑器面板中,按 CTRL+D 启动调试器,并将以下源代码粘贴到文本区域中:然后按确定。
现在,您可以逐步完成调试过程,最后,您可以看到解析器生成的 AST:
以及树行者如何走过所说的 AST:
如果您现在在树语法,比如说的
^('*'表达式表达式)
,您定义^('*'表达式)
。如果您再次调试树语法,您将在通过42
节点后看到它失败:在 AST 中,
42
节点之后还有另一个节点,而树遍历器预计*
之后只有 1 个单个节点 (42
)代码>根节点。当然,这是一个简单的语法,但即使你熟悉 ANTLR,有时在 @$& 中也会很痛苦。追踪树语法中的错误! :)
You don't need to provide the AST yourself, only the parser that produces the AST.
Given the following grammar that produces an AST:
The following would be a tree grammar for the AST produced by grammar above:
Be sure to put both
ASTDemo.g
andASTDemoWalker.g
in the same folder. Open both grammars in ANTLRWorks and generate the lexer & parser fromASTDemo.g
first by pressing CTRL+SHIFT+G and then generate the tree walker by openingASTDemoWalker.g
and pressing CTRL+SHIFT+G.Now, from the
ASTDemoWalker.g
editor panel, fire up the debugger by pressing CTRL+D and paste the following source in the text area:and press OK.
You can now step through the debugging process and at the end, you can see both what AST the parser generated:
and how the tree walker walked over said AST:
If you now make an "accidental" mistake in the tree grammar, say, instead of
^('*' expression expression)
you define^('*' expression)
. If you debug the tree grammar again, you will see it fail after passing the42
node:In the AST there is another node after the
42
node, while the tree walker expected only 1 single node (42
) after the*
root node.Of course, this is an easy grammar, but even if you're familiar with ANTLR, it's sometimes a pain in the @$& to track down errors in a tree grammar! :)