# I need a formula parser

**URL:** <https://rubytalk.org/t/i-need-a-formula-parser/17965>\
**Category:** ruby-talk\
**Created:** [28 April 2005 14:35 UTC](https://rubytalk.org/t/i-need-a-formula-parser/17965 "2005-04-28T14:35:16Z")\
**Posts on this page:** 1\
**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:** [28 April 2005 14:35 UTC](https://rubytalk.org/t/i-need-a-formula-parser/17965/1 "2005-04-28T14:35:16Z")

</div>

I posted this message a couple weeks ago. The example contains  
a simple expression parser - just +, -, \*, /, %, and (), but  
you should get the idea.

Bear in mind that the API is not frozen. Right now, I'm  
working on the API to the file, string, or even token stream  
(Cursor - like Iterator but more geared toward files). I'll  
also make it easier to make make lexers (they aren't necessary)  
and add a few more goodies. In the example below, I will  
likely change how recursion is done and defintely the  
Stream/Cursor interface.

Eric

> **···**
>
> -----------
> 
> 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
> 
> --- Lyndon Samson \<lyndon.samson@gmail.com\> wrote:
> 
> > On 4/28/05, James Edward Gray II \<james@grayproductions.net\> \> wrote:  
> > \> On Apr 28, 2005, at 2:18 AM, Lyndon Samson wrote:  
> > \>  
> > \> \> Sounds like another fine RubyQuiz!!!  
> > \>  
> > \> I miss Parse::RecDescent myself, but porting it is not  
> > exactly a quiz  
> > \> size task. Or perhaps I didn't understand what you  
> > meant...  
> > \>  
> > I meant a generic formula parser
> > 
> > ie.  
> > "(1+2)\*pow(2,2)", block callback for function execution  
> > "(1+2)\*pow($a,2)", passed in hash lookup for variable sub
> > 
> > etc
> > 
> > \> James Edward Gray II  
> > \>  
> > \>
> > 
> > --  
> > Into RFID? [www.rfidnewsupdate.com](http://www.rfidnewsupdate.com) Simple, fast, news.
> 
> \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> Do You Yahoo!?  
> Tired of spam? Yahoo! Mail has the best spam protection around
> 
> > **[Yahoo Mail - Organized Email](https://mail.yahoo.com/)**
> >
> > It's time to get stuff done with Yahoo Mail. Just add your Gmail, Outlook, AOL or Yahoo Mail to get going. We automatically organize all the things life throws at you, like receipts and attachments, so you can find what you need fast. Plus, we've got...
