考虑到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失败。在