In ES6, what is the meaning of the parameter [Yield], [In] in a grammar production

# Coolwust (10 years ago)

I know a production may be parameterized by suffixing the nonterminal symbol, my question is what is the meaning of the parameter [Yield], [In]? For example, in section 12.1, identifier expression has the syntax below:

IdentifierReference [Yield] :
Identifier
[~Yield] yield

Thanks.

# Michael Dyck (10 years ago)

On 15-03-09 04:10 AM, Coolwust wrote:

I know a production may be parameterized by suffixing the nonterminal symbol, my question is what is the meaning of the parameter [Yield], [In]?

To figure out the 'meaning' of a particular parameter, look at all the places where that parameter is used as a prefix/guard for a whole RHS.

E.g., consider Yield:

12.14 says:

 AssignmentExpression[In,Yield] :
   ...
   [+Yield] YieldExpresion[?In]

So the presence of the Yield parameter means that a YieldExpression is allowed (i.e., 'yield' is treated as a keyword).

Whereas 12.1 says:

 IdentifierReference[Yield] :
   Identifier
   [~Yield] yield

(And similarly for BindingIdentifier and LabelIdentifier.) So the absence of the Yield parameter means that 'yield' is allowed as an IdentifierReference or BindingIdentifier or LabelIdentifier. (That is, it's treated the same as an Identifier.)

So, in general, the Yield parameter controls whether 'yield' is treated as a ReservedWord or not.

# Coolwust (10 years ago)

My bad, it is a keyword, I found. Thanks again.

# Coolwust (10 years ago)

I have one more question, why yield and in keywords are so special? When can I treat yield and in as the non-reserved-word?

# Brendan Eich (10 years ago)

Coolwust wrote:

I have one more question, why yield and in keywords are so special? When can I treat yield and in as the non-reserved-word?

You can't treat in as an unreserved identifier, in any event. It has been reserved since JS1 in 1995, de-jure in ECMA-262 Edition 1 (ES1).

These are different, their histories differ. ES6 (after ES4 but without opt-in versioning) supports yield in generator functions but not elsewhere, because extant code on the Web over the last almost-20-years uses yield as an identifier in plain functions and/or global code.

in needs special treatment due to the ES3 grammatical ambiguity that would otherwise allow two ways of parsing the left sentential form for (var x = y in z ... (where the ... is meta). ECMA-262 aspires to specify an LR(1) grammar with lookahead restrictions and error correction procedures such as ASI. Allowing in expressions in variable intiialisers at the front of for loops would make the grammar not LR(k) for any k. (The potential ambiguity arose first in ES3 because that was when the in operator was added.)

# Coolwust (10 years ago)

Thanks Brendan, very informative reply!