Ruby Grammar

后端 未结 3 2117
眼角桃花
眼角桃花 2020-12-02 11:10

I\'m looking for Ruby grammar in BNF form. Is there an official version?

3条回答
  •  甜味超标
    2020-12-02 11:44

    The YACC syntax is in the Ruby source. Download it and run the bundled utiliy to get the readable syntax.

    wget ftp://ftp.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p195.tar.gz
    tar xvzf ruby-2.0.0-p195.tar.gz
    cd ruby-2.0.0-p195
    ruby sample/exyacc.rb < parse.y
    

    Output sample (total 918 lines for the v2.0.0-p195)

    program         : top_compstmt
                    ;
    
    top_compstmt    : top_stmts opt_terms
                    ;
    
    top_stmts       : none
                    | top_stmt
                    | top_stmts terms top_stmt
                    | error top_stmt
                    ;
    
    top_stmt        : stmt
                    | keyword_BEGIN
                      '{' top_compstmt '}'
                    ;
    
    bodystmt        : compstmt
                      opt_rescue
                      opt_else
                      opt_ensure
                    ;
    
    compstmt        : stmts opt_terms
                    ;
    

提交回复
热议问题