On Mon, 03 Jan 2011 16:14:55 +0100, Dmitry Soshnikov <[email protected]> wrote:

Yes, the thing I wanted to underline, that the parser (because of
"ExpressionStatement" cannot begin with a `function` keyword) always treats such production as a `FunctionDeclaration`. And that the `SyntaxError`
relates to two possible cases.

In first case, yes, it's a missing name of the FD. But if we add the name, there are still the syntax error. Thus, notice that the FD itself is parsed
and created normally.

Pedantically you can't really say where/how a string *doesn't* match a BNF, only that it doesn't. You can show a derivation for a matching string, but there is no
concrete "non-matching derivation" when it doesn't.

More pragmatically you can look at a parser that consumes scanned tokens from left to right (e.g., a classical LALR(1) parser). In that case, you can point to the first token that cannot be part of a match (the "minimal non-matching prefix", to coin a term for it). Since the JavaScript grammar is almost suitable for a LALR(1) parser (if you ignore RegExp literals), that's a reasonable thing to do. In that case, the first bad token in the first case is the "(" after "function", which until then could be matched as a FunctionDeclaration, and in the second case, it's the final ")", which could until then be parsed as a FunctionDeclaration followed
by an ExpressionStatment starting with "(".


And this is the second case which I wanted to clarify and correct. The
widespread meaning was that the parentheses at the end are about the _call_.

Indeed. I was wrong here, thinking the first "(" was the bad token, and not
the second.

However, I wanted to predict the questions such as:

Why in the following case there is a SyntaxError (accepting that we have a name of the FD):

// SyntaxError
function foo(x) {
  alert(x);
}();

And if we "pass the argument", then there is no error:

 // SyntaxError
function foo(x) {
  alert(x);
}(1);

Yes, that one is subtle, and well worth pointing out.


...
So, concluding, exactly the fact that this is a _grouping operator_ in this case appears for the parser (and not the call, `Arguments` production), and
exactly it's one of the syntax error reasons.

True. Or more pedantically: It's the beginning of a grouping operator expression,
followed by a ')' that cannot be matched by anything in the grammar.

/L 'I do say "pedantically" a lot.'

--
To view archived discussions from the original JSMentors Mailman list: 
http://www.mail-archive.com/[email protected]/

To search via a non-Google archive, visit here: 
http://www.mail-archive.com/[email protected]/

To unsubscribe from this group, send email to
[email protected]

Reply via email to