Skip to content

"python3 -m pegen python" generates a Python parser that contains invalid code #133560

Closed as not planned

Description

@alexprengere

Bug report

Bug description:

I am currently experimenting with the cpython parser/tokenizer, following the parser documentation:

$ python3.14 -m pegen python  ../../Grammar/python.gram

This generates a parser.py file, that contains remnants of C code:

# line 34
return _PyAST_FunctionType ( a , b , p -> arena )

# end of file
void *
_PyPegen_parse(Parser *p)
{
    // Initialize keywords
    p->keywords = reserved_keywords;
    p->n_keyword_lists = n_keyword_lists;
    p->soft_keywords = soft_keywords;

    // Run parser

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs

Activity

  1. sobolevn commented on May 7, 2025

    @sobolevn
    Member

    This looks like a possible duplicate of #96424

  2. alexprengere commented on May 7, 2025

    @alexprengere
    ContributorAuthor

    OK I get it now, this is definitely a duplicate.
    I think perhaps the instructions in parser.md are a bit misleading, as they suggest that when you are "adding some new rules to the grammar" (e.g. updating the python.gram), "it is a good idea to experiment first by generating a Python parser", when in fact you cannot use python -m pegen python on the modified python.gram you may be working on.
    I will see if I can improve the docs once I get a better understanding on how to leverage pegen to generate a Python parser.

  3. lysnikolaou commented on May 7, 2025

    @lysnikolaou
    Member

    "it is a good idea to experiment first by generating a Python parser", when in fact you cannot use python -m pegen python on the modified python.gram you may be working on.

    You can do so using the pure Python version of the grammar. I don't think it's up-to-date with the latest updates to the CPython grammar though. I need to find some time to update it.

    I think we can close this as won't fix for now. If you wanna update the docs in the future, please open a new issue for that.

  4. sobolevn commented on May 7, 2025

    @sobolevn
    Member

    However, since this is not the first time people ask this - I propose adding a clear error message that this can't be done.

  5. added a commit that references this issue on May 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions