cpython/Doc/reference/grammar.rst
Petr Viktorin f0a0467c17
gh-143054: Disallow non-top-level Cut for now (GH-143622)
The behaviour of Cut in nested parentheses, Repeat, Opt, and similar
is somewhat chaotic. Apparently even the academic papers on PEG aren't
as clear as they could be.

And it doesn't really matter. Python only uses top-level cuts.
When that changes, we can clarify as much as necessary (and even
change the implementation to make sense for what we'll need).

Document that this is deliberately unspecified, and add a test to
make sure any decision is deliberate, tested and documented.
2026-01-13 13:21:59 +01:00

29 lines
995 B
ReStructuredText

.. _full-grammar-specification:
Full Grammar specification
==========================
This is the full Python grammar, derived directly from the grammar
used to generate the CPython parser (see :source:`Grammar/python.gram`).
The version here omits details related to code generation and
error recovery.
The notation used here is the same as in the preceding docs,
and is described in the :ref:`notation <notation>` section,
except for an extra complication:
* ``~`` ("cut"): commit to the current alternative; fail the rule
if the alternative fails to parse
Python mainly uses cuts for optimizations or improved error
messages. They often appear to be useless in the listing below.
.. see gh-143054, and CutValidator in the source, if you want to change this:
Cuts currently don't appear inside parentheses, brackets, lookaheads
and similar.
Their behavior in these contexts is deliberately left unspecified.
.. literalinclude:: ../../Grammar/python.gram
:language: peg