2.7 KiB
Changing CPython's grammar
There's more to changing Python's grammar than editing
Grammar/python.gram.
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
-
Grammar/python.gram: The grammar definition, with actions that build AST nodes. After changing it, runmake regen-pegen(orbuild.bat --regenon Windows), to regenerateParser/parser.c. (This runs Python's parser generator,Tools/peg_generator). -
Grammar/Tokensis a place for adding new token types. After changing it, runmake regen-tokento regenerateInclude/internal/pycore_token.h,Parser/token.c,Lib/token.pyandDoc/library/token-list.inc. If you change bothpython.gramandTokens, runmake regen-tokenbeforemake regen-pegen. On Windows,build.bat --regenwill regenerate both at the same time. -
Parser/Python.asdlmay need changes to match the grammar. Then runmake regen-astto regenerateInclude/internal/pycore_ast.handPython/Python-ast.c. -
Parser/lexer/contains the tokenization code. This is where you would add a new type of comment or string literal, for example. -
Python/ast.cwill need changes to validate AST objects involved with the grammar change. -
Python/ast_unparse.cwill need changes to unparse AST involved with the grammar change ("unparsing" is used to turn annotations into strings per PEP 563. -
The
compilermay need to change when there are changes to theAST. -
_Unparserin theLib/ast.pyfile may need changes to accommodate any modifications in the AST nodes. -
Doc/library/ast.rstmay 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
pyclbr. -
Lib/tokenize.pyneeds changes to match changes to the tokenizer. -
Documentation must be written! Specifically, one or more of the pages in
Doc/reference/will need to be updated.