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

Andrew Koenig ark at research.att.com
Mon Feb 10 10:54:41 EST 2003


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.





More information about the Python-list mailing list