# BNF-like grammar specified DIRECTLY in Ruby

**URL:** <https://rubytalk.org/t/bnf-like-grammar-specified-directly-in-ruby/17697>\
**Category:** ruby-talk\
**Created:** [16 April 2005 14:23 UTC](https://rubytalk.org/t/bnf-like-grammar-specified-directly-in-ruby/17697 "2005-04-16T14:23:26Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Eric\_Mahurin1](https://avatars.discourse-cdn.com/v4/letter/e/34f0e0/32.png) [@Eric\_Mahurin1](https://rubytalk.org/u/Eric_Mahurin1)\
**Post date:** [16 April 2005 14:23 UTC](https://rubytalk.org/t/bnf-like-grammar-specified-directly-in-ruby/17697/1 "2005-04-16T14:23:26Z")

</div>

Would it be better to put it on rubyforge after renaming?

Also, is it best rubygems? Is this the ruby equivalent of cpan  
for perl?

> **···**
>
> --- Jamis Buck \<jamis@37signals.com\> wrote:
> 
> > Very nice! One quibble: any chance you could change the name?
> > 
> > [http://rubyforge.org/projects/syntax](http://rubyforge.org/projects/syntax)
> > 
> > 🙂 Might get confusing if someone wants to install both libs.
> > 
> > - Jamis
> > 
> > On Apr 15, 2005, at 10:25 PM, Eric Mahurin wrote:
> > 
> > \> Here is my first contribution to Ruby:  
> > \>  
> > \> [http://raa.ruby-lang.org/project/syntax/](http://raa.ruby-lang.org/project/syntax/)  
> > \>  
> > \> There is still plenty missing in here and it is a work in  
> > \> progress, but I think it is ready for some of you to try it  
> > out  
> > \> if you like it.  
> > \>  
> > \> To get an idea of what this is, there is a simple  
> > expression  
> > \> evaluator example below. This is pure Ruby code - no yacc  
> > type  
> > \> compiling necessary. That's what I love about it.  
> > \>  
> > \> Eric  
> > \>  
> > \>  
> > \> #!/usr/bin/ruby -d  
> > \>  
> > \> require "syntax"  
> > \>  
> > \> NULL = Syntax::NULL  
> > \> INF = +1.0/0  
> > \> LOOP0 = (0..INF)  
> > \> LOOP1 = (1..INF)  
> > \>  
> > \> int = (("0".."9")\*LOOP1).qualify { |m| m.to\_s.to\_i }  
> > \>  
> > \> number = (  
> > \> int +  
> > \> (("."+int)|NULL) +  
> > \> ((("e"|"E")+("+"|"-"|NULL)+int)|NULL)  
> > \> ).qualify { |m|  
> > \> if (m.length\>1)  
> > \> m.to\_s.to\_f  
> > \> else  
> > \> m[0]  
> > \> end  
> > \> }  
> > \>  
> > \> ws = ((" "|"\t"|"\n")\*LOOP0).qualify { TRUE } # skipped  
> > with  
> > \> TRUE  
> > \>  
> > \> expr = Syntax::Pass.new # need to predefine object for  
> > \> recursion  
> > \>  
> > \> atom =  
> > \> (number+ws).qualify{|m|m[0]} |  
> > \> ("(" + expr + ")" + ws).qualify{|m|m[1]}  
> > \>  
> > \> term = (  
> > \> atom + ( ("\*"|"/"|"%") + ws + atom )\*LOOP0  
> > \> ).qualify { |m|  
> > \> product = m[0]  
> > \> m[1].each { |m|  
> > \> case m[0]  
> > \> when "\*" then product \*= m[1]  
> > \> when "/" then product /= m[1]  
> > \> when "%" then product %= m[1]  
> > \> end  
> > \> }  
> > \> product  
> > \> }  
> > \>  
> > \> expr \<\< (  
> > \> ws + term + ( ("+"|"-") + ws + term )\*LOOP0  
> > \> ).qualify { |m|  
> > \> sum = m[0]  
> > \> m[1].each { |m|  
> > \> case m[0]  
> > \> when "+" then sum += m[1]  
> > \> when "-" then sum -= m[1]  
> > \> end  
> > \> }  
> > \> sum  
> > \> }  
> > \>  
> > \> while (gets)  
> > \> p(expr===RandomAccessStream.new($\_))  
> > \> end
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do you Yahoo!?  
> Plan great trips with Yahoo! Travel: Now over 17,000 guides!  
> [http://travel.yahoo.com/p-travelguide](http://travel.yahoo.com/p-travelguide)

---

<div class="post-metadata">

**Author:** ![Jamis\_Buck2](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@Jamis\_Buck2](https://rubytalk.org/u/Jamis_Buck2)\
**Post date:** [16 April 2005 15:54 UTC](https://rubytalk.org/t/bnf-like-grammar-specified-directly-in-ruby/17697/2 "2005-04-16T15:54:51Z")

</div>

> Would it be better to put it on rubyforge after renaming?

That's not necessary, but it's nice if you need a CVS repository, or a home page, or a wiki for your project. It also gives you bug and feature request tracking, mailing lists, forums, and more. If any of that sounds appealing, you may want to apply for a new project there. If you have those things all hosted elsewhere, or you don't want/need them, then you probably won't gain anything by a RubyForge project (except, perhaps, better project visibility).

> Also, is it best rubygems? Is this the ruby equivalent of cpan  
> for perl?

Currently RubyGems is the de facto standard for Ruby libraries. There really isn't a CPAN equivalent for Ruby, yet--we've got RAA and RubyForge, with RubyGems as the package manager. (Another nice thing about RubyForge--when you add a gem file to your project file list, it will automatically be added to the RubyGems package list.)

- Jamis

> **···**
>
> On Apr 16, 2005, at 8:23 AM, Eric Mahurin wrote:
> 
> > --- Jamis Buck \<jamis@37signals.com\> wrote:
> > 
> > > Very nice! One quibble: any chance you could change the name?
> > > 
> > > [http://rubyforge.org/projects/syntax](http://rubyforge.org/projects/syntax)
> > > 
> > > 🙂 Might get confusing if someone wants to install both libs.
> > > 
> > > - Jamis
> > > 
> > > On Apr 15, 2005, at 10:25 PM, Eric Mahurin wrote:
> > > 
> > > > Here is my first contribution to Ruby:
> > > > 
> > > > [http://raa.ruby-lang.org/project/syntax/](http://raa.ruby-lang.org/project/syntax/)
> > > > 
> > > > There is still plenty missing in here and it is a work in  
> > > > progress, but I think it is ready for some of you to try it
> > > 
> > > out
> > > 
> > > > if you like it.
> > > > 
> > > > To get an idea of what this is, there is a simple
> > > 
> > > expression
> > > 
> > > > evaluator example below. This is pure Ruby code - no yacc
> > > 
> > > type
> > > 
> > > > compiling necessary. That's what I love about it.
> > > > 
> > > > Eric
> > > > 
> > > > #!/usr/bin/ruby -d
> > > > 
> > > > require "syntax"
> > > > 
> > > > NULL = Syntax::NULL  
> > > > INF = +1.0/0  
> > > > LOOP0 = (0..INF)  
> > > > LOOP1 = (1..INF)
> > > > 
> > > > int = (("0".."9")\*LOOP1).qualify { |m| m.to\_s.to\_i }
> > > > 
> > > > number = (  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;int +  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;(("."+int)|NULL) +  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;((("e"|"E")+("+"|"-"|NULL)+int)|NULL)  
> > > > ).qualify { |m|  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;if (m.length\>1)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m.to\_s.to\_f  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;m[0]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > }
> > > > 
> > > > ws = ((" "|"\t"|"\n")\*LOOP0).qualify { TRUE } # skipped
> > > 
> > > with
> > > 
> > > > TRUE
> > > > 
> > > > expr = Syntax::Pass.new # need to predefine object for  
> > > > recursion
> > > > 
> > > > atom =  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;(number+ws).qualify{|m|m[0]} |  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;("(" + expr + ")" + ws).qualify{|m|m[1]}
> > > > 
> > > > term = (  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;atom + ( ("\*"|"/"|"%") + ws + atom )\*LOOP0  
> > > > ).qualify { |m|  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;product = m[0]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;m[1].each { |m|  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case m[0]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "\*" then product \*= m[1]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "/" then product /= m[1]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "%" then product %= m[1]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;product  
> > > > }
> > > > 
> > > > expr \<\< (  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;ws + term + ( ("+"|"-") + ws + term )\*LOOP0  
> > > > ).qualify { |m|  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;sum = m[0]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;m[1].each { |m|  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case m[0]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "+" then sum += m[1]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;when "-" then sum -= m[1]  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;}  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;sum  
> > > > }
> > > > 
> > > > while (gets)  
> > > > &nbsp;&nbsp;&nbsp;&nbsp;p(expr===RandomAccessStream.new($\_))  
> > > > end
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > Do you Yahoo!?  
> > Plan great trips with Yahoo! Travel: Now over 17,000 guides!  
> > [http://travel.yahoo.com/p-travelguide](http://travel.yahoo.com/p-travelguide)
