python赋值语句合法条件_Python元组赋值和签入条件语句

考虑到if 1 == 1,2:的一个例子,它应该导致SyntaxError,下面是{a1}:if 1 == 1,2:

使用if_stmt: 'if' test ':' suite ('elif' test ':' suite)* ['else' ':' suite],我们可以移动if关键字并开始解析1 == 1,2:

对于test规则,只有第一个生产匹配:

^{pr2}$

然后我们得到:or_test: and_test ('or' and_test)*

然后进入and_test:and_test: not_test ('and' not_test)*

现在我们就进入not_test:not_test: 'not' not_test | comparison

注意,我们的输入是1 == 1,2:,因此第一个产品不匹配,我们检查另一个:(1)comparison: expr (comp_op expr)*

继续下一步(我们只接受第一个非终端,因为零星或更多星需要一个我们输入中根本没有的终端):expr: xor_expr ('|' xor_expr)*

xor_expr: and_expr ('^' and_expr)*

and_expr: shift_expr ('&' shift_expr)*

shift_expr: arith_expr (('<>') arith_expr)*

arith_expr: term (('+'|'-') term)*

term: factor (('*'|'/'|'%'|'//') factor)*

factor: ('+'|'-'|'~') factor | power

现在我们使用power产品:power: atom trailer* ['**' factor]

atom: ('(' [yield_expr|testlist_comp] ')' |

'[' [testlist_comp] ']' |

'{' [dictorsetmaker] '}' |

NAME | NUMBER | STRING+ | '...' | 'None' | 'True' | 'False')

并移位NUMBER(在我们的输入中是1)和reduce。现在我们回到(1),输入==1,2:进行解析。==匹配comp_op:comp_op: ''|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not'

因此,我们对它进行移位并减少,只剩下输入1,2:(当前解析输出是NUMBER comp_op,我们现在需要匹配expr)。我们在左侧重复这个过程,直接转到atom非终端,并选择NUMBER产品。转移和减少。在

由于,与任何comp_op不匹配,我们减少test非终端并接收{}。我们现在需要匹配else、elif或{},但是我们有{},所以我们用SyntaxError失败。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值