diff --git a/Grammar/Grammar b/Grammar/Grammar index 6be7418b5f7..1a98a6aa698 100644 --- a/Grammar/Grammar +++ b/Grammar/Grammar @@ -123,7 +123,7 @@ try_stmt: 'try' ':' suite (except_clause ':' suite)+ | 'try' ':' suite 'finally' except_clause: 'except' [test [',' test]] suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT -test: and_test ('or' and_test)* +test: and_test ('or' and_test)* | lambdef and_test: not_test ('and' not_test)* not_test: 'not' not_test | comparison comparison: expr (comp_op expr)* @@ -135,9 +135,7 @@ shift_expr: arith_expr (('<<'|'>>') arith_expr)* arith_expr: term (('+'|'-') term)* term: factor (('*'|'/'|'%') factor)* factor: ('+'|'-'|'~') factor | atom trailer* -atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | lambdef | NAME | NUMBER | STRING -# Note ambiguity in grammar: "lambda x: x[1]" could mean "(lambda x: x)[1]" -# but the parser is eager so interprets it as "lambda x: (x[1])"... +atom: '(' [testlist] ')' | '[' [testlist] ']' | '{' [dictmaker] '}' | '`' testlist '`' | NAME | NUMBER | STRING lambdef: 'lambda' [varargslist] ':' test trailer: '(' [testlist] ')' | '[' subscript ']' | '.' NAME subscript: test | [test] ':' [test] diff --git a/Python/compile.c b/Python/compile.c index 77752a494b7..aee25850c7c 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -659,18 +659,6 @@ com_atom(c, n) } com_addoparg(c, LOAD_CONST, i); break; - case lambdef: - if ((v = (object *) compile(ch, c->c_filename)) == NULL) { - c->c_errors++; - i = 255; - } - else { - i = com_addconst(c, v); - DECREF(v); - } - com_addoparg(c, LOAD_CONST, i); - com_addbyte(c, BUILD_FUNCTION); - break; case NAME: com_addopname(c, LOAD_NAME, ch); break; @@ -1106,20 +1094,35 @@ com_test(c, n) struct compiling *c; node *n; { - int i; - int anchor; - REQ(n, test); /* and_test ('and' and_test)* */ - anchor = 0; - i = 0; - for (;;) { - com_and_test(c, CHILD(n, i)); - if ((i += 2) >= NCH(n)) - break; - com_addfwref(c, JUMP_IF_TRUE, &anchor); - com_addbyte(c, POP_TOP); + REQ(n, test); /* and_test ('and' and_test)* | lambdef */ + if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { + object *v; + int i; + v = (object *) compile(CHILD(n, 0), c->c_filename); + if (v == NULL) { + c->c_errors++; + i = 255; + } + else { + i = com_addconst(c, v); + DECREF(v); + } + com_addoparg(c, LOAD_CONST, i); + com_addbyte(c, BUILD_FUNCTION); + } + else { + int anchor = 0; + int i = 0; + for (;;) { + com_and_test(c, CHILD(n, i)); + if ((i += 2) >= NCH(n)) + break; + com_addfwref(c, JUMP_IF_TRUE, &anchor); + com_addbyte(c, POP_TOP); + } + if (anchor) + com_backpatch(c, anchor); } - if (anchor) - com_backpatch(c, anchor); } static void diff --git a/Python/graminit.c b/Python/graminit.c index 1126b9c1d80..f923059a873 100644 --- a/Python/graminit.c +++ b/Python/graminit.c @@ -674,22 +674,31 @@ static state states_31[5] = { {1, arcs_31_3}, {2, arcs_31_4}, }; -static arc arcs_32_0[1] = { +static arc arcs_32_0[2] = { {72, 1}, + {74, 2}, }; static arc arcs_32_1[2] = { - {73, 0}, + {73, 3}, {0, 1}, }; -static state states_32[2] = { - {1, arcs_32_0}, +static arc arcs_32_2[1] = { + {0, 2}, +}; +static arc arcs_32_3[1] = { + {72, 1}, +}; +static state states_32[4] = { + {2, arcs_32_0}, {2, arcs_32_1}, + {1, arcs_32_2}, + {1, arcs_32_3}, }; static arc arcs_33_0[1] = { - {74, 1}, + {75, 1}, }; static arc arcs_33_1[2] = { - {75, 0}, + {76, 0}, {0, 1}, }; static state states_33[2] = { @@ -697,11 +706,11 @@ static state states_33[2] = { {2, arcs_33_1}, }; static arc arcs_34_0[2] = { - {76, 1}, - {77, 2}, + {77, 1}, + {78, 2}, }; static arc arcs_34_1[1] = { - {74, 2}, + {75, 2}, }; static arc arcs_34_2[1] = { {0, 2}, @@ -715,7 +724,7 @@ static arc arcs_35_0[1] = { {54, 1}, }; static arc arcs_35_1[2] = { - {78, 0}, + {79, 0}, {0, 1}, }; static state states_35[2] = { @@ -723,16 +732,16 @@ static state states_35[2] = { {2, arcs_35_1}, }; static arc arcs_36_0[10] = { - {79, 1}, {80, 1}, {81, 1}, {82, 1}, {83, 1}, {84, 1}, {85, 1}, + {86, 1}, {55, 1}, - {76, 2}, - {86, 3}, + {77, 2}, + {87, 3}, }; static arc arcs_36_1[1] = { {0, 1}, @@ -741,7 +750,7 @@ static arc arcs_36_2[1] = { {55, 1}, }; static arc arcs_36_3[2] = { - {76, 1}, + {77, 1}, {0, 3}, }; static state states_36[4] = { @@ -751,10 +760,10 @@ static state states_36[4] = { {2, arcs_36_3}, }; static arc arcs_37_0[1] = { - {87, 1}, + {88, 1}, }; static arc arcs_37_1[2] = { - {88, 0}, + {89, 0}, {0, 1}, }; static state states_37[2] = { @@ -762,10 +771,10 @@ static state states_37[2] = { {2, arcs_37_1}, }; static arc arcs_38_0[1] = { - {89, 1}, + {90, 1}, }; static arc arcs_38_1[2] = { - {90, 0}, + {91, 0}, {0, 1}, }; static state states_38[2] = { @@ -773,10 +782,10 @@ static state states_38[2] = { {2, arcs_38_1}, }; static arc arcs_39_0[1] = { - {91, 1}, + {92, 1}, }; static arc arcs_39_1[2] = { - {92, 0}, + {93, 0}, {0, 1}, }; static state states_39[2] = { @@ -784,11 +793,11 @@ static state states_39[2] = { {2, arcs_39_1}, }; static arc arcs_40_0[1] = { - {93, 1}, + {94, 1}, }; static arc arcs_40_1[3] = { - {94, 0}, {95, 0}, + {96, 0}, {0, 1}, }; static state states_40[2] = { @@ -796,11 +805,11 @@ static state states_40[2] = { {3, arcs_40_1}, }; static arc arcs_41_0[1] = { - {96, 1}, + {97, 1}, }; static arc arcs_41_1[3] = { - {97, 0}, {98, 0}, + {99, 0}, {0, 1}, }; static state states_41[2] = { @@ -808,12 +817,12 @@ static state states_41[2] = { {3, arcs_41_1}, }; static arc arcs_42_0[1] = { - {99, 1}, + {100, 1}, }; static arc arcs_42_1[4] = { {21, 0}, - {100, 0}, {101, 0}, + {102, 0}, {0, 1}, }; static state states_42[2] = { @@ -821,16 +830,16 @@ static state states_42[2] = { {4, arcs_42_1}, }; static arc arcs_43_0[4] = { - {97, 1}, {98, 1}, - {102, 1}, - {103, 2}, + {99, 1}, + {103, 1}, + {104, 2}, }; static arc arcs_43_1[1] = { - {99, 3}, + {100, 3}, }; static arc arcs_43_2[2] = { - {104, 2}, + {105, 2}, {0, 2}, }; static arc arcs_43_3[1] = { @@ -842,12 +851,11 @@ static state states_43[4] = { {2, arcs_43_2}, {1, arcs_43_3}, }; -static arc arcs_44_0[8] = { +static arc arcs_44_0[7] = { {16, 1}, - {105, 2}, - {107, 3}, - {110, 4}, - {111, 5}, + {106, 2}, + {108, 3}, + {111, 4}, {12, 5}, {112, 5}, {113, 5}, @@ -858,11 +866,11 @@ static arc arcs_44_1[2] = { }; static arc arcs_44_2[2] = { {9, 7}, - {106, 5}, + {107, 5}, }; static arc arcs_44_3[2] = { - {108, 8}, - {109, 5}, + {109, 8}, + {110, 5}, }; static arc arcs_44_4[1] = { {9, 9}, @@ -874,16 +882,16 @@ static arc arcs_44_6[1] = { {18, 5}, }; static arc arcs_44_7[1] = { - {106, 5}, + {107, 5}, }; static arc arcs_44_8[1] = { - {109, 5}, -}; -static arc arcs_44_9[1] = { {110, 5}, }; +static arc arcs_44_9[1] = { + {111, 5}, +}; static state states_44[10] = { - {8, arcs_44_0}, + {7, arcs_44_0}, {2, arcs_44_1}, {2, arcs_44_2}, {2, arcs_44_3}, @@ -919,7 +927,7 @@ static state states_45[5] = { }; static arc arcs_46_0[3] = { {16, 1}, - {105, 2}, + {106, 2}, {116, 3}, }; static arc arcs_46_1[2] = { @@ -939,7 +947,7 @@ static arc arcs_46_5[1] = { {0, 5}, }; static arc arcs_46_6[1] = { - {106, 5}, + {107, 5}, }; static state states_46[7] = { {3, arcs_46_0}, @@ -1064,11 +1072,11 @@ static state states_51[8] = { }; static dfa dfas[52] = { {256, "single_input", 0, 3, states_0, - "\004\030\001\000\250\360\057\040\007\020\000\000\106\112\047"}, + "\004\030\001\000\250\360\057\040\007\040\000\000\214\224\047"}, {257, "file_input", 0, 2, states_1, - "\204\030\001\000\250\360\057\040\007\020\000\000\106\112\047"}, + "\204\030\001\000\250\360\057\040\007\040\000\000\214\224\047"}, {258, "eval_input", 0, 3, states_2, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\007"}, {259, "funcdef", 0, 6, states_3, "\000\010\000\000\000\000\000\000\000\000\000\000\000\000\000"}, {260, "parameters", 0, 4, states_4, @@ -1080,13 +1088,13 @@ static dfa dfas[52] = { {263, "fplist", 0, 3, states_7, "\000\020\001\000\000\000\000\000\000\000\000\000\000\000\000"}, {264, "stmt", 0, 2, states_8, - "\000\030\001\000\250\360\057\040\007\020\000\000\106\112\047"}, + "\000\030\001\000\250\360\057\040\007\040\000\000\214\224\047"}, {265, "simple_stmt", 0, 4, states_9, - "\000\020\001\000\250\360\057\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\250\360\057\000\000\040\000\000\214\224\007"}, {266, "small_stmt", 0, 2, states_10, - "\000\020\001\000\250\360\057\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\250\360\057\000\000\040\000\000\214\224\007"}, {267, "expr_stmt", 0, 2, states_11, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\007"}, {268, "print_stmt", 0, 3, states_12, "\000\000\000\000\010\000\000\000\000\000\000\000\000\000\000"}, {269, "del_stmt", 0, 3, states_13, @@ -1126,45 +1134,45 @@ static dfa dfas[52] = { {286, "except_clause", 0, 5, states_30, "\000\000\000\000\000\000\000\000\040\000\000\000\000\000\000"}, {287, "suite", 0, 5, states_31, - "\004\020\001\000\250\360\057\000\000\020\000\000\106\112\007"}, - {288, "test", 0, 2, states_32, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\004\020\001\000\250\360\057\000\000\040\000\000\214\224\007"}, + {288, "test", 0, 4, states_32, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\007"}, {289, "and_test", 0, 2, states_33, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\003"}, {290, "not_test", 0, 3, states_34, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\003"}, {291, "comparison", 0, 2, states_35, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {292, "comp_op", 0, 4, states_36, - "\000\000\000\000\000\000\200\000\000\220\177\000\000\000\000"}, + "\000\000\000\000\000\000\200\000\000\040\377\000\000\000\000"}, {293, "expr", 0, 2, states_37, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {294, "xor_expr", 0, 2, states_38, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {295, "and_expr", 0, 2, states_39, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {296, "shift_expr", 0, 2, states_40, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {297, "arith_expr", 0, 2, states_41, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {298, "term", 0, 2, states_42, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {299, "factor", 0, 4, states_43, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {300, "atom", 0, 10, states_44, - "\000\020\001\000\000\000\000\000\000\000\000\000\000\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\000\224\003"}, {301, "lambdef", 0, 5, states_45, "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\004"}, {302, "trailer", 0, 7, states_46, - "\000\000\001\000\000\000\000\000\000\000\000\000\000\002\020"}, + "\000\000\001\000\000\000\000\000\000\000\000\000\000\004\020"}, {303, "subscript", 0, 4, states_47, - "\000\120\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\120\001\000\000\000\000\000\000\040\000\000\214\224\007"}, {304, "exprlist", 0, 3, states_48, - "\000\020\001\000\000\000\000\000\000\000\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\000\000\000\214\224\003"}, {305, "testlist", 0, 3, states_49, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\007"}, {306, "dictmaker", 0, 5, states_50, - "\000\020\001\000\000\000\000\000\000\020\000\000\106\112\007"}, + "\000\020\001\000\000\000\000\000\000\040\000\000\214\224\007"}, {307, "classdef", 0, 8, states_51, "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\040"}, }; @@ -1243,6 +1251,7 @@ static label labels[118] = { {6, 0}, {289, 0}, {1, "or"}, + {301, 0}, {290, 0}, {1, "and"}, {1, "not"}, @@ -1280,7 +1289,6 @@ static label labels[118] = { {306, 0}, {27, 0}, {25, 0}, - {301, 0}, {2, 0}, {3, 0}, {1, "lambda"},