Max Koretskyi
Max Koretskyi

Reputation: 105497

Why use `IdentifierName` symbol recursively in the productions for the `IdentifierName`

I'm looking at the following productions of the IdentifierName symbol in the lexical EcmaScript grammar:

IdentifierName ::
    IdentifierStart
    IdentifierName IdentifierPart

I'm wondering why the grammar uses IdentifierName in the second row of productions?

I was thinking first that IdentifierName can be expanded into IdentifierStart and hence for the IdentifierName we can get the following:

IdentifierName -> IdentifierStart IdentifierPart

But isn't that case then can be put like this?

IdentifierName ::
    IdentifierStart
    IdentifierStart IdentifierPart

    ^^^^
    here `IdentifierStart` instead of `IdentifierName`

How using this grammar I can build a parse tree for the variable name git?

Upvotes: 1

Views: 65

Answers (1)

sepp2k
sepp2k

Reputation: 370202

Both IdentifierStart and IdentifierPart only match a single character each. So if the rule were IdentifierName -> IdentifierStart IdentifierPart, it would only match identifiers with a length of exactly 2 and your second suggestion would match identifiers of length 1 or 2.

The recursion is how the rule can match identifiers of arbitrary length.


Here's how you'd derive "git":

                 IdentifierName
                /              \
        IdentifierName IdentifierPart
       /              \         |
IdentifierName IdentifierPart  't'
       |                |
IdentiiferStart        'i'
       |
      'g'

Upvotes: 3

Related Questions