Search code examples
javascriptecmascript-6compiler-constructioncontext-free-grammar

Does this grammar define right-associativity by right recursion


I'm reading this article about adding the ** operator to the EcmaScript spec where the author states the following:

Exponentiation must be evaluated before multiplication and more importantly, the BNF grammar must be written such that the operator’s right-associativity is clearly defined (unlike MultiplicativeExpression, which is left-associative).

And he defines the new non-terminal ExponentiationExpression symbol in the grammar as:

ExponentiationExpression : 
  UnaryExpression[?Yield]
  UnaryExpression[?Yield] ** ExponentiationExpression[?Yield]

MultiplicativeExpression[Yield] :
  ExponentiationExpression[?Yield]
  MultiplicativeExpression[?Yield] MultiplicativeOperator ExponentiationExpression[?Yield]

This article states that:

To write a grammar that correctly expresses operator associativity:

  • For left associativity, use left recursion.
  • For right associativity, use right recursion.

It seems that he follows that rule and defines the associativity by using right recursion for the ExponentiationExpression here:

ExponentiationExpression -> UnaryExpression[?Yield] ** ExponentiationExpression[?Yield]

Am I right?


Solution

  • Am I right?

    Yes.