[Python] Syntactically required and semantically relevant tokens in Python grammars

Discussão em 'Python' iniciado por Stack, Setembro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to parse code into AST. I want to keep minimum keywords and delimiters in the AST while keeping the semantics.

    In Python function definition def foo():, the last : is syntactically required but also redundant in semantic structure, which means it can be omitted in the AST and reconstructed from the AST.

    But I also notice that the : in slice expression start:end is semantically relevant that it can't be omitted in the AST. Removing it will lead to ambiguous semantic; take index: and :index, for example.

    I wonder if there are more keywords or delimiters in Python grammar that share a similar property to : in slice expressions?

    Tree-sitter is what I am using for parsing.

    Continue reading...

Compartilhe esta Página