[Python-ideas] Allow parentheses to be used with "with" block

MRAB python at mrabarnett.plus.com
Mon Feb 16 11:55:07 CET 2015


On 2015-02-16 01:33, Neil Girdhar wrote:
> Oh, hmm, I thought it would just be converting:
>
> with_stmt: 'with' with_item (',' with_item)*  ':' suite
>
> to
>
> with_stmt: 'with' ('(' with_item (',' with_item)* ')' | with_item (','
> with_item)*)  ':' suite
>
> but I guess that is ambiguous for a LL(1) parser.
>
> Well, maybe it's harder than I thought.  Someone better with grammars
> than me might know a nice way around this.
>
I think it might be possible, but it's not very nice.

Change this:

     expression_list ::=  expression ( "," expression )* [","]

to:

     expression_list ::=  expression ["as" target] ( "," expression 
["as" target] )* [","]

and then add checks to forbid the 'as' form where it's not acceptable.

For example, the 'if' statement is:

if_stmt ::=  "if" expression ":" suite
              ( "elif" expression ":" suite )*
              ["else" ":" suite]

so you'd scan the expression syntax trees and complain if any included
the 'as' form.



More information about the Python-ideas mailing list