Question

According to the ECMAScript specification in section 7.8.1 a NullLiteral is defined as follows:

NullLiteral :: 
    null

What I am trying to understand is how this is represented in tree form when a NullLiteral is included in the following productions found in sections 7.6.1 and 7.8.

ReservedWord :: 
    Keyword 
    FutureReservedWord 
    NullLiteral 
    BooleanLiteral 
Literal :: 
    NullLiteral 
    BooleanLiteral 
    NumericLiteral 
    StringLiteral 

My best guess as to how it would look is this:

InputElementDiv
    |
  Token
    |
IdentifierName
    |
ReservedWord 
    |
 Literal 
    |
NullLiteral 
    |
   null

This just does not seem right to me though.

Note

From my research it seems that very few compilers actually generate CSTs from the language grammar. I can of course understand why but this is a learning exercise for me so I want to get this right before I move to more professional means of parsing such as using a parser generator.

Was it helpful?

Solution

The tree as shown is not covered by the grammar, because that does not provide a derivation from IdentifierName to ReservedWord, and it does not provide for deriving ReservedWord to Literal either.

The ReservedWord production in fact is used only to restrict valid values of IdentifierName, and this should be seen on the lexical level. It does not make it into a CST, where you would see just the IdentifierName.

The context of Literal is PrimaryExpression, so a fragment of a real CST could look like this:

   ...
    |
PrimaryExpression
    |
 Literal 
    |
NullLiteral 
    |
   null
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top