I'm reading the Python documentation 2.76
This is the lexical syntax of the 3 kinds of 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
This is the lexical syntax for comparision:
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"
and the lexical syntax for or_expr:
and_expr ::= shift_expr | and_expr "&" shift_expr
xor_expr ::= and_expr | xor_expr "^" and_expr
or_expr ::= xor_expr | or_expr "|" xor_expr
The Notation in the document is explained here:
Now comes the question:
Is
not_test ::= comparison | "not" not_test
parsed likenot_test ::= comparison | ("not" not_test)
?If
1.
is true, a validcomparison
is also a validnot_test
.(e.g.1 < 2
is anot_test
even without anot
in it.)In addition, because a valid
and_test
can just be a single validnot_test
,1 < 2
is a validand_test
as well. The same goes for theor_test
.Then what is a valid
comparison
?1 < 2
fits the pattern obviously. And thoese bitwise comparison expr are aslo validcomparison
. What's in common is that the presense of at least one operator('>','<',or bitwise stuff) is required. (I'm not sure.)Here comes to the weird part. Consider
x and y
for example. According to
and_test ::= not_test | and_test "and" not_test
and_test ::= not_test | (and_test "and" not_test)
# parsed like this I believe?
If it is true that x and y
is a valid and_test
(it can never be a not_test
for the presence of and
), then x
must be a valid and_test
which can just be one valid not_test
. And y
must be a valid not_test
too.
A not_test
can either be a single comparison
or another not_test
preceeded by not
. So a not_test
is basically zero or more not
s followed by one comparison
. What matters now is the lexical syntax of comparison
.
According to 4.
, a comparison
must have at least one operator whatever. But this is conflict with the following example:
assign x = 3, y = 4 . 3 and 4
seems a valid and_test
.
But I don't see how 3
or 4
can be valid comparison
.
Where did I go wrong?