What parser generator does CPython use?

2019-06-20 10:12发布

问题:

I was reading this page in the documentation, and noticed that it says

This is the full Python grammar, as it is read by the parser generator and used to parse Python source files

However, I'm having difficulty finding out what parser generator CPython uses. So what parser generator does CPython use? Are there other parser generators that would take the grammar on that page without any modifications?

回答1:

Python is open-source, so you can inspect the source code...

In the Python source directory is a "Parser" directory containing "Python.asdl" with the note

-- ASDL's four builtin types are identifier, int, string, object

There's also an "asdl.py" file in the same directory...

"""An implementation of the Zephyr Abstract Syntax Definition Language.

See http://asdl.sourceforge.net/ and
http://www.cs.princeton.edu/research/techreps/TR-554-97

Only supports top level module decl, not view.  I'm guessing that view
is intended to support the browser and I'm not interested in the
browser.

Changes for Python: Add support for module versions
"""

So it appears that it is a custom parser generator. LALR(1) parser generators are not so hard to write.