<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">(Originally posted to python-dev, discussion moved here per request by GvR; also fixed pseudo-code to not use a keyword as local var)<br><div><br><div>Begin forwarded message:</div><blockquote type="cite"><div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><font class="Apple-style-span" color="#000000"><b><br></b></font></div></div><div>I was recently reviewing some Python code for a friend who is a C++ programmer, and he had code something like this:<br><br>def foo():<br> attempt = 0<br> while attempt<MAX:<br>    ret = bar()<br>    if ret: break<br>    ++attempt<br><br>I was a bit surprised that this was syntactically valid, and because the timeout condition only occurred in exceptional cases, the error has not yet caused any problems.<br><br>It appears that the grammar treats the above example as the unary + op applied twice:<br><br>u_expr ::=<br>            power | "-" u_expr<br>             | "+" u_expr | "\~" u_expr<br><br>Playing in the interpreter, expressions like "1+++++++++5" and  "1+-+-+-+-+-+-5" evaluate to 6.<br><br>I'm not a EBNF expert, but it seems that we could modify the grammar to be more restrictive so the above code would not be silently valid. E.g., "++5" and "1+++5" and "1+-+5" are syntax errors, but still keep "1++5", "1+-5", "1-+5" as valid. (Although, '~' throws in a kink... should '~-5' be legal? Seems so...)<br><br>Jared<br></div><div><br></div></blockquote></div></div></body></html>