For review: PEP 308 - If-then-else expression

holger krekel pyth at devel.trillke.net
Mon Feb 10 12:02:49 EST 2003


Andrew Koenig wrote:
> holger> You are not targeting the real problem, i think. Quoting from 
> holger> Python-2.2.2/Grammar/Grammar
> 
> holger>     stmt: simple_stmt | compound_stmt    
> 
> holger>     compound_stmt: if_stmt | while_stmt | for_stmt | ...
> 
> holger>     simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE
> 
> holger>     small_stmt: expr_stmt | print_stmt  | del_stmt | ...
> 
> I propose to change the grammar as follows:
> 
> 
> Original:
> 
> expr_stmt: testlist (augassign testlist | ('=' testlist)*)
> 
> test: and_test ('or' and_test)* | lambdef
> 
> 
> Proposed:
> 
> expr_stmt: utestlist (augassign testlist | ('=' testlist)*)
> 
> utestlist: uncond_test (',' test)* [',']
> 
> test: uncond_test | 'if' test ':' test ('elif' test)* 'else' ':' test
> 
> uncond_test: and_test ('or' and_test)* | lambdef
> 
> 
> 
> In other words, for the various possibilities for `test', which is
> currently the most general kind of expression, I'm want to add an
> if-else expression.  The former "test" becomes "uncond_test".
> 
> A new grammar production, "utestlist", represents a (nonempty) list of
> tests in which the *first* test is not an if-then-else expression.

> 
> An expr_statement is then a utestlist rather than a testlist.
> 
> 
> I don't know for sure that this grammar is correct, but I do
> know that pgen accepted it without complaint.

should work, although i am not sure that pgen itself checks 
for amiguities.  Of course, you avoid the problem of 
if-cond-expr-else-expr as a statement by disallowing it.  
Possible but not very uniform.  

If you really want to integrate this with the PEP [1] then you 
might want to bring it to the point that you can completly compile 
python.  At least i would do it if i were in favour of more powerful
expressions <wink>.  You don't need to run it (although empty 
code generation in compile.c would easily allow that, i guess). 

take care, 

    holger


[1] i remain unconvinced that we need more powerful expressions
    in python, though.  Let's just add really useful statements. 





More information about the Python-list mailing list