NullLiteral 如何以树形式表示?

发布于 2024-09-10 00:31:36 字数 883 浏览 2 评论 0原文

根据 ECMAScript 规范 中的 < strong>7.8.1 a NullLiteral 定义如下:

NullLiteral :: 
    null

我试图理解的是,当 NullLiteral 包含在其中时,它如何以树形式表示以下产生式可在 7.6.17.8 节中找到。

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

我对它的外观的最佳猜测是:

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

但这对我来说似乎并不正确。

注意

根据我的研究,似乎很少有编译器真正从语言语法生成 CST。我当然可以理解为什么,但这对我来说是一个学习练习,所以我想在转向更专业的解析方法(例如使用解析器生成器)之前先解决这个问题。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

影子的影子 2024-09-17 00:31:36

语法未涵盖所示的树,因为它不提供从 IdentifierNameReservedWord 的派生,并且不提供派生 ReservedWord code> 到 Literal 之一。

事实上,ReservedWord 产生式仅用于限制 IdentifierName 的有效值,这应该在词法级别上看到。它不会进入 CST,在 CST 中您只能看到 IdentifierName

Literal 的上下文是 PrimaryExpression,因此真实 CST 的片段可能如下所示:

   ...
    |
PrimaryExpression
    |
 Literal 
    |
NullLiteral 
    |
   null

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文