Transcompiling到另一种语言 [英] Transcompiling to another language

查看:179
本文介绍了Transcompiling到另一种语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是在code可以transcompiled典型的方式?目前,我正在写一个简单的编程语言,它的处理方式是递归。节点的列表,通过循环,并说当前节点是一个变量节点,它会调用一个 emit_variable_node 函数,它将从字面上追加一些code为字符串例如:

What are the typical ways in which code can be transcompiled? Currently, I'm writing a simple programming language, and the way it's handled is recursively. A list of nodes are looped through, and say the current node is a variable node, it will call a emit_variable_node function, which will literally append some code to a string, for example:

以下code是伪十岁上​​下,我写我用C项目,编译为C。

The following code is psuedo-ish, I'm writing my project in C, and compiling to C.

char *file_contents;

void emit_variable_node(VariableNode *var) {
    // I know += doesn't work on strings, just pretend it does.
    file_contents += var.getType();
    file_contents += " "; // a space
    file_contents += var.getName();
    // etc
}

我也假设,我们正在考虑到code已被语义分析,是正确的。然后file_contents字符串存储到临时文件时,它已经被C编译器编译之后被删除。

I'm also assuming that the code we're given has been semantically analyzed, and is correct. The file_contents string is then stored into a temporary file, which is deleted after it's been compiled by a C compiler.

那是一个不好的做法,还是有更好,更简洁的方式来做到这一点?

Is that a bad practice, or are there better, more cleaner ways to do this?

推荐答案

您可以编写一个解析器以任何方式你喜欢,并产生code,因为它解析,没有AST节点必要的(语法制导翻译)。这通常会产生pretty可怕code,因为code发电机组没有拍到方面考虑,以生成更好code。

You can write a parser by any means you like, and generate code as it parses, no AST nodes necessary ("syntax directed translation"). That will generally produce pretty awful code, because the code generator has no opportunity to take context into account to generate better code.

您可以构建构建抽象语法树(AST的)作为第一关,然后作为第二遍走在树生成code不看任何相邻节点的分析器。这仅仅是previous答案在它的AST。
这里有一个做了做这样的事情没有优化transpiler输出的令人震惊的坏榜样。

You can build a parser that builds abstract syntax trees (ASTs) as a first pass, and then as a second pass walks over the tree generating code without looking at any neighboring nodes. This is just the previous answer with ASTs in it. Here's a stunningly bad example of unoptimized transpiler output done doing something like this.

更好的是从AST,其中每个节点的AST当地code发生器检查其邻国产生code,以决定该怎么做。这会给你有所好转code。

Better is to generate code from the AST, where each AST node local code generator inspects its neighbors, to decide what to do. This will give you somewhat better code.

一个更好的解决办法是遵循传统的编译器的领导下,建立你的语言,包括符号表和控制流和数据流分析的一个很好的前端。然后,您可以用它来生成更好的code。

A better solution is to follow the lead of conventional compilers, build a good front end for your language, including symbol tables and control and data flow analysis. You can then use this to generate much better code.

关于实际code一代:是的,你可以打印文本字符串。字符串模板是更方便一点,但他们只是打印文本字符串的一个奇特的方式,所以他们不添加任何权力或改善所得code质量。

Regarding actual code generation: yes, you can print text strings. String templates are a little more convenient, but they are just a fancy way to print text strings, so they don't add any power or improve the resulting code quality.

一个更好的解决方案是在你的源语言转换的AST,AST的到你的目标语言,包括所有的地方检查,并使用从符号表信息和流量分析。这样做的很好的结果是,在目标语言产生的AST,你现在可以申请在目标语言,是不可能在源语言的优化。 [实时编译器做这样的事情,他们使用,但条件是翻译AST为IR(内部重新presentation),他们做的IR优化。]毕竟目标AST的优化是完整的,你有以pretty,打印最终AST ...使用类似字符串模板。

A better solution is to transform ASTs in your source language, into ASTs in your target language, including all the local checks and using information from the symbol table and flow analysis. The nice consequence of this is that by producing ASTs in the target language, you can now apply optimizations in the target language that are not possible in the source language. [Real compilers do something like this, the but terms they use are "translate AST to IR (internal representation)" and they do optimizations on the IR.] After all the optimizations on the target AST are complete, you have to pretty-print the final AST... using something like string templates.

大多数人不必从头开始建立了良好的transpiler的能量。因此,他们不喜欢的第一个建议(只是在说')一些哈克的事情。但如果你想从一种语言转换code到另一个一个很好的基础,看看我们的 DMS软件再造工具包。 DMS有许多语言解析器,可以实现自定义语言解析器,自动生成的AST,提供了大量的支持的生命解析,例如,建筑符号表和流量分析之后,确实给AST AST改造,并拥有pretty打印机。 DMS被设计为支持这种任务的一个平台。这意味着你可以集中建设任务的高质量翻译一部分,而不是试图建立的所有有用的基础设施。

Most people don't have the energy to build a good transpiler from scratch. So they do some hacky thing like the first suggestion (just sayin'). But if you want a really good foundation for transforming code from one language to another, check out our DMS Software Reengineering Toolkit. DMS has parsers for many languages, can implement parsers for custom languages, automatically builds ASTs, provides a lot of support for Life After Parsing, e.g., building symbol tables and flow analysis, does AST to AST transformation, and has pretty printers. DMS is designed to be a platform to support this kind of task. What this means is you can concentrate on building the high-quality translation part of the task, rather than trying to build all that useful infrastructure.

这篇关于Transcompiling到另一种语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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