What do you need to know to make a simple Python interpreter?

Source: Internet
Author: User
I am a computer major student, learning how to build a Python interpreter in the process of compiling the principles of the course, but our course only talk about contextual unrelated grammar, lexical analysis, grammar analysis and four yuan expression of these pieces of content, and only some manual conversion method, It's not very deep about some practical applications. I learned that the Python language is context-sensitive and that there is no detailed explanation of the grammar in the school curriculum. I would like to ask here if I am going to manually write a Python interpreter what skill points do I need to master?

Reply content:

For the implementation of an interpreter, the parser part is actually relatively boring work, if you skip this part of the implementation, it may be easier to get started.
Vinzenz/libpypa GitHub Can try this project, independent Python parser,c++ write, the current Dropbox Python implementation Pyston, is to use this library as a Python parser.
Alternatively, you can directly call CPython's AST module, export the parse good AST to a file, and read it directly with the interpreter that you have implemented. That's what Pyston did before he introduced Libpypa.
There is another way of thinking that directly implements a Python bytecode interpreter, the parse. pyc file is available.

What you need to know then is the Python object model (for example, how a Python object is represented in memory). If it is the C language to achieve it is probably "everything is a heap of allocated structure" on it.
The remaining piece is a GC, and you need to know about the common centralized GC implementation scenario. The simplest should be the reference count. If it is a C/s implementation, but also directly with the Boehm GC, this piece of itself will not be tube.

The relevant Python implementations:
Jython and IronPython Needless to say, the implementation of Python on the JVM and. NET two platforms, if familiar to these two platforms, can be directly referenced.
Dropbox's pyston,c++ implementation, using LLVM as the CodeGen, because C + + is relatively difficult, and there is also a JIT part, is not suitable to get started.
Micronpython, https:// micropython.org/ , specifically for embedded chip implementation of Python,ansi C, can be studied.
Tinypy Tinypy:: Home , the 64k code implementation of the more complete Python, can be referenced.

Also recommended Python source code Anatomy of this book, the content is very comprehensive Python source code analysis (watercress)

Vczh Free Script

Vczh Free Script 2.0 Beta release


Look at this, the sophomore wrote, except the grammar is not the same as there is no difference.

Parser is recommended for use with PEG, which can be easily processed by indentation. Do not consider the syntax analysis this step, want to focus on understanding the Python object model, you can go to see the Python source code analysis.
  • 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.