Grammar for plus and minus unary ops
I was recently reviewing some Python code for a friend who is a C++ programmer, and he had code something like this: def foo(): try = 0 while try<MAX: ret = bar() if ret: break ++try 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. It appears that the grammar treats the above example as the unary + op applied twice: u_expr ::= power | "-" u_expr | "+" u_expr | "\~" u_expr Playing in the interpreter, expressions like "1+++++++++5" and "1+-+- +-+-+-+-5" evaluate to 6. 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...) Jared
Jared Grubb wrote:
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...)
So you want something like u_expr :: = power | "-" xyzzy_expr | "+" xyzzy_expr | "\~" u_expr xyzzy_expr :: = power | "\~" u_expr Such that: 5 # valid u_expr +5 # valid u_expr -5 # valid u_expr ~5 # valid u_expr ~~5 # valid u_expr ~+5 # valid u_expr +~5 # valid u_expr ~-5 # valid u_expr -~5 # valid u_expr +~-5# valid u_expr ++5 # not valid u_expr +-5 # not valid u_expr -+5 # not valid u_expr --5 # not valid u_expr While, I'm not a python developer, (just a python user) that sounds reasonable to me, as long as this does not silently change the meaning of any expression, but only noisily breaks programs, and that the broken constructs are not used frequently. Can anybody come up with any expressions that would silently change in meaning if the above were applied? Obviously a sane name would need to be chosen to replace xyzzy_expr.
Please take this to python-ideas. On Fri, Mar 27, 2009 at 2:15 PM, Joe Smith <unknown_kev_cat@hotmail.com> wrote:
Jared Grubb wrote:
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...)
So you want something like u_expr :: = power | "-" xyzzy_expr | "+" xyzzy_expr | "\~" u_expr xyzzy_expr :: = power | "\~" u_expr
Such that: 5 # valid u_expr +5 # valid u_expr -5 # valid u_expr ~5 # valid u_expr ~~5 # valid u_expr ~+5 # valid u_expr +~5 # valid u_expr ~-5 # valid u_expr -~5 # valid u_expr +~-5# valid u_expr
++5 # not valid u_expr +-5 # not valid u_expr -+5 # not valid u_expr --5 # not valid u_expr
While, I'm not a python developer, (just a python user) that sounds reasonable to me, as long as this does not silently change the meaning of any expression, but only noisily breaks programs, and that the broken constructs are not used frequently.
Can anybody come up with any expressions that would silently change in meaning if the above were applied?
Obviously a sane name would need to be chosen to replace xyzzy_expr.
_______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/guido%40python.org
-- --Guido van Rossum (home page: http://www.python.org/~guido/)
participants (3)
-
Guido van Rossum -
Jared Grubb -
Joe Smith