The trouble with ambiguous grammars

Waldemar Horwat waldemar at google.com
Fri Oct 17 12:25:25 PDT 2008


Here's a clearer case that Firefox gets wrong (or your grammar gets wrong, depending on your point of view):

function f() {return "f"}
var x = 3;
let (a = 1) a ? f : x++();

The grammar states that the last statement must evaluate to "f".  Firefox gives a syntax error.  This is incorrect because the let expression up to the ++ is a PrimaryExpression so can be used as the left operand of a function call.

This is why ambiguous grammars are bad and unsuitable for our spec.  In an unambiguous grammar, if you find a series of production expansions that matches a source program, then you know that those are the expansions that will be taken.  In an ambiguous grammar, you also need to prove the negative:  no *other* expansion can match that source program and shadow your expansions.  Proving the negative causes trouble because a language extension could turn the mismatch into a match and because sometimes, as in the above case, you expected some other part of the grammar to shadow your expansions but it didn't.

    Waldemar


More information about the Es-discuss mailing list