我刚刚在这里读到(http://ruby.runpaint.org/programs#lexical),评论是标记。我从来没有把注释看作是标记,因为它们要么是注解,要么是用于后处理。
评论真的是标记吗?还是这个来源是错误的?
评论真的是标记吗?还是这个来源是错误的?
是的,它们应该是标记,但在解析器忽略它们后。如果您使用类似于这样的文件运行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)
是的,它们是解析器的标记。通常,如果你使用解析器生成器,这就是注释的定义。
{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语法。它们通常是被忽略的标记。
请记住,您在源代码中编写的所有内容都是标记,这始终是第一步。解析器需要从标记开始构建抽象语法树。