Changing CPython's grammar
There's more to changing Python's grammar than editing
. Below is a checklist of things that may need to change.
Note
Many of these changes require re-generating some of the derived files. If things mysteriously don't work, it may help to run make clean.
Checklist
: The grammar definition, with actions that build AST nodes. After changing it, run make regen-pegen (or build.bat --regen on Windows), to regenerate
. (This runs Python's parser generator,
).
is a place for adding new token types. After changing it, run make regen-token to regenerate
Include/internal/pycore_token.h
,
,
and
. If you change both python.gram and Tokens, run make regen-token before make regen-pegen. On Windows, build.bat --regen will regenerate both at the same time.
may need changes to match the grammar. Then run make regen-ast to regenerate
and
.
contains the tokenization code. This is where you would add a new type of comment or string literal, for example.
will need changes to validate AST objects involved with the grammar change.
will need changes to unparse AST involved with the grammar change ("unparsing" is used to turn annotations into strings per
.
The
may need to change when there are changes to the AST.
_Unparser in the
file may need changes to accommodate any modifications in the AST nodes.
may need to be updated to reflect changes to AST nodes.
Add some usage of your new syntax to test_grammar.py.
Certain changes may require tweaks to the library module
.
needs changes to match changes to the tokenizer.
Documentation must be written! Specifically, one or more of the pages in
will need to be updated.