MRI Ruby 1.9.2 中的词法分析

发布于 2024-09-29 17:52:41 字数 91 浏览 12 评论 0原文

我目前正在学习一些编译器理论和实践。 Ruby 是我日常选择的语言,所以我去查看它的词法分析器和解析语法。 ruby 有单独的词法分析器吗?如果有,它在哪个文件中描述?

I'm learning some compiler theory and practice at the moment. Ruby is my every day language of choice, and so I went to look at its lexer and parse grammar. Does ruby have a separate lexer? If so, which file is it described in?

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

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

发布评论

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

评论(1

潇烟暮雨 2024-10-06 17:52:41

在 ruby​​ 源代码中有包含语法的 parse.y 文件。我相对确定 ruby​​ 使用单独的词法分析器(就像大多数 LR 解析器一样)。另外,词法分析器似乎是有状态的:

enum lex_state_e {
EXPR_BEG,           /* ignore newline, +/- is a sign. */
EXPR_END,           /* newline significant, +/- is an operator. */
EXPR_ENDARG,        /* ditto, and unbound braces. */
EXPR_ARG,           /* newline significant, +/- is an operator. */
EXPR_CMDARG,        /* newline significant, +/- is an operator. */
EXPR_MID,           /* newline significant, +/- is an operator. */
EXPR_FNAME,         /* ignore newline, no reserved words. */
EXPR_DOT,           /* right after `.' or `::', no reserved words. */
EXPR_CLASS,         /* immediate after `class', no here document. */
EXPR_VALUE          /* alike EXPR_BEG but label is disallowed. */
};

我想这是必要的,因为在某些情况下会忽略换行符,而在其他情况下它会终止表达式等。而且“class”并不总是像“x.class”中的关键字。

但我不是专家。

编辑:深入查看 parse.y 文件,词法分析器并不完全与解析器分开:

superclass  : //[...]
    | '<'
        {
        lex_state = EXPR_BEG;
        }

In the ruby source there is the parse.y file which contains the grammar. I am relatively sure that ruby uses a separate lexer (like most LR parsers). Also it seems like the lexer is stateful:

enum lex_state_e {
EXPR_BEG,           /* ignore newline, +/- is a sign. */
EXPR_END,           /* newline significant, +/- is an operator. */
EXPR_ENDARG,        /* ditto, and unbound braces. */
EXPR_ARG,           /* newline significant, +/- is an operator. */
EXPR_CMDARG,        /* newline significant, +/- is an operator. */
EXPR_MID,           /* newline significant, +/- is an operator. */
EXPR_FNAME,         /* ignore newline, no reserved words. */
EXPR_DOT,           /* right after `.' or `::', no reserved words. */
EXPR_CLASS,         /* immediate after `class', no here document. */
EXPR_VALUE          /* alike EXPR_BEG but label is disallowed. */
};

I guess this necessary because a newline is ignored in some cases and in other cases it terminates expressions etc. Also 'class' is not always a keyword like e.g. in 'x.class'.

But i'm no expert.

EDIT: Looking deeper in the parse.y file the lexer is not completely separate from the parser:

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