[New-bugs-announce] [issue5460] Python 3.0 grammar is ambiguous with the addition of star_expr

Fabio Zadrozny report at bugs.python.org
Mon Mar 9 20:00:14 CET 2009

New submission from Fabio Zadrozny <fabioz at users.sourceforge.net>:

Note: A discussion related to this bug was raised on:

The following constructs are ambiguous in the Python 3.0 grammar:

arglist: (argument ',')*
                        (argument [',']
                         |'*' test (',' argument)* [',' '**' test]
                         |'**' test

argument: test [comp_for]
test: or_test
or_test: and_test
and_test: not_test
not_test: 'not' not_test | comparison
comparison: star_expr
star_expr: ['*'] expr

So, with that construct, having call(arglist) in a format:

call(*test), the grammar would find it to be consumed in the argument
construction (because of the star_expr) and not in the arglist in the
'*' test.

Python seems to be lucky in this because it seems to be getting in the
correct choice, when that's not really possible from the grammar --
maybe it tries the 2nd construct before the 1st and succeeds because of
that? It seems to me that this could actually be a bug in the Python
grammar generator. 

It doesn't seem possible to disambiguate that without semantic actions
later on, but the grammar could be changed to disambiguate that.

I've used the constructs below in a JavaCC grammar successfully (and I
think Python could use the same constructs):

expr_stmt: testlist_star_expr (augassign (yield_expr|testlist) |
                     ('=' (yield_expr|testlist_star_expr))*)
testlist_star_expr: (test|star_expr) (',' test|star_expr)* [',']

star_expr: '*' expr

components: Interpreter Core
message_count: 1.0
messages: 83395
nosy: fabioz
nosy_count: 1.0
severity: normal
status: open
title: Python 3.0 grammar is ambiguous with the addition of star_expr
type: feature request
versions: Python 3.0, Python 3.1

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list