Lexical Analysis in MRI Ruby 1.9.2

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

+3
source share
1 answer

There is a file in the ruby ​​source parse.ythat contains the grammar. I'm sure Ruby uses a separate lexer (like most LR parsers). It also seems that the lexer has the state:

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. */
};

, , , .. "" , , . 'x.class'.

.

: parse.y, :

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

Source: https://habr.com/ru/post/1771337/


All Articles