元语言解释器的确切定义是什么?

时间:2021-10-01 20:44:57

Is it legal to call a C compiler written in C or a PHP interpreter written in PHP metacircular? Is this definition valid only for languages of a specific type, like Lisp? In short, what are the conditions that an interpreter should satisfy for being called Metacircular?

调用用C编写的C编译器或用PHP元库编写的PHP解释器是合法的吗?这个定义是否只对特定类型的语言有效,比如Lisp?简而言之,译员需要满足什么条件才能被称为元胞体呢?

4 个解决方案

#1


21  

A metacircular interpreter is an interpreter written in a (possibly more basic) implementation of the same language. This is usually done to experiment with adding new features to a language, or creating a different dialect.

元字符解释器是用同一种语言(可能更基本的)实现编写的解释器。这通常是为了尝试为一种语言添加新特性,或者创建一种不同的方言。

The reason this process is associated with Lisp is because of the highly lucid paper "The Art of the Interpreter", which shows several metacircular interpreters based on Scheme. (The paper is the kernel for the book SICP, and its fourth chapter works through others that create e.g. a lazily-evaluated Scheme.)

这个过程与Lisp有关的原因是由于高度清晰的论文“解释器的艺术”,它显示了基于Scheme的几个元表达式解释器。(这篇论文是这本书的核心,它的第四章是通过其他的方法来完成的,比如创建一个延迟评估的方案。)

This is also vastly easier to do in a "homoiconic" language (a language whose code can be manipulated as data at runtime), such as Lisp, Prolog, and Forth.

在“homoicon”语言(一种代码可以在运行时作为数据进行操作的语言)中,这也容易得多,例如Lisp、Prolog等等。

As to your direct question - the C compiler wouldn't be an interpreter at all. A compiler written in its own language is 'self-hosting', which is a similar property, but more related to bootstrapping. A PHP interpreter in PHP probably wouldn't count, since you would likely be re-implementing a nontrivial amount of the language in the process. The major benefit of a conventional metacircular interpreter is that doing so isn't necessary - you can plug in the existing parser, garbage collection (if any), etc., and just write a top-level evaluator with different semantics. In Scheme or Prolog, it's often less than a page of code.

至于你的直接问题——C编译器根本不是一个解释器。用自己的语言编写的编译器是“自托管”,这是一个类似的属性,但更与引导相关。PHP中的PHP解释器可能不算数,因为您可能会在过程中重新实现大量的语言。传统的元编译器的主要好处是不需要这样做——您可以插入现有的解析器、垃圾收集(如果有的话)等,然后编写具有不同语义的*求值器。在Scheme或Prolog中,它通常少于一页代码。

#2


5  

Here is a definition from the wikipedia page for metacircular:

以下是来自*的一个定义:

A meta-circular evaluator is a special case of a self-interpreter in which the existing facilities of the parent interpreter are directly applied to the source code being interpreted, without any need for additional implementation.

元循环评估器是一种自解释器的特殊情况,在这种情况下,父解释器的现有设施直接应用于被解释的源代码,而不需要额外的实现。

So the answer is no in both cases:

所以答案是否定的

  • A C compiler is not an interpreter (evaluator). It translates a program from one form to another without executing it.
  • C编译器不是解释器(求值程序)。它将程序从一种形式转换为另一种形式,而不执行它。
  • A (hypothetical) PHP interpreter written in PHP would be a self interpreter, but not necessarily metacircular.
  • 在PHP中编写的(假设的)PHP解释器将是一个自解释器,但不一定是元数据。

#3


3  

To complement the above answers: http://www.c2.com/cgi/wiki?MetaCircularEvaluator

补充以上答案:http://www.c2.com/cgi/wiki?

#4


-1  

As i understand it, a metacircular interpreter is an interpreter that can interpret itself.

正如我所理解的那样,元字符解释器是一种解释器,可以解释自己。

A compiler only translates code, and doesn't execute it.

编译器只翻译代码,不执行它。

Any Turing-complete language is mathematically able to emulate any logical computation, so here's an example using Python. Instead of using CPython to translate this code to CPU instructions and execute it, you could also use PyPy. The latter is bootstrapped, so fulfills some arbitrary criterion that some people use to define a metacircular interpreter.

任何图灵完全语言都可以在数学上模拟任何逻辑计算,所以这里有一个使用Python的例子。不使用CPython将代码转换为CPU指令并执行它,您还可以使用PyPy。后者是引导的,因此满足了一些人用来定义元数据解释器的任意标准。

"""
Metacircular Python interpreter with macro feature.
By Cees Timmerman, 14aug13.
"""

import re

def meta_python_exec(code):
    # Optional meta feature.
    re_macros = re.compile("^#define (\S+) ([^\r\n]+)", re.MULTILINE)
    macros = re_macros.findall(code)
    code = re_macros.sub("", code)
    for m in macros:
        code = code.replace(m[0], m[1])

    # Run the code.
    exec(code)

if __name__ == "__main__":
    #code = open("metacircular_overflow.py", "r").read()  # Causes a stack overflow in Python 3.2.3, but simply raises "RuntimeError: maximum recursion depth exceeded while calling a Python object" in Python 2.7.3.
    code = "#define 1 2\r\nprint(1 + 1)"
    meta_python_exec(code)

#1


21  

A metacircular interpreter is an interpreter written in a (possibly more basic) implementation of the same language. This is usually done to experiment with adding new features to a language, or creating a different dialect.

元字符解释器是用同一种语言(可能更基本的)实现编写的解释器。这通常是为了尝试为一种语言添加新特性,或者创建一种不同的方言。

The reason this process is associated with Lisp is because of the highly lucid paper "The Art of the Interpreter", which shows several metacircular interpreters based on Scheme. (The paper is the kernel for the book SICP, and its fourth chapter works through others that create e.g. a lazily-evaluated Scheme.)

这个过程与Lisp有关的原因是由于高度清晰的论文“解释器的艺术”,它显示了基于Scheme的几个元表达式解释器。(这篇论文是这本书的核心,它的第四章是通过其他的方法来完成的,比如创建一个延迟评估的方案。)

This is also vastly easier to do in a "homoiconic" language (a language whose code can be manipulated as data at runtime), such as Lisp, Prolog, and Forth.

在“homoicon”语言(一种代码可以在运行时作为数据进行操作的语言)中,这也容易得多,例如Lisp、Prolog等等。

As to your direct question - the C compiler wouldn't be an interpreter at all. A compiler written in its own language is 'self-hosting', which is a similar property, but more related to bootstrapping. A PHP interpreter in PHP probably wouldn't count, since you would likely be re-implementing a nontrivial amount of the language in the process. The major benefit of a conventional metacircular interpreter is that doing so isn't necessary - you can plug in the existing parser, garbage collection (if any), etc., and just write a top-level evaluator with different semantics. In Scheme or Prolog, it's often less than a page of code.

至于你的直接问题——C编译器根本不是一个解释器。用自己的语言编写的编译器是“自托管”,这是一个类似的属性,但更与引导相关。PHP中的PHP解释器可能不算数,因为您可能会在过程中重新实现大量的语言。传统的元编译器的主要好处是不需要这样做——您可以插入现有的解析器、垃圾收集(如果有的话)等,然后编写具有不同语义的*求值器。在Scheme或Prolog中,它通常少于一页代码。

#2


5  

Here is a definition from the wikipedia page for metacircular:

以下是来自*的一个定义:

A meta-circular evaluator is a special case of a self-interpreter in which the existing facilities of the parent interpreter are directly applied to the source code being interpreted, without any need for additional implementation.

元循环评估器是一种自解释器的特殊情况,在这种情况下,父解释器的现有设施直接应用于被解释的源代码,而不需要额外的实现。

So the answer is no in both cases:

所以答案是否定的

  • A C compiler is not an interpreter (evaluator). It translates a program from one form to another without executing it.
  • C编译器不是解释器(求值程序)。它将程序从一种形式转换为另一种形式,而不执行它。
  • A (hypothetical) PHP interpreter written in PHP would be a self interpreter, but not necessarily metacircular.
  • 在PHP中编写的(假设的)PHP解释器将是一个自解释器,但不一定是元数据。

#3


3  

To complement the above answers: http://www.c2.com/cgi/wiki?MetaCircularEvaluator

补充以上答案:http://www.c2.com/cgi/wiki?

#4


-1  

As i understand it, a metacircular interpreter is an interpreter that can interpret itself.

正如我所理解的那样,元字符解释器是一种解释器,可以解释自己。

A compiler only translates code, and doesn't execute it.

编译器只翻译代码,不执行它。

Any Turing-complete language is mathematically able to emulate any logical computation, so here's an example using Python. Instead of using CPython to translate this code to CPU instructions and execute it, you could also use PyPy. The latter is bootstrapped, so fulfills some arbitrary criterion that some people use to define a metacircular interpreter.

任何图灵完全语言都可以在数学上模拟任何逻辑计算,所以这里有一个使用Python的例子。不使用CPython将代码转换为CPU指令并执行它,您还可以使用PyPy。后者是引导的,因此满足了一些人用来定义元数据解释器的任意标准。

"""
Metacircular Python interpreter with macro feature.
By Cees Timmerman, 14aug13.
"""

import re

def meta_python_exec(code):
    # Optional meta feature.
    re_macros = re.compile("^#define (\S+) ([^\r\n]+)", re.MULTILINE)
    macros = re_macros.findall(code)
    code = re_macros.sub("", code)
    for m in macros:
        code = code.replace(m[0], m[1])

    # Run the code.
    exec(code)

if __name__ == "__main__":
    #code = open("metacircular_overflow.py", "r").read()  # Causes a stack overflow in Python 3.2.3, but simply raises "RuntimeError: maximum recursion depth exceeded while calling a Python object" in Python 2.7.3.
    code = "#define 1 2\r\nprint(1 + 1)"
    meta_python_exec(code)