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

Andrew Koenig ark at research.att.com
Mon Feb 10 09:08:43 EST 2003


Roman> In Python, expression is a standalone statement. So in case of 

Roman> if expr1: expr2 else: expr3

Roman> the parser will notice the difference only at "else".

Here is how I am proposing to change the grammar to avoid that problem.
Note that I have not run this grammar through a parser generator;
nevertheless, I think the idea behind it is pretty clear.



Original:

expression ::= 
             or_test | lambda_form

expression_stmt ::= 
             expression_list

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




Proposed:

uncond_expression ::= 
             or_test | lambda_form

expression ::=
	     uncond_expression | if_form

if_form ::=
	     "if" expression ":" expression
	     ( "elif" expression ":" expression)*
	     "else" ":" expression

expression_stmt ::=
	     uncond_expression ( "," expression )* [","]


-- 
Andrew Koenig, ark at research.att.com, http://www.research.att.com/info/ark




More information about the Python-list mailing list