Ruby:评论是代币吗?

发布于 2024-11-09 08:48:20 字数 193 浏览 0 评论 0原文

我刚刚读过这里(http://ruby.runpaint.org/programs#lexical)评论是令牌。我从未将注释视为标记,因为它们要么是注释,要么是后处理器的。

评论真的是象征性的还是这个来源是错误的?

I've just read here (http://ruby.runpaint.org/programs#lexical) that comments are tokens. I've never thought of comments as tokens as they're either annotations or for a post-processor.

Are comments really tokens or is this source wrong?

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

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

发布评论

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

评论(2

说好的呢 2024-11-16 08:48:20

是的,它们应该是标记,但稍后会被解析器忽略。如果您对如下所示的文件执行 ruby​​ --dump parsetree foo.rb ,

# this is a comment
1+1
# another comment

您将得到以下结果:

# @ NODE_SCOPE (line: 3)
# +- nd_tbl: (empty)
# +- nd_args:
# |   (null node)
# +- nd_body:
#     @ NODE_CALL (line: 2)
#     +- nd_mid: :+
#     +- nd_recv:
#     |   @ NODE_LIT (line: 2)
#     |   +- nd_lit: 1
#     +- nd_args:
#         @ NODE_ARRAY (line: 2)
#         +- nd_alen: 1
#         +- nd_head:
#         |   @ NODE_LIT (line: 2)
#         |   +- nd_lit: 1
#         +- nd_next:
#             (null node)

Yes, they should be tokens, but ignored by the parser later on. If you do ruby --dump parsetree foo.rb with a file that looks like this

# this is a comment
1+1
# another comment

this is what you'll get:

# @ NODE_SCOPE (line: 3)
# +- nd_tbl: (empty)
# +- nd_args:
# |   (null node)
# +- nd_body:
#     @ NODE_CALL (line: 2)
#     +- nd_mid: :+
#     +- nd_recv:
#     |   @ NODE_LIT (line: 2)
#     |   +- nd_lit: 1
#     +- nd_args:
#         @ NODE_ARRAY (line: 2)
#         +- nd_alen: 1
#         +- nd_head:
#         |   @ NODE_LIT (line: 2)
#         |   +- nd_lit: 1
#         +- nd_next:
#             (null node)
烂柯人 2024-11-16 08:48:20

是的,它们是解析器的标记。通常,如果您使用解析器生成器,这就是注释的定义

{code} short_comment = '//' not_cr_lf* eol | '#' not_cr_lf* eol;
{code} long_comment = '/*' not_star* '*'+ (not_star_slash not_star* '*'+)* '/';  /* '4vim */
Ignored Tokens
  short_comment,
  long_comment;

。这是 SableCC 语法。它们通常是被忽略的标记。

请记住,您在源代码中编写的所有内容都是一个令牌,这始终是第一步。解析器需要开始从标记构建抽象语法树。

Yeah they're tokens to the parser. Usually, if you use a parser generator this is the definition of a comment

{code} short_comment = '//' not_cr_lf* eol | '#' not_cr_lf* eol;
{code} long_comment = '/*' not_star* '*'+ (not_star_slash not_star* '*'+)* '/';  /* '4vim */
Ignored Tokens
  short_comment,
  long_comment;

This is a SableCC grammar. They're usually ignored tokens.

Remember that everything you write in a source code is a token, that's always the first step. The parser needs to start building the abstract syntax tree from tokens.

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