I'm confused by what it said about the choice operator no longer being commutative. I thought it was always the case that A | B | C would evaluate A first, and only evaluate B if A was false, and so on.
It seems the article is copying-and-pasting a snippet from PEP 617 -- just in an imperfect and somewhat confusing way.
Basically, what the PEG parser does is change how grammar rules such as rule: A | B | C are used by the parser. Nothing is changing about the semantics of the Python language itself, including the semantics of the A | B | Cpython expression.
Using the old LL(1) parser, it didn't matter whether we encoded this part of the grammar as rule: A | B | C or rule: C | B | A or whatever. Since we can only look one token ahead, all three child rules needed to start with a different token to keep the grammar unambiguous, which as a side effect means the order in which we list the rules won't matter.
In contrast, the PEG parser doesn't have this "all child rules need to start with a different token" restriction since it effectively has infinite lookahead. So then how does it resolve ambiguities where multiple rules end up matching?
Well, Python's PEG parser implementation will try using each child rule in the order they're listed and pick the first matching one. And so, the order can now sometimes be meaningful which in turn means the operator is no longer commutative.
2
u/manifestsilence I use Python to try to forget my work languages. May 11 '20
I'm confused by what it said about the choice operator no longer being commutative. I thought it was always the case that A | B | C would evaluate A first, and only evaluate B if A was false, and so on.