Search code examples
javascriptecmascript-6context-free-grammarcompiler-construction

Why specify a separate production for assignment operator with `=` literal instead of part `AssignmentOperator` symbol


The EcmaScript grammar for AssignmentExpression specifies the following:

AssignmentExpression :
    ConditionalExpression
    YieldExpression
    ArrowFunction
    AsyncArrowFunction
    LeftHandSideExpression = AssignmentExpression                     <-------------- here
    LeftHandSideExpression AssignmentOperator AssignmentExpression

AssignmentOperator : one of
    *= /= %= += -= <<= >>= >>>= &= ^= |= **=

I'm wondering what is the rationale behind specifying LeftHandSideExpression = AssignmentExpression as a separate production instead of simply including = into AssignmentOperator?


Solution

  • The Grammar would be completely equivalent if the two productions were merged.

    However, there are several places later in the spec where the two productions are treated differently. For example in the Static Semantics section right after the grammar and then later in the Destructuring Assignment section, which only applies to the = production.

    If the two productions were one, the phrases referring to them would have to be more awkward such as "LeftHandSideExpression AssignmentOperator AssignmentExpression when the AssignmentOperator is =".

    So presumably the two productions were written down separately, so it's then easier and less confusing to also talk about them separately in the text.