About the grammar
Martin v. Loewis
martin at v.loewis.de
Mon Apr 19 08:58:11 CEST 2010
> Does any one knows why the grammar is so coded? Any intuition?
The 2.7 Grammar clarifies that:
# The reason that keywords are test nodes instead of NAME is that using
# NAME results in an ambiguity. ast.c makes sure it's a NAME.
argument: test [comp_for] | test '=' test
The ambiguity is this: if I have
foo(a, b = 2)
and have parsed it up to
then, if I get "b", should I enter the "test" clause (for the left
alternative) or the right clause? With the grammar being as stated,
it can be parsed as
argument: test ([comp_for] | '=' test)
so that parsing always starts with a test. Afterwards, I either get a
'=', indicating a keyword argument, or not: the '=' is not in the FIRST
set of comp_for, so there is no ambiguity here.
More information about the Python-list