Python writes a scheme interpreter (i)

Source: Internet
Author: User

  1. The nature of the interpreter

    ?

      1. When we change the language to write the interpreter, in fact, the nature and Scheme write scheme is the same, the string will be entered as the source program execution and syntax and semantics are designed and strictly implemented by themselves.

        ?

      2. Here we use Python to implement our second version of the scheme interpreter, first Python-supported list derivation, lambda, pattern matching and other syntax sugar is very suitable to write the interpreter, on the other hand, Python built-in data structure, easy to use.

        Of course, we adopt C + + is also completely feasible, but because C + + string processing function is weak, and different types of data conversion need to be done manually, the implementation of the noise is too much, that is, focus on some not the essence of the time is more than the problem. But in fact, writing a word in C + + can improve the understanding of this problem.

        In the difficult to achieve and reap such a problem on the choice of nature is a matter of opinion.

        Java or C # is certainly more feasible, but if you use C language to do it, feel a little trouble, not worth the candle.

        ?

      3. The process of interpreter implementation:

        If you have read any of the compiling principles of a book, the introduction will introduce this process

        1. Lexical analysis

          Convert the string stream to a meaningful token unit:

          For example (cons 1 2) should be broken down into ' (' cons ' 1 ' 2 ') ', where the left and right brackets are also listed as tokens because it is a delimiter for a simple expression in scheme.

          ?

        2. Syntax analysis

          Convert tokens to parse tree, the general compiling principle of the textbook here will be evaluated as an example of expression, such as 1+2 *3-2

          .-

    .2. +

    .1 *

    .2.3

    ?

    For pre-Order Expressions: (/(+ 1 (* 3 2) (-2 3))

    ./

    .+ .-

    .1. *. 2.3

    .3.2

    The previous blog post, "Recursive descent resolution expression evaluation," discusses a similar issue in detail, where we take advantage of the recursive nature of the expression itself, that is, we simply find and enclose the smallest expression unit and then evaluate it, and each part of the nested complex expression recursively calls the interpreter to evaluate it.

    For a simple example, (cons 1 (cons 2 3))

    After lexical analysis: ' (' cons ' 1 ' (' Cons ' ' 2 ' 3 ') ') '

    After parsing: [' cons ' 1 [' Cons ' 2 3]]

    There are two main things that have been observed:

      1. The number of the self-evaluation Unit is converted to its value;
      2. Make a list of the sub-expressions;
      1. Value evaluation

        After converting the input string to the parsed result, what we should do is to evaluate it according to semantics, which is implemented in Python, so it is translated directly into some data structures and functions in Python, and after this step the interpreter ends, without involving intermediate code or compiled translations.

        ?

        On the two blog posts, the parameters required by the evaluation device are: Program + evaluation environment, and the environment is essentially a variable and value of the key pair, so the dict in Python can be implemented.

      1. After the implementation is complete, the interpreter is finished, and the rest is a matter of interactive interface.

?

    1. Definition and selection of grammar

      ?

      The above process is basically that, and with scheme to achieve the same, we need to consider what our interpreter provides the functions and capabilities, the best way is to provide enough abstract barrier on the basis of simple to complex gradually to expand, rather than first define the complete syntax, Then follow the above process to one by one implementation.

      ?

      For example, we first implement a arithmetic evaluation, it contains all the steps in the above process, and then add such as cons, car, CDR, list and other built-in data structures and common functions, and then add the support of variables, lambda function support, procedure call support.

      ?

      It is important to note that the implementation of functions and environments has been mentioned before with scheme implementations, such as the closure nature of the function needs to be maintained, the interface nature of the environment must be observed, and so on.

      ?

      This part is the real key and meaningful part, more specific content and implementation see next blog post.

?

?

?

?

Python writes a scheme interpreter (i)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.