[Python-Dev] funcdef grammar production (original) (raw)

Jewett, Jim J jim.jewett at eds.com
Thu Mar 18 10:53:07 EST 2004


Greg Ewing:

Jewett, Jim J:

one possible path is

(defparameter ",")* (defparameter [","])

Even that seems unnecessarily obtuse.

I agree, but that is the current definition. (Except that I explicitly chose the last of several alternatives, instead of leaving in the "choice".)

I would write it as

defparameter ("," defparameter)* [","]

That would be incorrect.

Even the first defparameter is optional if there is an *args or a **kwargs.

There does need to be something in the list. (An empty parameter list is handled by making it an optional part of funcdef.) I assume this restriction is also due to the unambiguous-at-first-token rule.

That optional "," is required if you have both a defparameter and an *args or **kwargs. It is forbidden if there is no defparameter.

Under my formulation, it would be ambiguous whether "a" was a defparameter or a (regular) parameter, which are different productions. Looking ahead for the "=" violates a no-lookahead rule.

If the grammar is only for documentation, none of that matters

There is value in keeping the grammar documentation as close as possible to the actual coded grammar. If a rule expressible in the grammar is actually implemented outside it, then either the documented grammar and the real grammar diverge, or some rules must be listed only in the text. Pick your poison.

By the way, it occurs to me that BNF on its own doesn't seem to be up to the task of specifying this sort of thing clearly to a human reader.

Agreed, but text often fails. The advantage of BNF is that it can be checked to ensure that the writer and the reader agree.

If it weren't for the commas,

Yes. Particularly evil is the rule that allows a trailing comma sometimes but not always. That said, I would rather have a confusing grammar than have to add trailing commas for standardization.

The sublist possibilities don't make things any easier, particularly since they aren't full parameter lists.
(You can't use defaults, *, or ** within a sublist, though you can use a default for the entire list.) I can only assume that this was an attempt to meet lispers halfway; there are people who consider destructuring-bind to be a valuable feature.

-jJ



More information about the Python-Dev mailing list