Much like the parser module exposes the Python parser, this PEP proposes that the parser generator used to create the Python parser, pgen, be exposed as a module in Python.
Through the course of Pythonic history, there have been numerous discussions about the creation of a Python compiler [1]. These have resulted in several implementations of Python parsers, most notably the parser module currently provided in the Python standard library [2] and Jeremy Hylton’s compiler module [3]. However, while multiple language changes have been proposed [4] [5], experimentation with the Python syntax has lacked the benefit of a Python binding to the actual parser generator used to build Python.
By providing a Python wrapper analogous to Fred Drake Jr.’s parser wrapper, but targeted at the pgen library, the following assertions are made:
The proposed module will be called pgen. The pgen module will contain the following functions:
The parseGrammarFile() function will read the file pointed to by fileName and create an AST object. The AST nodes will contain the nonterminal, numeric values of the parser generator meta-grammar. The output AST will be an instance of the AST extension class as provided by the parser module. Syntax errors in the input file will cause the SyntaxError exception to be raised.
The parseGrammarString() function will follow the semantics of the parseGrammarFile(), but accept the grammar text as a string for input, as opposed to the file name.
The buildParser() function will accept an AST object for input and return a DFA (deterministic finite automaton) data structure. The DFA data structure will be a C extension class, much like the AST structure is provided in the parser module. If the input AST does not conform to the nonterminal codes defined for the pgen meta-grammar, buildParser() will throw a ValueError exception.
The parseFile() function will essentially be a wrapper for the PyParser_ParseFile() C API function. The wrapper code will accept the DFA C extension class, and the file name. An AST instance that conforms to the lexical values in the token module and the nonterminal values contained in the DFA will be output.
The parseString() function will operate in a similar fashion to the parseFile() function, but accept the parse text as an argument. Much like parseFile() will wrap the PyParser_ParseFile() C API function, parseString() will wrap the PyParser_ParseString() function.
The symbolToStringMap() function will accept a DFA instance and return a dictionary object that maps from the DFA’s numeric values for its nonterminals to the string names of the nonterminals as found in the original grammar specification for the DFA.
The stringToSymbolMap() function output a dictionary mapping the nonterminal names of the input DFA to their corresponding numeric values.
Extra credit will be awarded if the map generation functions and parsing functions are also methods of the DFA extension class.
A cunning plan has been devised to accomplish this enhancement:
Under this proposal, would be designers of Python 3000 will still be constrained to Python’s lexical conventions. The addition, subtraction or modification of the Python lexer is outside the scope of this PEP.
No reference implementation is currently provided. A patch was provided at some point in http://sourceforge.net/tracker/index.php?func=detail&aid=599331&group_id=5470&atid=305470 but that patch is no longer maintained.
This document has been placed in the public domain.
Source: https://github.com/python/peps/blob/main/peps/pep-0269.rst
Last modified: 2025-02-01 08:55:40 UTC