PEP 306 – How to Change Python’s Grammar
- How to Change Python’s Grammar
- Michael Hudson <mwh at python.net>, Jack Diederich <jackdied at gmail.com>, Nick Coghlan <ncoghlan at gmail.com>, Benjamin Peterson <benjamin at python.org>
This PEP has been moved to the Python dev guide .
There’s more to changing Python’s grammar than editing
Python/compile.c. This PEP aims to be a
checklist of places that must also be fixed.
It is probably incomplete. If you see omissions, just add them if you can – you are not going to offend the author’s sense of ownership. Otherwise submit a bug or patch and assign it to mwh.
This PEP is not intended to be an instruction manual on Python grammar hacking, for several reasons.
People are getting this wrong all the time; it took well over a
year before someone noticed  that adding the floor division
//) broke the
Grammar/Grammar: OK, you’d probably worked this one out :)
Parser/Python.asdlmay need changes to match the
Python/ast.cwill need changes to create the AST objects involved with the
Lib/compiler/ast.pywill need matching changes to the pure-python AST objects.
Parser/pgenneeds to be rerun to regenerate
Python/graminit.c. (make should handle this for you.)
Python/symbtable.c: This handles the symbol collection pass that happens immediately before the compilation pass.
Python/compile.c: You will need to create or modify the
compiler_*functions to generate opcodes for your productions.
- You may need to regenerate
parsermodule. Add some of your new syntax to
test_parser, bang on
Modules/parsermodule.cuntil it passes.
- Add some usage of your new syntax to
compilerpackage. A good test is to compile the standard library and test suite with the
compilerpackage and then check it runs. Note that this only needs to be done in Python 2.x.
- If you’ve gone so far as to change the token structure of
Python, then the
Lib/tokenizer.pylibrary module will need to be changed.
- Certain changes may require tweaks to the library module
- Documentation must be written!
- After everything’s been checked in, you’re likely to see a new
Python/Python-ast.c. This is because this (generated) file contains the SVN version of the source from which it was generated. There’s no way to avoid this; you just have to submit this file separately.
- CPython Developer’s Guide: Changing CPython’s Grammar https://devguide.python.org/grammar/
- SF Bug #676521, parser module validation failure https://bugs.python.org/issue676521
This document has been placed in the public domain.
Last modified: 2018-07-21 23:07:02 GMT