创建源到源转换器
我想知道创建源到源翻译器(即创建从一种高级语言到另一种高级语言的翻译)的策略是什么。我想到的两种方法是
1-将一种语言的语法树更改为另一种语言的语法树 2-将其更改为中间语言,然后将其转换为其他高级语言
我的问题是,是否可以使用这两种策略进行转换,哪种策略更可行,任何人都可以对由一些转换器类似于上述任何方法。是否有任何基于标准 xml 的中间语言,我知道 xmlvm 使用 xml 作为中间语言,但它没有提供任何适当的中间语言规范。
I want to know that what are the strategies to create a source to source translator i.e creating translation from one high level language to another. The two ways that come into my mind are
1- Changing syntax tree of one language to other language syntax tree
2- Changing it to intermediate language and then converting that to other high level language
My question is that is it possible to do the conversion using both strategies and which is more feasible to do, can anyone give some refernces to any theory or implementation done by some converter like any of above methods. And is there any standard xml based intermediate language, i know that xmlvm uses xml as intermediate language but it does not provide any proper specification of the intermediate language.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
粗略地说,任何编译器都是源到源的翻译器。目标语言可以是汇编语言(或直接是二进制机器代码语言)、C 语言或您喜欢的任何高级语言。因此,一般的编译器理论是适用的。
正如我的建议——一种中间语言通常是不够的。使用更多。使用数十种中间语言,每种语言都与以前的语言只有一个微小的方面不同。这样,任何语言到语言的翻译都变得微不足道。
另一条建议(预计这里会有反对票)——远离 XML,尤其是作为 AST< 的表示形式/a>s。
Any compiler is, roughly, a source-to-source translator. Target language can be an assembly language (or directly a binary machine code language), or C, or whatever high level language you fancy. So, the general compilers theory is applicable.
And just as a word of advice - one intermediate language is normally not nearly enough. Use more. Use dozens of intermediate languages, each different from a previous one in just one tiny aspect. This way any language-to-language translation is nothing but trivial.
Another word of advice (anticipating downvotes here) - stay away from XML, especially as a representation for ASTs.
我会考虑 LLVM,它可以进行源到源的操作。虽然输出并不漂亮,但它可能提供一些好的想法。
I would look at LLVM, which can do source to source. Although the output isn't pretty, it might provide some good ideas.
转换器通常基于构建一个程序的语义树,然后将其重新塑造为目标 PL。例如,请查看C# 到 Java 转换器。
第二种方法也是可行的,但转换后代码的组织可能会完全改变。因此,最好保持中间通用结构(IL、ST 等)尽可能高的水平。
The converters are usually based on constructing the semantic tree of one program and then re-shaping it to the target PL. As an example, take a look at C# to Java convertor.
The second approach is also possible, but the organization of your code may change completely after conversion. So, it is better to keep the intermediate common structure (IL, ST, etc), as high level as possible.
试试叮当!它对于源到源的翻译非常强大。到目前为止,它完全支持 C、C++、Objective C 和 Objective C++。
您可能还想查看 ROSE 编译器基础结构。
Try Clang! It is powerful for source-to-source translation. As of now it fully supports C, C++, Objective C and Objective C++.
You may also want to look at ROSE compiler infrastructure.