Python 中的 __peg_parser__ 是什么?

Abh*_*wal 55 python python-3.x python-3.9

我使用keyword内置模块来获取当前 Python 版本的所有关键字的列表。这就是我所做的:

>>> import keyword
>>> print(keyword.kwlist)
['False', 'None', 'True', '__peg_parser__', 'and', 'as', 'assert', 'async', 'await', 'break', 'class', 'continue', 'def', 'del', 'elif', 'else', 'except', 'finally', 'for', 'from', 'global', 'if', 'import', 'in', 'is', 'lambda', 'nonlocal', 'not', 'or', 'pass', 'raise', 'return', 'try', 'while', 'with', 'yield']
Run Code Online (Sandbox Code Playgroud)

keyword.kwlist列表中有__peg_parser__. 所以为了看看它的作用,我__peg_parser__在 Windows 上输入了一个 Python 3.9 解释器(你将在 Mac OS 和 Linux 上得到相同的输出),这就是得到的:

>>> __peg_parser__
  File "<stdin>", line 1
    __peg_parser__
    ^
SyntaxError: You found it!
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,什么是__peg_parser__,为什么我得到SyntaxError: You found it!

Sha*_*ger 31

这是一个与新 PEG 解析器的推出有关的复活节彩蛋。复活节彩蛋以及旧的 LL(1) 解析器将在 3.10 中删除


U10*_*ard 13

圭多在github上公布新PEG解析器。

它也在Python PEP 上

正如它所提到的:

该 PEP 建议用新的基于 PEG 的解析器替换当前的基于 LL(1) 的 CPython 解析器。这个新的解析器将允许消除当前语法中存在的多个“黑客”,以规避 LL(1) 限制。它将大大降低与编译管道相关的某些领域的维护成本,例如语法、解析器和 AST 生成。新的 PEG 解析器还将取消对当前 Python 语法的 LL(1) 限制。

Python 3.9 What's new页面中也提到过。

在 Python 3.10 中,LL(1)解析器将被删除。Python 3.9 使用新的解析器,基于 PEG 而不是 LL(1)。

在 Python 3.6 中,它没有定义:

>>> __peg_parser__
Traceback (most recent call last):
  File "<pyshell#13>", line 1, in <module>
    __peg_parser__
NameError: name '__peg_parser__' is not defined
>>> 
Run Code Online (Sandbox Code Playgroud)


Abh*_*wal 7

什么是__peg_parser__

它是 Python 中的复活节彩蛋(Peg Parser 的),用于发布新的Peg Parser时。正如本讨论中提到的,它将在 Python 3.10 中删除。

在调用复活节彩蛋之前__new_parser__,但已更改为__peg_parser__,以使其成为消息中提到的未来证明:

newex或者ng不是真正的未来证明名称。我们可以将关键字重命名为__peg_parser__吗?

你为什么得到SyntaxError: You found it!

你得到,SyntaxError: You found it!因为它是复活节彩蛋的一部分。

将来会被删除吗?

由于LL(1) 解析器将被新的Peg Parser替换,它将在 Python 3.10 中删除。

__peg_parser__ 在 Python 的早期和晚期版本中

它在早期版本的 Python 中不存在。

Python 3.8 及更早版本:

>>> __peg_parser__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__peg_parser__' is not defined
Run Code Online (Sandbox Code Playgroud)

蟒蛇 3.9:

>>> __peg_parser__
  File "<stdin>", line 1
    __peg_parser__
    ^
SyntaxError: You found it!
Run Code Online (Sandbox Code Playgroud)

Python 3.10:

>>> __peg_parser__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__peg_parser__' is not defined
Run Code Online (Sandbox Code Playgroud)