[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

BNF-like grammar specified DIRECTLY in Ruby

Eric Mahurin

4/16/2005 4:25:00 AM

Here is my first contribution to Ruby:

http://raa.ruby-lang.org/proje...

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!?
Make Yahoo! your home page
http://www.yaho...


2 Answers

Jamis Buck

4/16/2005 4:39:00 AM

0

Very nice! One quibble: any chance you could change the name?

http://rubyforge.org/proje...

:) 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/proje...
>
> 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!?
> Make Yahoo! your home page
> http://www.yaho...
>



Takaaki Tateishi

4/16/2005 3:14:00 PM

0

Eric Mahurin wrote:
> Here is my first contribution to Ruby:
> http://raa.ruby-lang.org/proje...

Let me inform you that I'm also developing similar library TDP4R. :-)
http://rubyforge.org/proj...
Some examples are available at:
http://rubyforge.org/cgi-bin/viewcvs.cgi/tdp4r/samples/?cvs...

Thanks,
--
Takaaki Tateishi <ttate@ttsky.net>