[Python-Dev] small Grammar questions

Andrew Dalke dalke at dalkescientific.com
Wed Feb 20 00:58:26 CET 2008


On Feb 19, 2008 1:38 PM, Andrew Dalke <dalke at dalkescientific.com> wrote:
> def spam((a) = c):
>   print a

On Feb 20, 2008 12:29 AM, Brett Cannon <brett at python.org> wrote:
> The error might be odd, but I don't see why that should be allowed
> syntax. Having a parameter surrounded by a parentheses like that makes
> no sense in a context of a place where arbitrary expressions are not
> allowed.

I'm fine with that.  This is a corner that no one but language lawyers
will care about.  The thing is, the online documentation and the
Grammar file allow it, as did Python 2.5.

In any case, the error message is not helpful.

> From what I can tell the grammar does not prevent it. But it is
> possible that during AST creation or bytecode compilation a specific
> check is made that is throwing the exception...

In my code it's during AST generation...  Your pointer to ast.c:672
was helpful.  It's stuff  jeremy.hylton did in r54415.  Now I have to
figure out how Python's internal ASTs work..  Which might take a
while.

> Are you asking why the decision was made to make the expression
> illegal, or why the grammar is flagging it is wrong?

Why it's illegal.  Supporting a comma doesn't seem to make anything
ambiguous, but PLY's LALR(1) might handle things that Python itself
doesn't and I don't have the experience to tell.

There are other places where the grammar definition allows syntax that
is rejected later on.  Those I can justify by saying it's easier to
define the grammar that way (hence f(1+1=2) is legal grammar but
illegal Python), or to produce better error messages (like "from .a
import *").

But this one prohibiting [x for x in 1,] I can't figure out.


       Andrew
       dalke at dalkescientific.com


More information about the Python-Dev mailing list