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

查看:402
本文介绍了使用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,我使用Visitor类执行所需的操作.然后,我修改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会这样做.
  • 生成保留原始缩进的布局.
  • 保留文字值的基数,前导零计数和其他格式"属性
  • 使用合理的转义符重新生成字符串

做好所有这些都是棘手的.请参阅我的答案如何将AST编译回源代码以获取更多详细信息. (很奇怪, ANTLR的家伙建议根本不使用AST ;我猜测这是因为字符串模板仅适用于ANTLR可以解析其结构的ANTLR parse 树,而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天全站免登陆