【问题标题】:Lexical analysis in MRI Ruby 1.9.2MRI Ruby 1.9.2 中的词法分析
【发布时间】:2010-10-25 16:22:19
【问题描述】:

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

【问题讨论】:

    标签: ruby compiler-construction lexer


    【解决方案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. */
    };
    

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

    但我不是专家。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-20
      • 2011-08-25
      相关资源
      最近更新 更多