Identifiers in python are described as:
identifier ::= (letter|"_") (letter | digit | "_")*
Hence, 2if
can't be an identifier hence if must be 2
,if
. Similar logic applies to the rest of the expression.
Basically interpreting 2if-1e1else 1
would go something like this (the full parsing would be quite complicated):
2if
not valid identifier, 2
matches digit digit ::= "0"..."9"
,if
matches keyword.
-1e1else
, -1
is the unary negation (u_expr ::= power | "-" u_expr | "+" u_expr | "~" u_expr
) of : ( 1
which matches intpart
in exponentfloat ::= (intpart | pointfloat) | exponent
, e1
is exponent exponent ::= ("e" | "E") ["+" | "-"] digit+
.) You can see expressions of the form Ne+|-x
yields a float this from:
>>> type(2e3)
<type 'float'>
then the else
is seen as the keyword, and -1
etc..
You can peruse the gammar to read more about it.