Search code examples
ecmascript-6ecmascript-next

Meaning of ECMAScript notation syntax?


I am confused with how to properly read this syntax line.

  • Why IdentifierName is defined with an IdentifierName?
  • Why do you have a new line in the middle of the definition?

How to read this notation?

Syntax

IdentifierName ::
       IdentifierStart
       IdentifierName IdentifierPart

Solution

  • It's a recursive definition, which is not uncommon in grammar notations.

    IdentifierName is composed of IdentifierStart or IdentifierName IdentifierPart. If we expand IdentifierName in the second alternative again we get

    IdentifierStart IdentifierPart
    IdentifierName IdentifierPart IdentifierPart
    

    and so on.

    To express this in an informal way: IdentifierName is compose of IdentifierStart followed by an arbitrary number of IdentifierParts.


    From the specifiction itself:

    As another example, the syntactic definition:

    ArgumentList:
      AssignmentExpression
      ArgumentList,AssignmentExpression
    

    states that an ArgumentList may represent either a single AssignmentExpression or an ArgumentList, followed by a comma, followed by an AssignmentExpression. This definition of ArgumentList is recursive, that is, it is defined in terms of itself. The result is that an ArgumentList may contain any positive number of arguments, separated by commas, where each argument expression is an AssignmentExpression. Such recursive definitions of nonterminals are common.