使用 Antlr4 和 StringTemplates 从 AST 生成源代码 [英] Generate source code from AST with Antlr4 and StringTemplates

查看:28
本文介绍了使用 Antlr4 和 StringTemplates 从 AST 生成源代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个 AST 并且修改它,我可以使用 StringTemplates 来生成修改后的 AST 的源代码吗?

If I have an AST and modify it, can I use StringTemplates to generate the source code for the modified AST?

我已经成功地为 Antlr4 实现了我的语法.它生成源代码的 AST,我使用访问者类来执行所需的操作.然后我在 AST 中修改一些东西,我想为修改后的 AST 生成源代码.(我相信它被称为漂亮打印?).

I have successfully implemented my grammar for Antlr4. It generates the AST of a source code and I use the Visitor Class to perform the desired actions. I then modify something in the AST and I would like to generate the source code for that modified AST. (I believe it is called pretty-printing?).

Antlr 内置的 StringTemplates 是否具有执行此操作的所有功能?应该从哪里开始(非常欢迎实用建议)?

Does Antlr's built in StringTemplates have all the functionality to do this? Where should one start (practical advice is very welcome)?

推荐答案

您可以遍历树并使用字符串模板(甚至是简单的字符串打印)来吐出在某种程度上再现源文本的文本等效项.

You can walk the tree and use string templates (or even plain out string prints) to spit out text equivalents that to some extent reproduce the source text.

>

但是您会发现以现实的方式再现源文本比这建议的更难.如果你想要原程序员不会拒绝的回溯代码,你需要:

But you will find reproducing the source text in a realistic way harder to do than this suggests. If you want back code that the original programmer will not reject, you need to:

  • 保留评论.我认为 ANTLR AST 不会这样做.
  • 生成保留原始缩进的布局.
  • 保留字面值的基数、前导零计数和其他格式"属性
  • 使用合理的转义重新生成字符串

做好这一切是很棘手的.有关更多详细信息,请参阅我的 SO 回答 如何将 AST 编译回源代码.(奇怪的是,ANTLR 人员建议根本不要使用 AST;我猜这是因为字符串模板仅适用于ANTLR 解析树,其结构 ANTLR 可以理解,而 AST 则是您自己编写的.)

Doing all of this well is tricky. See my SO answer How to compile an AST back to source code for more details. (Weirdly, the ANTLR guy suggests not using an AST at all; I'm guessing this is because string templates only work on ANTLR parse trees whose structure ANTLR understands, vs. ASTs which are whatever you home-rolled.)

如果您做对了所有这些,您可能会发现修改解析树/AST 比看起来更难.对于几乎所有关于复杂语言的有趣任务,您都需要从树中提取并非微不足道的信息(例如,这个标识符的含义是什么?这个变量在哪里使用?,...) 我称之为解析后的生活.我的主要观点是修改 AST 和重新生成代码需要很多机器;请注意您的项目的规模.

If you get all of this right, what you are likely to discover is that modifying the parse tree/AST is harder than it looks. For almost any interesting task on complex languages, you need information which is not trivial to extract from the tree (e.g., what is the meaning of this identifier?, where is this variable used?,...) I call this the problem of Life After Parsing. My main point is that it takes a lot of machinery to modify ASTs and regenerate code; be aware of the size of your project.

这篇关于使用 Antlr4 和 StringTemplates 从 AST 生成源代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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