[Python-Dev] Python Grammar Ambiguity
Michael Foord
fuzzyman at voidspace.org.uk
Mon Apr 24 19:23:50 CEST 2006
skip at pobox.com wrote:
> Michael> I've hit on what looks like a fundamental ambiguity in the
> Michael> Python grammar which is difficult to get round with PLY; and
> Michael> I'm wondering *why* the grammar is defined in this way.
>
> Michael,
>
> You refer to the ref manual documentation:
>
> Michael> List displays (list comprehensions) are defined as (from
> Michael> http://docs.python.org/ref/lists.html )
>
> Note that the BNF there is mostly designed for human consumption. Have you
> verified that the ambiguity is also present in the Grammar file?
>
From :
http://svn.python.org/view/python/tags/r243/Grammar/Grammar?rev=43414&view=auto
list_for: 'for' exprlist 'in' testlist_safe [list_iter]
So in the Python grammar list_for *is* defined as an expression list.
That follows, because using the parser module I can create an ast for a
list comprehension like the following :
import parser
expr = '[1 for 1 in n]\n'
ast = parser.expr(expr)
print parser.compileast(ast)
Traceback (most recent call last):
File "ast_example.py", line 6, in ?
print parser.compileast(ast)
SyntaxError: can't assign to literal
The syntax error is thrown at the compile stage, not the parse stage.
Having list_for being defined in terms of something like varlist makes
more sense, but isn't how the grammar is done currently.
Michael Foord
> Skip
>
>
More information about the Python-Dev
mailing list