2

I'm not a novice programmer and I know what booleans are and how to use them. Trouble is, I can't quite understand what is implied in Python's documentation regarding boolean operations:

or_test  ::=  and_test | or_test "or" and_test
and_test ::=  not_test | and_test "and" not_test
not_test ::=  comparison | "not" not_test

What does all this recursion mean? Why should there be an "and_test" inside of an or_test? The same question about "not_test" in the description of and_test The third line makes even less sense to me. Could anyone please guide me through these lines so that I can finally understand this somewhat unnecessarily recursive and tangled notation

1 Answer 1

2

This is part of the larger language grammar and in essence specifies the operator precedence.

It means that and binds tighter than or does. Note that or_test does not have to contain and, it contains the and_test rule, which is constructed from either an not_test rule or and_test "and" not_test. not_test itself constructed in a similar manner, etc.

Also see the Notation section of the reference documentation.

Sign up to request clarification or add additional context in comments.

3 Comments

Do I get it right that this EBNF notation doesn't actually indicate the syntax of boolean operations but rather the order in which Python's interpreter evaluates expressions inside of a construction?
Well, it does both. The syntax for the AND boolean operation is spelled and, for example. Expression grammar rules are generally very simple; compare these with the rules for compound statements.
Thank you, Martijn! You helped a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.