A counter-proposal to __future__ in PEP 236

Martin von Loewis loewis at informatik.hu-berlin.de
Fri Mar 2 10:42:09 EST 2001


Joshua Marshall <jmarshal at mathworks.com> writes:

> So anybody care to post an example of how this can be ambiguous (in
> context) so I don't need to go study Python's parser...?

>From the language level, it is not ambiguous. On the parser level, you have

stmt: simple_stmt | compound_stmt

and then

simple_stmt: small_stmt (';' small_stmt)* [';'] NEWLINE

small_stmt: expr_stmt | print_stmt | del_stmt | pass_stmt | flow_stmt
            | import_stmt | global_stmt | exec_stmt | assert_stmt

expr_stmt: testlist (augassign testlist | ('=' testlist)*)

A few productions later, you find that testlist can be 

atom: NAME | <more alternatives>

So when you write 'a = b', then a and b are NAME tokens, which reduce
to testlists, which then reduces to an expr_stmt, and a stmt. Your
proposal is

small_stmt: <all alternatives so far> | directive_stmt
directive_stmt: NAME NAME atom [';']

Now, the Python parser is recursive-descent, meaning that it needs to
look at the first token to descend into an alternative. If you look at
all stmt's, you find that you can pick an alternative just by looking
at the first token. E.g.

for, while, do, if, try, class, def -> compound_stmt
NAME, print, del, pass, break, continue, return, raise, import,
      global, exec, assert -> simple_stmt

Inside simple_stmt, you alway can descend to small_stmt, where you can
pick an alternative based on the first token. If you add the
directive_stmt, both expr_stmt and directive_stmt would start with a
NAME token, and you'd have to look at the next token to find out which
case it is. Since the parser is LL(1), it cannot do that.

Regards,
Martin



More information about the Python-list mailing list