[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

AST for ruby code

John Micheal

6/9/2008 11:10:00 AM

Hi!
I want AST for the following code as it is given below. I have install
ruby_parser 1.0 on my system. We are able to parse string using
"RubyParser.new.parse("1+1 = 2")" and the result is " s(:str, "1+1=2")".
But how I can parse the full source file(given below)?

Further, whenever I try to parse the string containing class or def in
it, it gives error (it means, it is parsing C code not ruby) I have also
attached the screenshot for this problem. Please also guide us in this
mater.


Ruby Code:
....................readfile.rb

class TestClass

def method1(a,b)
sum = a + b
puts sum
end

def method2(*c)
l = c.length
puts l
end
end

Desired AST Tree:
....................

s(:class, :TestClass, s(:const, :Object), s(:defn, :method1, s(:scope,
s(:block, s(:args, :a, :b), s(:lasgn, :sum, s(:call, s(:lvar, :a), :+,
s(:array, s(:lvar, :b)))), s(:fcall, :puts, s(:array, s(:lvar,
:sum)))))), s(:defn, :method2, s(:scope, s(:block, s(:args, :"*c"),
s(:lasgn, :l, s(:call, s(:lvar, :c), :length)), s(:fcall, :puts,
s(:array, s(:lvar, :l)))))))

Attachments:
http://www.ruby-...attachment/2131...

--
Posted via http://www.ruby-....

3 Answers

Luis Lavena

6/9/2008 7:08:00 PM

0

On Jun 9, 1:09 pm, John Micheal <zul_...@hotmail.com> wrote:
> Hi!
> I want AST for the following code as it is given below. I have install
> ruby_parser 1.0 on my system. We are able to parse string using
> "RubyParser.new.parse("1+1 = 2")" and the result is " s(:str, "1+1=2")".
> But how I can parse the full source file(given below)?
>
> Further, whenever I try to parse the string containing class or def in
> it, it gives error (it means, it is parsing C code not ruby) I have also
> attached the screenshot for this problem. Please also guide us in this
> mater.
>
> Ruby Code:
> ...................readfile.rb
>
> class TestClass
>
>      def method1(a,b)
>         sum = a + b
>         puts sum
>      end
>
>      def method2(*c)
>         l = c.length
>         puts l
>      end
> end
>
> Desired AST Tree:
> ...................
>
> s(:class, :TestClass, s(:const, :Object), s(:defn, :method1, s(:scope,
> s(:block, s(:args, :a, :b), s(:lasgn, :sum, s(:call, s(:lvar, :a), :+,
> s(:array, s(:lvar, :b)))), s(:fcall, :puts, s(:array, s(:lvar,
> :sum)))))), s(:defn, :method2, s(:scope, s(:block, s(:args, :"*c"),
> s(:lasgn, :l, s(:call, s(:lvar, :c), :length)), s(:fcall, :puts,
> s(:array, s(:lvar, :l)))))))
>
> Attachments:http://www.ruby-forum.com/attachment/2131...
>

On line 14 of your sample you're starting a block (well, not
actually), just replace the |file| just just file and see if it works.

HTH,
--
Luis Lavena

Ryan Davis

6/9/2008 10:35:00 PM

0


On Jun 9, 2008, at 04:09 , John Micheal wrote:

> But how I can parse the full source file(given below)?

RubyParser.new.parse(File.read(path))

> Further, whenever I try to parse the string containing class or def in
> it, it gives error (it means, it is parsing C code not ruby) I have
> also

because you're throwing individual lines at the parser when it expects
complete source.

RubyParser.new.parse("class Blah") is not valid ruby.


Radoslaw Bulat

6/9/2008 11:10:00 PM

0

WW91IGFyZSB0cnlpbmcgdG8gcGFyc2UgaXQgbGluZSBieSBsaW5lIGJ1dCBpdCB3b24ndCB3b3Jr
IGluIHRoYXQgd2F5LgpKdXN0IHBhcnNlIHdob2xlIGZpbGU6CgpzZXhwID0gUnVieVBhcnNlci5u
ZXcucGFyc2UoRmlsZS5yZWFkKCJyZWFkbGluZS5yYiIpKQoKLS0gClJhZG9zs2F3IEJ1s2F0Cgpo
dHRwOi8vcmFkYXJlay5qb2dnZXIucGwgLSBt82ogYmxvZwo=