| 1 |
PyPy Parser |
| 2 |
=========== |
| 3 |
|
| 4 |
Overview |
| 5 |
-------- |
| 6 |
|
| 7 |
The PyPy parser includes a tokenizer and a recursive descent parser. |
| 8 |
|
| 9 |
|
| 10 |
Tokenizer |
| 11 |
--------- |
| 12 |
|
| 13 |
At the moment, the tokenizer is implemented as a single function |
| 14 |
(``generate_tokens`` in :source:`pypy/interpreter/pyparser/pytokenizer.py`) that builds |
| 15 |
a list of tokens. The tokens are then fed to the parser. |
| 16 |
|
| 17 |
|
| 18 |
Parser |
| 19 |
------ |
| 20 |
|
| 21 |
The parser is a simple LL(1) parser that is similar to CPython's. |
| 22 |
|
| 23 |
|
| 24 |
Building the Python grammar |
| 25 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 26 |
|
| 27 |
The python grammar is built at startup from the pristine CPython grammar file |
| 28 |
(see :source:`pypy/interpreter/pyparser/metaparser.py`). The grammar builder first |
| 29 |
represents the grammar as rules corresponding to a set of Nondeterministic |
| 30 |
Finite Automatons (NFAs). It then converts them to a set of Deterministic |
| 31 |
Finite Automatons (DFAs). The difference between a NFA and a DFA is that a NFA |
| 32 |
may have several possible next states for any given input while a DFA may only |
| 33 |
have one. DFAs are therefore more limiting, but far more efficient to use in |
| 34 |
parsing. Finally, the assigns the grammar builder assigns each DFA state a |
| 35 |
number and packs them into a list for the parser to use. The final product is |
| 36 |
an instance of the ``Grammar`` class in :source:`pypy/interpreter/pyparser/parser.py`. |
| 37 |
|
| 38 |
|
| 39 |
Parser implementation |
| 40 |
~~~~~~~~~~~~~~~~~~~~~ |
| 41 |
|
| 42 |
The workhorse of the parser is the ``add_token`` method of the ``Parser`` class. |
| 43 |
It tries to find a transition from the current state to another state based on |
| 44 |
the token it receives as a argument. If it can't find a transition, it checks |
| 45 |
if the current state is accepting. If it's not, a ``ParseError`` is |
| 46 |
raised. When parsing is done without error, the parser has built a tree of |
| 47 |
``Node``. |
| 48 |
|
| 49 |
|
| 50 |
Parsing Python |
| 51 |
~~~~~~~~~~~~~~ |
| 52 |
|
| 53 |
The glue code between the tokenizer and the parser as well as extra Python |
| 54 |
specific code is in :source:`pypy/interpreter/pyparser/pyparse.py`. The |
| 55 |
``parse_source`` method takes a string of Python code and returns the parse |
| 56 |
tree. It also detects the coding cookie if there is one and decodes the source. |
| 57 |
Note that __future__ imports are handled before the parser is invoked by |
| 58 |
manually parsing the source in :source:`pypy/interpreter/pyparser/future.py`. |
| 59 |
|
| 60 |
|
| 61 |
Compiler |
| 62 |
-------- |
| 63 |
|
| 64 |
The next step in generating Python bytecode is converting the parse tree into an |
| 65 |
Abstract Syntax Tree (AST). |
| 66 |
|
| 67 |
|
| 68 |
Building AST |
| 69 |
~~~~~~~~~~~~ |
| 70 |
|
| 71 |
Python's AST is described in :source:`pypy/interpreter/astcompiler/tools/Python.asdl`. |
| 72 |
From this definition, :source:`pypy/interpreter/astcompiler/tools/asdl_py.py` generates |
| 73 |
:source:`pypy/interpreter/astcompiler/ast.py`, which RPython classes for the compiler |
| 74 |
as well as bindings to application level code for the AST. Some custom |
| 75 |
extensions to the AST classes are in |
| 76 |
:source:`pypy/interpreter/astcompiler/asthelpers.py`. |
| 77 |
|
| 78 |
:source:`pypy/interpreter/astcompiler/astbuilder.py` is responsible for converting |
| 79 |
parse trees into AST. It walks down the parse tree building nodes as it goes. |
| 80 |
The result is a toplevel ``mod`` node. |
| 81 |
|
| 82 |
|
| 83 |
AST Optimization |
| 84 |
~~~~~~~~~~~~~~~~ |
| 85 |
|
| 86 |
:source:`pypy/interpreter/astcompiler/optimize.py` contains the AST optimizer. It does |
| 87 |
constant folding of expressions, and other simple transformations like making a |
| 88 |
load of the name "None" into a constant. |
| 89 |
|
| 90 |
|
| 91 |
Symbol analysis |
| 92 |
~~~~~~~~~~~~~~~ |
| 93 |
|
| 94 |
Before writing bytecode, a symbol table is built in |
| 95 |
:source:`pypy/interpreter/astcompiler/symtable.py`. It determines if every name in the |
| 96 |
source is local, implicitly global (no global declaration), explicitly global |
| 97 |
(there's a global declaration of the name in the scope), a cell (the name in |
| 98 |
used in nested scopes), or free (it's used in a nested function). |
| 99 |
|
| 100 |
|
| 101 |
Bytecode generation |
| 102 |
~~~~~~~~~~~~~~~~~~~ |
| 103 |
|
| 104 |
Bytecode is emitted in :source:`pypy/interpreter/astcompiler/codegen.py`. Each |
| 105 |
bytecode is represented temporarily by the ``Instruction`` class in |
| 106 |
:source:`pypy/interpreter/astcompiler/assemble.py`. After all bytecodes have been |
| 107 |
emitted, it's time to build the code object. Jump offsets and bytecode |
| 108 |
information like the line number table and stack depth are computed. Finally, |
| 109 |
everything is passed to a brand new ``PyCode`` object. |