2010-10-25 72 views
3

我正在學習一些編譯器理論和實踐。 Ruby是我每天選擇的語言,所以我去看看它的詞法分析器和語法分析。紅寶石是否有單獨的詞法分析器?如果是這樣,它描述了哪個文件?MRI中的詞法分析Ruby 1.9.2

回答

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

我想這必要的,因爲一個換行符在某些情況下,忽略,在其他情況下終止表情等也「階級」並不總是像例如關鍵字在'x.class'中。

但我不是專家。

編輯:尋找在parse.y文件中的詞法分析器不能從解析器完全獨立更深:

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