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

Jared Grubb jared.grubb at gmail.com
Fri Mar 27 21:46:18 CET 2009


(This is a reply to Joe's post on python-dev)

That looks like a good solution.

The downside I see with your rules is that combinations like "~+~-~ 
+~-" would still be valid, but if people want to write obfuscated  
code, there are always ways to do it. Forbidding the examples that you  
gave (and the ones I gave) is still a positive move, in my opinion.

Jared

On 27 Mar 2009, at 12:15, Joe Smith 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.
>



More information about the Python-ideas mailing list