构建您自己的可以充当编译器的解释器
我可以构建自己的解释器,然后将其转换为编译器吗?如果是,我该如何构建它?
It is possible for me to build my own Interpreter that could then be transformed into a compiler? If yes, how do I go about building it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这被称为第二次二村投影。它首先由 Prof. 描述。 Yoshihiko Futamura 在他 1971 年的论文 计算过程的部分评估 - 编译器-编译器的方法(日语),英文版 28 年后重新发布。
它使用部分求值,通过相对于解释器部分求值部分求值器本身,从而产生编译器。
因此,您需要两个要素:目标语言的解释器,用某种宿主语言(可能与目标语言相同或不同)编写,以及能够评估解释器及其本身的部分评估器,换句话说,它需要部分评估宿主语言,并且它本身需要用它可以评估的宿主语言编写。
This is called the Second Futamura Projection. It was first described by Prof. Yoshihiko Futamura in his 1971 paper Partial Evaluation of Computation Process – An approach to a Compiler-Compiler (Japanese), an English version of which was republished 28 years later.
It uses Partial Evaluation, by partially evaluating the partial evaluator itself with respect to the interpreter, thus yielding a compiler.
So, you need two ingredients: an interpreter for your target language, written in some host language (which may or may not be the same as the target language) and a partial evaluator capable of evaluating both the interpreter and itself, in other words it needs to partially evaluate the host language and it needs to itself be written in the host language it can evaluate.
已经提到的部分评估是可能的方法之一(一种计算量非常大的方法,但另一方面却相当通用)。另一种方法是元编程:如果一种语言的解释器以针对另一种解释语言的简单翻译器的形式实现,那么以后很容易将其重新定位到某种编译语言,或者用编译器替换目标解释器。
An already mentioned partial evaluation is one of the possible methods (a very computationally intensive one, but quite generic on the other hand). Another approach is metaprogramming: if an interpreter of a language is implemented in a form of a simple translator which targets another interpreted language, it is very easy to re-target it later to some compiled language, or replace the target interpreter with a compiler.
除了 Futaruma 投影之外,另一种方法是元跟踪 jit。元跟踪 jit 不会直接跟踪或 jit 您的程序,而是通过解释器间接跟踪或 jit。
RPython 是一个很酷的元跟踪框架。你用Python的受限版本编写一个解释器,RPython将它变成C语言的jit编译器。
Apart from Futaruma projections, another approach is meta-tracing jit. Meta-tracing jit doesn't directly trace or jit your programs, but indirectly, through the interpreter.
RPython is a cool meta-tracing framework. You write an interpreter in a restricted version of python, and RPython turns it into a jit compiler in C.