我正在阅读 Python 文档 2.76
or_test ::= and_test | or_test "or" and_test
and_test ::= not_test | and_test "and" not_test
not_test ::= comparison | "not" not_test
comparison ::= or_expr ( comp_operator or_expr )*
comp_operator ::= "<" | ">" | "==" | ">=" | "<=" | "<>" | "!="
| "is" ["not"] | ["not"] "in"
and_expr ::= shift_expr | and_expr "&" shift_expr
xor_expr ::= and_expr | xor_expr "^" and_expr
or_expr ::= xor_expr | or_expr "|" xor_expr
文档中的符号在这里解释:
竖线 (|) 用于分隔备选方案;它是这种表示法中约束力最小的运算符。
现在问题来了:
被
not_test ::= comparison | "not" not_test解析为not_test ::= comparison | ("not" not_test)?如果
1.为真,则有效comparison也是有效的not_test。(例如1 < 2,not_test即使其中没有 anot。)此外,因为 valid
and_test只能是一个 validnot_test,1 < 2所以也是一个 validand_test。对于or_test.那么什么是有效的
comparison?1 < 2显然符合模式。并且这些按位比较 expr 也是有效的comparison。共同点是至少需要一个运算符('>'、'<' 或按位的东西)。(我不知道。)这是奇怪的部分。例如考虑
x and y。根据
and_test ::= not_test | and_test "and" not_test
and_test ::= not_test | (and_test "and" not_test) # 我相信这样解析?
如果它是x and y一个有效的and_test(它永远不可能是not_test存在的and),那么它x必须是一个有效的and_test,它只能是一个有效not_test的。并且y必须也是有效的not_test。
Anot_test可以是单一的,也可以是前面有 的comparison另一个。所以 a基本上是零个或多个s 后跟一个。现在重要的是.not_testnotnot_testnotcomparisoncomparison
根据4.,一个comparison必须至少有一个运算符。但这与以下示例冲突:
分配 x = 3, y = 4 。3 and 4似乎是一个有效的and_test.
但我不知道如何3或4可以是有效comparison的。我哪里做错了?