3

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

4

1 回答 1

1

在 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;
        }
于 2010-10-25T18:03:02.263 回答