PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > AST for ruby code
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
AST for ruby code

Réponse
 
LinkBack Outils de la discussion
Vieux 09/06/2008, 12h09   #1
John Micheal
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut AST for ruby code

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/error.GIF

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

  Réponse avec citation
Vieux 09/06/2008, 20h07   #2
Luis Lavena
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: AST for ruby code

On Jun 9, 1:09pm, 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/error.GIF
>


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
  Réponse avec citation
Vieux 09/06/2008, 23h35   #3
Ryan Davis
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: AST for ruby code


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.


  Réponse avec citation
Vieux 10/06/2008, 00h10   #4
Rados³aw Bu³at
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: AST for ruby code

WW91IGFyZSB0cnlpbmcgdG8gcGFyc2UgaXQgbGluZSBieSBsaW 5lIGJ1dCBpdCB3b24ndCB3b3Jr
IGluIHRoYXQgd2F5LgpKdXN0IHBhcnNlIHdob2xlIGZpbGU6Cg pzZXhwID0gUnVieVBhcnNlci5u
ZXcucGFyc2UoRmlsZS5yZWFkKCJyZWFkbGluZS5yYiIpKQoKLS 0gClJhZG9zs2F3IEJ1s2F0Cgpo
dHRwOi8vcmFkYXJlay5qb2dnZXIucGwgLSBt82ogYmxvZwo=

  Réponse avec citation
Vieux 10/06/2008, 20h44   #5
Michel Son
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: AST for ruby code

Radosław Bułat wrote:
> You are trying to parse it line by line but it won't work in that way.
> Just parse whole file:
>
> sexp = RubyParser.new.parse(File.read("readline.rb"))
>
> --
> Rados�aw Bu�at
>
> http://radarek.jogger.pl - m�j blog



------------------
One more from you that this code is working perfectly on Ruby IDE,
can u tell me how does it works on Ruby Rail. Because when i run it
using ruby on rails, it is not displaying anything and also no errors.
Even I have properly include rubygems and ruby_parser.

Even Rails server stops working

<%
require "rubygems"
require "ruby_parser"

sexpfaulty1 = RubyParser.new.parse(File.read("faulty_0.rb"))

%>

<%= sexpfaulty1 %>
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 17h06.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,17222 seconds with 13 queries