I am a computer major student. I came up with the idea of creating a python interpreter while learning the Compilation Principle Course, however, in our course, we only talk about irrelevant grammar, lexical analysis, syntax analysis, and four-element representation. We only talk about some manual conversion methods, I am not very familiar with some practical applications. I have learned that the python language is context-related, and there is no detailed explanation of this grammar in school courses. I would like to ask here what skills do I need to master if I want to manually write a python interpreter? I am a computer major student. I came up with the idea of creating a python interpreter while learning the Compilation Principle Course, however, in our course, we only talk about irrelevant grammar, lexical analysis, syntax analysis, and four-element representation. We only talk about some manual conversion methods, I am not very familiar with some practical applications. I have learned that the python language is context-related, and there is no detailed explanation of this grammar in school courses. I would like to ask here what skills do I need to master if I want to manually write a python interpreter? Reply: for implementing an interpreter, the parser part is actually relatively boring. If you skip this part, it may be easier to get started.
Vinzenz/libpypa · GitHub
You can try this project, which is written in an independent Python parser and C ++. Currently, Python of Dropbox implements Pyston and uses this library as the parser of Python.
In addition, you can directly call the ast module of CPython to export the AST of parse to the file, and then directly read it using the interpreter implemented by yourself. Pyston did this before introducing libpypa.
Another idea is to directly implement a Python bytecode interpreter. You can use the parse. pyc file.
Next, you need to understand the Python object model (for example, how a Python object is represented in memory ). If the C language is used for implementation, it is probably "Everything is a distributed structure on the stack.
The remaining part is GC. You need to understand common centralized GC implementation solutions. The simplest thing is the reference count. If it is implemented in C/C ++, you can also directly use Boehm GC, so you don't have to worry about it.
Related Python implementation:
Needless to say, Jython and IronPython are implemented in Python on JVM and. net platforms. If you are familiar with these two platforms, you can directly use them for reference.
The Pyston and C ++ implementations of Dropbox use LLVM as codegen, because C ++ is relatively difficult and JIT is not suitable for getting started.
MicronPython, https://micropython.org/
Specifically for Python and ansi C implemented by embedded chips, you can study it.
Tinypy: home
, 64 K code to achieve a more complete Python, you can refer.
In addition, we recommend that you analyze the Python source code. The content of this book is very comprehensive Python source code analysis (Douban)
.
Vczh Free Script
Vczh Free Script 2.0 beta released
Let's look at this. There is no difference except for the syntax.
PEG is recommended for Parser to facilitate indentation. If you want to focus on the python object model without considering the syntax analysis step, you can go to python source code analysis.