[Python-ideas] if-syntax for regular for-loops

dbpokorny at gmail.com dbpokorny at gmail.com
Mon Oct 13 00:32:03 CEST 2008

On Oct 12, 12:21 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> On 12 Oct 2008, at 19:38, dbpoko... at gmail.com wrote:
> > On Oct 10, 12:51 pm, Arnaud Delobelle <arno... at googlemail.com> wrote:
> >> There is also a problem with parsing as:
> >> * the following is correct:
> >>      for i in L1 if cond else L2:
> >>         do_something()
> >> * Python's grammar is LL(1)
> > Not really.
> What do you mean? Python's grammar is not LL(1)? Or Python + for-in-if  
> statements is still LL(1)?

Oops! Here is what I should have said: if you replace

for_stmt: 'for' exprlist 'in' testlist ':' suite ['else' ':' suite]

in Grammar/Grammar with the following line

for_stmt: 'for' exprlist 'in' testlist_safe ['if' old_test] ':' suite
['else' ':' suite]

Then the grammar is still LL(1) since it resembles the LC syntax. I
neglected to turn the testlist into a testlist_safe. Now in theory
this could break code...it would break anything like

for x in my_list if some_condition else []:

Now this wouldn't even be a potential problem if Python converted to a
GLR parser, but that's another discussion.

> > for_stmt: 'for' exprlist 'in' testlist ['if' or_test] ':' suite
> > ['else' ':' suite]
> What does that prove?
> If I show you:
>     for i in L if
> How do you know, without looking at further tokens,  whether the 'if'  
> is part of an if-expression or part of a for-in-if statement?

You can say the same thing about an LC. The answer in that situation
is the last token ('if') maps to the first element of the right-hand
side of the list_if production.


More information about the Python-ideas mailing list