What is the meaning of `~` in python grammar
Asked Answered
I

1

7

I was going through the python grammer specification and find the following statement

for_stmt:
    | 'for' star_targets 'in' ~ star_expressions ':' [TYPE_COMMENT] block [else_block]

What does ~ means in this grammar rule?. The other symbols used in the grammer(like &, !, |) are already documented but not ~.

The notation is a mixture of EBNF and PEG. In particular, & followed by a symbol, token or parenthesized group indicates a positive lookahead (i.e., is required to match but not consumed), while ! indicates a negative lookahead (i.e., is required not to match). We use the | separator to mean PEG’s “ordered choice” (written as / in traditional PEG grammars)

Individuation answered 8/9, 2021 at 6:5 Comment(2)
@buran This is a question about EBNF/PEG, not Python syntax.Rouvin
@theberzi, my badStain
N
9

It's documented in PEP 617 under Grammar Expressions:

~

Commit to the current alternative, even if it fails to parse.

rule_name: '(' ~ some_rule ')' | some_alt

In this example, if a left parenthesis is parsed, then the other alternative won’t be considered, even if some_rule or ‘)’ fail to be parsed.

The ~ basically indicates that once you reach it, you're locked into the particular rule and cannot move onto the next rule if the parse fails. PEP 617 mentions earlier that | some_alt can be written in the next line.

Neutron answered 8/9, 2021 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.