如何使用 ANTLRWorks 创建/指定用于测试树语法的 AST 输入?

发布于 2024-12-28 02:39:19 字数 429 浏览 1 评论 0原文

背景:我创建了一个 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 技术交流群。

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

发布评论

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

评论(1

小糖芽 2025-01-04 02:39:19

使用 ANTLRWorks 测试树语法时,如何指定 AST 作为输入?

您不需要自己提供 AST,只需提供生成 AST 的解析器即可。

给定生成 AST 的以下语法:

grammar ASTDemo;

options { 
  output=AST;
}

tokens {
  ROOT;
  U_MIN;
}

parse
 : expression EOF -> ^(ROOT expression)
 ;

expression
 : addition
 ;

addition
 : multiplication (('+' | '-')^ multiplication)*
 ;

multiplication
 : unary (('*' | '/')^ unary)*
 ;

unary
 : '-' atom -> ^(U_MIN atom)
 | atom
 ;

atom
 : ID
 | NUMBER
 | '(' expression ')' -> expression
 ;

ID     : ('a'..'z' | 'A'..'Z')+;
NUMBER : '0'..'9'+ ('.' '0'..'9'*)?;
SPACE  : (' ' | '\t' | '\r' | '\n')+ {skip();};

以下是由上述语法生成的 AST 的树语法:

tree grammar ASTDemoWalker;

options {
  output=AST;
  tokenVocab=ASTDemo;
  ASTLabelType=CommonTree;
}

parse
 : ^(ROOT expression)
 ;

expression
 : ^('+' expression expression)
 | ^('-' expression expression)
 | ^('*' expression expression)
 | ^('/' expression expression)
 | ^(U_MIN expression)
 | atom
 ;

atom
 : ID
 | NUMBER
 ;

请务必同时放置 ASTDemo.gASTDemoWalker.g在同一个文件夹中。在 ANTLRWorks 中打开这两个语法并生成词法分析器和语法分析器。首先按 CTRL+SHIFT+G 来自 ASTDemo.g 的解析器,然后通过打开 < 生成树遍历器code>ASTDemoWalker.g 并按 CTRL+SHIFT+G

现在,在 ASTDemoWalker.g 编辑器面板中,按 CTRL+D 启动调试器,并将以下源代码粘贴到文本区域中:

42 * ((a + 3) / -3.14)

然后按确定

现在,您可以逐步完成调试过程,最后,您可以看到解析器生成的 AST:

在此处输入图像描述

以及树行者如何走过所说的 AST:

在此处输入图像描述

如果您现在在树语法,比如说的^('*'表达式表达式),您定义^('*'表达式)。如果您再次调试树语法,您将在通过 42 节点后看到它失败:

enter image这里的描述

在 AST 中,42 节点之后还有另一个节点,而树遍历器预计 * 之后只有 1 个单个节点 (42)代码>根节点。

当然,这是一个简单的语法,但即使你熟悉 ANTLR,有时在 @$& 中也会很痛苦。追踪树语法中的错误! :)

How do I specify the AST as input when testing the tree grammar with ANTLRWorks?

You don't need to provide the AST yourself, only the parser that produces the AST.

Given the following grammar that produces an AST:

grammar ASTDemo;

options { 
  output=AST;
}

tokens {
  ROOT;
  U_MIN;
}

parse
 : expression EOF -> ^(ROOT expression)
 ;

expression
 : addition
 ;

addition
 : multiplication (('+' | '-')^ multiplication)*
 ;

multiplication
 : unary (('*' | '/')^ unary)*
 ;

unary
 : '-' atom -> ^(U_MIN atom)
 | atom
 ;

atom
 : ID
 | NUMBER
 | '(' expression ')' -> expression
 ;

ID     : ('a'..'z' | 'A'..'Z')+;
NUMBER : '0'..'9'+ ('.' '0'..'9'*)?;
SPACE  : (' ' | '\t' | '\r' | '\n')+ {skip();};

The following would be a tree grammar for the AST produced by grammar above:

tree grammar ASTDemoWalker;

options {
  output=AST;
  tokenVocab=ASTDemo;
  ASTLabelType=CommonTree;
}

parse
 : ^(ROOT expression)
 ;

expression
 : ^('+' expression expression)
 | ^('-' expression expression)
 | ^('*' expression expression)
 | ^('/' expression expression)
 | ^(U_MIN expression)
 | atom
 ;

atom
 : ID
 | NUMBER
 ;

Be sure to put both ASTDemo.g and ASTDemoWalker.g in the same folder. Open both grammars in ANTLRWorks and generate the lexer & parser from ASTDemo.g first by pressing CTRL+SHIFT+G and then generate the tree walker by opening ASTDemoWalker.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:

42 * ((a + 3) / -3.14)

and press OK.

You can now step through the debugging process and at the end, you can see both what AST the parser generated:

enter image description here

and how the tree walker walked over said AST:

enter image description here

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 the 42 node:

enter image description here

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! :)

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