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:

  1. 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
    
  2. Edit the Grammar/Grammar, and add to the pass_stmt directive as follows:

     ...
     pass_stmt: 'pass' | 'proceed' | 'anythingyouwant'
     ...
    
  3. Regenerate the grammar by running:

     make regen-grammar
    
  4. Recompile the Python source code

     make -j2 -s
    
  5. Run the Python interpreter

     ./python.exe -X oldparser
    
  6. 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