Ruby parsing in Ruby

W

Wil

Hi all,

I've been doing Ruby for a small while now, but I'm a newbie when it
comes to parsing. I've been digging around, but still can't seem to
find the answer. I was wondering if Ruby itself has classes that I can
use to parse ruby source code into a parse tree.

My first inclination was to use Racc
(http://i.loveruby.net/en/projects/racc/), but I didn't want to have to
write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
didn't have to (maybe it's not that hard?). I wasn't able to find a
library of BNF for different languages (including Ruby), so then I
started looking elsewhere.

Both irb and ri probably use a Ruby parser of some sort, but looking at
the source code, it looked like each of them rolled their own. I
wasn't able to dig out the ruby parsing functionality from either by
including what I thought were necessary code modules.

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Wil
 
D

David Vallner

--------------enigB6E967D73D9EAF1297AE6384
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
are you create a game as ruby?
=20

Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC=
=2E

David Vallner


--------------enigB6E967D73D9EAF1297AE6384
Content-Type: application/pgp-signature; name="signature.asc"
Content-Description: OpenPGP digital signature
Content-Disposition: attachment; filename="signature.asc"

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.5 (MingW32)

iD8DBQFFVjQqy6MhrS8astoRAnSIAJ0fQnKKqQaYmu6uJ0VDCnsr3lBPCwCdHPyP
K/uty+qeNp07RSLW3oCwukc=
=lQR/
-----END PGP SIGNATURE-----

--------------enigB6E967D73D9EAF1297AE6384--
 
T

Timothy Goddard

Wil said:
Hi all,

I've been doing Ruby for a small while now, but I'm a newbie when it
comes to parsing. I've been digging around, but still can't seem to
find the answer. I was wondering if Ruby itself has classes that I can
use to parse ruby source code into a parse tree.

My first inclination was to use Racc
(http://i.loveruby.net/en/projects/racc/), but I didn't want to have to
write the BNF(en.wikipedia.org/wiki/Backus-Naur_form) for Ruby if I
didn't have to (maybe it's not that hard?). I wasn't able to find a
library of BNF for different languages (including Ruby), so then I
started looking elsewhere.

Both irb and ri probably use a Ruby parser of some sort, but looking at
the source code, it looked like each of them rolled their own. I
wasn't able to dig out the ruby parsing functionality from either by
including what I thought were necessary code modules.

What would be the easiest way to parse Ruby source into parse trees in
Ruby? Thanks!

Wil

Take a look at parsetree. It should be able to do this quite happily
and uses Ruby's own parser. This avoids compatibility issues.
 
H

Hal Fulton

Jeffrey said:
What are you talking about? The only one who appears to have chimed
into an arbitrary thread with something unrelated is you. Is this some
kind of hip irony, or am I missing something?

You're missing something. :) David was right.

Go read the context...

Cheers,
Hal
 
W

Wil

Hrm, I remember stumbling over parsetree, and it seems like the classes
need to already exist in object/class space (at least from the example
that they give). What if you just had a string of the source of the
classes? Perhaps they're one in the same. I should look at Parsetree
a little bit closer.

Wil
 
D

Dido Sevilla

Hrm, I remember stumbling over parsetree, and it seems like the classes
need to already exist in object/class space (at least from the example
that they give). What if you just had a string of the source of the
classes? Perhaps they're one in the same. I should look at Parsetree
a little bit closer.

Well, why not eval the string in that case then? Then the classes
would exist inside the interpreter.
 
E

Eric Hodel

Hrm, I remember stumbling over parsetree, and it seems like the
classes
need to already exist in object/class space (at least from the example
that they give). What if you just had a string of the source of the
classes? Perhaps they're one in the same. I should look at Parsetree
a little bit closer.

$ ri ParseTree | grep string
parse_tree, parse_tree_for_method, parse_tree_for_string, process
$ ri ParseTree#parse_tree_for_string
---------------------------------------- ParseTree#parse_tree_for_string
parse_tree_for_string(source, filename = nil, line = nil, newlines
= false)
------------------------------------------------------------------------
Returns the parse tree for a string +source+.

Format:

[[sexps] ... ]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

xml parsing in ruby 9
xml parsing in ruby 0
Ruby Code Parsing 9
Url parsing in ruby 9
Pdf Parsing Project Example 4
Byte–stream parsing in Ruby 13
Parsing C/C++ in Ruby 2
parsing json document 2

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top