A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://mail.python.org/pipermail/python-dev/2009-March/087817.html below:

[Python-Dev] Grammar for plus and minus unary ops

[Python-Dev] Grammar for plus and minus unary opsJared Grubb jared.grubb at gmail.com
Fri Mar 27 18:19:46 CET 2009
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
More information about the Python-Dev mailing list

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4