[Python] try to add keyword to python's pass_stmt like book cpython internals

Discussão em 'Python' iniciado por Stack, Maio 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying book cpython internals's example: cpython internals ,you can find it at the page 72, an example that add keyword proceed to pass statement. I am using the newest version of cpython(3.14)instead of the book's.There are some differences in the source code,but the whole structure is similar.

    I do the folloing: add new keyword 'xxxxxx' to the simple_stmt.

    simple_stmt[stmt_ty] (memo):
    | assignment
    | &"type" type_alias
    | e=star_expressions { _PyAST_Expr(e, EXTRA) }
    | &'return' return_stmt
    | &('import' | 'from') import_stmt
    | &'raise' raise_stmt
    | &('pass'|'xxxxxx') pass_stmt
    | &'del' del_stmt
    | &'yield' yield_stmt
    | &'assert' assert_stmt
    | &'break' break_stmt
    | &'continue' continue_stmt
    | &'global' global_stmt
    | &'nonlocal' nonlocal_stmt


    and add the new keyword case in the pass_stmt:

    pass_stmt[stmt_ty]:
    | 'pass' { _PyAST_Pass(EXTRA) }
    | 'xxxxxx' { _PyAST_Pass(EXTRA) }


    this works. I can use xxxxxx to replace pass in the new python.But when i try this:

    pass_stmt[stmt_ty]:
    | ('pass'|'xxxxxx') { _PyAST_Pass(EXTRA) }


    it fails:

    Parser/parser.c: In function ‘pass_stmt_rule’:
    Parser/parser.c:2870:18: error: assignment to ‘stmt_ty’ {aka ‘struct _stmt *’} from incompatible pointer type ‘Token *’ [-Wincompatible-pointer-types]
    2870 | _res = _keyword;
    | ^
    Parser/parser.c:2889:18: error: assignment to ‘stmt_ty’ {aka ‘struct _stmt *’} from incompatible pointer type ‘Token *’ [-Wincompatible-pointer-types]
    2889 | _res = _keyword;
    |


    It seemed that the second try produced code that just returned the keyword,like"xxxxxx" or "pass",not _PyAST_Pass(EXTRA) .Why?

    Continue reading...

Compartilhe esta Página