Skip to content

Commit

Permalink
Improvements to "Guide to CPython’s Parser" (python#763)
Browse files Browse the repository at this point in the history
* put references to names from rules into ``backticks``

* two typos

* mention that ~ is called "the cut"

(The grammar gives that name, and I as a prolog programmer was looking
for the terminology ;-))
  • Loading branch information
cfbolz authored Nov 19, 2021
1 parent 1ff3dc7 commit 9d880b6
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions parser.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ Python-style comments.
``e1 e2``
'''''''''

Match e1, then match e2.
Match ``e1``, then match ``e2``.

::

Expand All @@ -178,7 +178,7 @@ Match e1, then match e2.
``e1 | e2``
'''''''''''

Match e1 or e2.
Match ``e1`` or ``e2``.

The first alternative can also appear on the line after the rule name
for formatting purposes. In that case, a \| must be used before the
Expand All @@ -193,7 +193,7 @@ first alternative, like so:
``( e )``
'''''''''

Match e.
Match ``e``.

::

Expand All @@ -209,7 +209,7 @@ operator together with the repeat operators:
``[ e ] or e?``
'''''''''''''''

Optionally match e.
Optionally match ``e``.

::

Expand All @@ -225,7 +225,7 @@ optional:
``e*``
''''''

Match zero or more occurrences of e.
Match zero or more occurrences of ``e``.

::

Expand All @@ -234,7 +234,7 @@ Match zero or more occurrences of e.
``e+``
''''''

Match one or more occurrences of e.
Match one or more occurrences of ``e``.

::

Expand All @@ -243,7 +243,7 @@ Match one or more occurrences of e.
``s.e+``
''''''''

Match one or more occurrences of e, separated by s. The generated parse
Match one or more occurrences of ``e``, separated by ``s``. The generated parse
tree does not include the separator. This is otherwise identical to
``(e (s e)*)``.

Expand All @@ -256,14 +256,14 @@ tree does not include the separator. This is otherwise identical to

.. _peg-positive-lookahead:

Succeed if e can be parsed, without consuming any input.
Succeed if ``e`` can be parsed, without consuming any input.

``!e``
''''''

.. _peg-negative-lookahead:

Fail if e can be parsed, without consuming any input.
Fail if ``e`` can be parsed, without consuming any input.

An example taken from the Python grammar specifies that a primary
consists of an atom, which is not followed by a ``.`` or a ``(`` or a
Expand All @@ -276,14 +276,15 @@ consists of an atom, which is not followed by a ``.`` or a ``(`` or a
``~``
''''''

Commit to the current alternative, even if it fails to parse.
Commit to the current alternative, even if it fails to parse (this is called
the "cut").

::

rule_name: '(' ~ some_rule ')' | some_alt

In this example, if a left parenthesis is parsed, then the other
alternative won’t be considered, even if some_rule or ‘)’ fail to be
alternative won’t be considered, even if some_rule or ``)`` fail to be
parsed.

Left recursion
Expand Down Expand Up @@ -343,7 +344,7 @@ inside curly-braces, which specifies the return value of the alternative::
| first_alt1 first_alt2 { first_alt1 }
| second_alt1 second_alt2 { second_alt1 }

If the action is ommited, a default action is generated:
If the action is omitted, a default action is generated:

* If there's a single name in the rule, it gets returned.

Expand Down

0 comments on commit 9d880b6

Please sign in to comment.