Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby Grammar

Tags:

ruby

grammar

bnf

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

like image 918
user68109 Avatar asked Mar 19 '09 16:03

user68109


People also ask

What is :: in Ruby?

The :: is a unary operator and is used to access (anywhere outside the class or module) constants, instance methods and class methods defined within a class or module. Note: In Ruby, classes and methods may be considered constants too.

How do you write syntax in Ruby?

Ruby interprets newline characters(\n) and semicolons(;) as the end of a statement. Note: If a line has +, – or backslash at the end of a line, then it indicates the continuation of a statement.

What does {} do in Ruby?

Ruby blocks are anonymous functions that are enclosed in a do-end statement or curly braces {} . Usually, they are enclosed in a do-end statement if the block spans through multiple lines and {} if it's a single line block.

What is EOS in Ruby?

EOS means end of string. it is displayed at the end of the string.


2 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                 ; 
like image 198
Kenji Noguchi Avatar answered Sep 21 '22 21:09

Kenji Noguchi


Yes, there is one Ruby BNF syntax by the University of buffalo.

Edit: I've also found this alternate Ruby BNF syntax.

like image 38
Adrian Grigore Avatar answered Sep 22 '22 21:09

Adrian Grigore