How to hack Python Grammar
This is a quick post about how you can hack the Python grammar and define your own constructs.
In this short tutorial we’ll be seeing how you can define your own words to replace the pass
statement in Python.
Here’s what you’ll need to do:
-
Start by cloning the CPython repository and installing the dev version of Python3
git clone https://github.com/python/cpython.git cd cpython ./configure make -j2 -s
-
Edit the
Grammar/Grammar
, and add to thepass_stmt
directive as follows:... pass_stmt: 'pass' | 'proceed' | 'anythingyouwant' ...
-
Regenerate the grammar by running:
make regen-grammar
-
Recompile the Python source code
make -j2 -s
-
Run the Python interpreter
./python.exe -X oldparser
-
Use the pass statement in your code and verify that it works, and feel proud!
def foo(): proceed foo()
That’s it! Pretty amazing isn’t it? 😃
References
- Take a look at this amazing book on CPython internals by Anthony Shaw for more details
- Talk by Pablo Salgado at EuroPython 2019 explaining everything about the Python parser generator