2003-01-29 10:07:17 -05:00
PEP: 306
Title: How to Change Python's Grammar
Version: $Revision$
Last-Modified: $Date$
2023-10-11 08:05:51 -04:00
Author: Michael Hudson <mwh@python.net>, Jack Diederich <jackdied@gmail.com>, Alyssa Coghlan <ncoghlan@gmail.com>, Benjamin Peterson <benjamin@python.org>
2011-01-17 19:31:57 -05:00
Status: Withdrawn
2003-01-29 10:07:17 -05:00
Type: Informational
2017-01-10 01:52:57 -05:00
Content-Type: text/x-rst
2003-01-29 10:07:17 -05:00
Created: 29-Jan-2003
2003-01-30 10:22:56 -05:00
Post-History: 30-Jan-2003
2003-01-29 10:07:17 -05:00
2011-01-17 19:31:57 -05:00
Note
2017-01-10 01:52:57 -05:00
====
2011-01-17 19:31:57 -05:00
2017-01-10 01:52:57 -05:00
This PEP has been moved to the Python dev guide [1]_ .
2011-01-17 19:31:57 -05:00
2003-01-29 10:07:17 -05:00
Abstract
2017-01-10 01:52:57 -05:00
========
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
There's more to changing Python's grammar than editing
2017-04-05 12:14:26 -04:00
`` Grammar/Grammar `` and `` Python/compile.c `` . This PEP aims to be a
2017-01-10 01:52:57 -05:00
checklist of places that must also be fixed.
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
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.
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
This PEP is not intended to be an instruction manual on Python
grammar hacking, for several reasons.
2003-01-29 10:07:17 -05:00
Rationale
2017-01-10 01:52:57 -05:00
=========
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
People are getting this wrong all the time; it took well over a
year before someone noticed [2]_ that adding the floor division
2017-04-05 12:14:26 -04:00
operator (`` // `` ) broke the `` parser `` module.
2003-01-29 10:07:17 -05:00
Checklist
2017-01-10 01:52:57 -05:00
=========
2003-01-29 10:07:17 -05:00
2017-04-05 12:14:26 -04:00
- `` Grammar/Grammar `` : OK, you'd probably worked this one out :)
2003-01-29 10:07:17 -05:00
2017-04-05 12:14:26 -04:00
- `` Parser/Python.asdl `` may need changes to match the `` Grammar `` . Run
`` make `` to regenerate `` Include/Python-ast.h `` and
`` Python/Python-ast.c `` .
2003-01-29 10:07:17 -05:00
2017-04-05 12:14:26 -04:00
- `` Python/ast.c `` will need changes to create the AST objects
involved with the `` Grammar `` change. `` Lib/compiler/ast.py `` will
2017-01-10 01:52:57 -05:00
need matching changes to the pure-python AST objects.
2007-03-01 17:39:00 -05:00
2017-04-05 12:14:26 -04:00
- `` Parser/pgen `` needs to be rerun to regenerate `` Include/graminit.h ``
and `` Python/graminit.c `` . (make should handle this for you.)
2007-03-01 17:39:00 -05:00
2017-04-05 12:14:26 -04:00
- `` Python/symbtable.c `` : This handles the symbol collection pass
2017-01-10 01:52:57 -05:00
that happens immediately before the compilation pass.
2007-03-01 17:39:00 -05:00
2017-04-05 12:14:26 -04:00
- `` Python/compile.c `` : You will need to create or modify the
2017-01-10 01:52:57 -05:00
`` compiler_* `` functions to generate opcodes for your productions.
2007-03-01 17:39:00 -05:00
2017-04-05 12:14:26 -04:00
- You may need to regenerate `` Lib/symbol.py `` and/or `` Lib/token.py ``
and/or `` Lib/keyword.py `` .
2003-01-29 10:07:17 -05:00
2017-04-05 12:14:26 -04:00
- The `` parser `` module. Add some of your new syntax to `` test_parser `` ,
bang on `` Modules/parsermodule.c `` until it passes.
2008-11-03 10:28:02 -05:00
2017-04-05 12:14:26 -04:00
- Add some usage of your new syntax to `` test_grammar.py `` .
2003-01-29 10:07:17 -05:00
2017-04-05 12:14:26 -04:00
- The `` compiler `` package. A good test is to compile the standard
library and test suite with the `` compiler `` package and then check
2017-01-10 01:52:57 -05:00
it runs. Note that this only needs to be done in Python 2.x.
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
- If you've gone so far as to change the token structure of
2017-04-05 12:14:26 -04:00
Python, then the `` Lib/tokenizer.py `` library module will need to
2017-01-10 01:52:57 -05:00
be changed.
2003-01-30 05:27:45 -05:00
2017-01-10 01:52:57 -05:00
- Certain changes may require tweaks to the library module
`` pyclbr `` .
2003-01-30 05:27:45 -05:00
2017-01-10 01:52:57 -05:00
- Documentation must be written!
2004-01-21 13:11:25 -05:00
2017-01-10 01:52:57 -05:00
- After everything's been checked in, you're likely to see a new
2017-04-05 12:14:26 -04:00
change to `` Python/Python-ast.c `` . This is because this
2017-01-10 01:52:57 -05:00
(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.
2007-04-16 13:08:51 -04:00
2003-01-29 10:07:17 -05:00
References
2017-01-10 01:52:57 -05:00
==========
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
.. [1] CPython Developer's Guide: Changing CPython's Grammar
2018-07-21 19:07:02 -04:00
https://devguide.python.org/grammar/
2017-01-10 01:52:57 -05:00
.. [2] SF Bug #676521, parser module validation failure
2018-07-21 19:07:02 -04:00
https://bugs.python.org/issue676521
2003-01-29 10:07:17 -05:00
Copyright
2017-01-10 01:52:57 -05:00
=========
This document has been placed in the public domain.
2003-01-29 10:07:17 -05:00
2017-01-10 01:52:57 -05:00
..
Local Variables:
mode: indented-text
indent-tabs-mode: nil
sentence-end-double-space: t
fill-column: 70
End: