Ruby语法

45

我正在寻找Ruby BNF语法形式的内容。是否有官方版本?

3个回答

38

YACC语法在Ruby源代码中。下载并运行捆绑的工具以获取可读的语法。

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

输出样例(v2.0.0-p195版本共918行)

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
                ;

12
现在只需运行 git clone https://github.com/ruby/ruby ; cd ruby ; ruby sample/exyacc.rb < parse.y 即可。这将从 Github 克隆 Ruby 代码仓库,进入该仓库的目录,然后使用解析器文件 parse.y 运行 exyacc.rb 脚本。 - Lance

15

5
但我认为第一个链接并不是“官方”的语法。我听说 Ruby 最接近官方语法的东西是 parse.y 文件。 - Eric Walker
6
很遗憾,类似于UoB和njit语法是完全错误的。for循环中没有do语句,缺少extend和include语句以及异常变量。 - user246672

5

链接未经密码授权,无法访问。 - Jeroen Heijmans
1
感谢您的报告。当我发表那条评论时,旧链接是有效的。但是我已经添加了一个新链接。现在应该可以使用了。 - GutenYe
非常酷。我看到它是为1.8.7版本的Ruby设计的。是否有适用于Ruby 2.0的更新版本? - sunnyrjuneja

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接