I'm looking for Ruby grammar in BNF form. Is there an official version?
3 Answers
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
;
-
10now just
git clone https://github.com/ruby/ruby ; cd ruby ; ruby sample/exyacc.rb < parse.y
– LanceJun 8, 2014 at 7:09
Yes, there is one Ruby BNF syntax by the University of buffalo.
Edit: I've also found this alternate Ruby BNF syntax.
-
5But I don't think the first link is to an "official" grammar. I hear that the closest thing to an official grammar for Ruby is the parse.y file. Jan 1, 2011 at 0:10
-
6Unfortunately, the similar UoB and the njit grammars are completely wrong. There is no do in a for loop, extend and include statements are missing and exception variables are missing.– user246672Feb 7, 2013 at 3:46
also an official version: Ruby Draft Specification. you can find the grammar there.
Ruby Draft Specification: http://ruby-std.netlab.jp. the server is down, but you can download it from http://www.ipa.go.jp/osc/english/ruby
-
-
1thanks for the report. The old link worked when I posted that comment. but I've add a new link. It should work now.– GutenYeAug 31, 2011 at 1:55
-
Very cool.I see that its for 1.8.7. Is there a newer one for Ruby 2.0? Nov 30, 2012 at 4:40