[Python-Dev] bug in grammar

Martin v. Loewis martin@mira.cs.tu-berlin.de
Thu, 18 Jan 2001 13:39:54 +0100


> Should the grammar allow one to construct the example statement
> above?

It should not. Please note that the grammar allows a number of other
things, e.g.

  a+b = c

(pass this to parser.suite to see details)

> If not, I'm not sure how to fix the grammar.

The central problem is that it allows testlist on the LHS of an
augassign or '=', whereas the languages only allows a small subset in
that position. It is not possible to restrict the grammar in itself,
as that will necessarily produce a conflict - you only know that the
'+' was incorrect when you see the '='.

> I suppose the compiler should detect that the list comp is misplaced

I think there should be a well-formedness pass in-between. I.e. after
the AST has been build, a single pass should descend through the tree,
looking for an expr_statement with more than a single testlist. Once
it finds one, it should confirm that this really is a well-formed
lvalue (in C speak). In this case, the test should be that each term
is a an atom without factors.

If the parser itself performs such checks, the compiler could be
simplified in many places, I guess.

Regards,
Martin