# Ruby parsing in Ruby

**URL:** <https://rubytalk.org/t/ruby-parsing-in-ruby/32845>\
**Category:** ruby-talk\
**Created:** [11 November 2006 15:55 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845 "2006-11-11T15:55:04Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Wil](https://avatars.discourse-cdn.com/v4/letter/w/59ef9b/32.png) [@Wil](https://rubytalk.org/u/Wil)\
**Post date:** [11 November 2006 15:55 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/1 "2006-11-11T15:55:04Z")

</div>

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/](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](http://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

---

<div class="post-metadata">

**Author:** ![juan\_pedro\_merino](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@juan\_pedro\_merino](https://rubytalk.org/u/juan_pedro_merino)\
**Post date:** [11 November 2006 15:59 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/2 "2006-11-11T15:59:23Z")

</div>

are you create a game as ruby?

> **···**
>
> 2006/11/11, Wil \<iamwil@gmail.com\>:
> 
> > 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/\](http://i.loveruby.net/en/projects/racc/%5C)), 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

---

<div class="post-metadata">

**Author:** ![Joey](https://avatars.discourse-cdn.com/v4/letter/j/f14d63/32.png) [@Joey](https://rubytalk.org/u/Joey)\
**Post date:** [11 November 2006 16:19 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/3 "2006-11-11T16:19:56Z")

</div>

gem install ParseTree

j`ey  
[http://www.eachmapinject.com](http://www.eachmapinject.com)

> **···**
>
> On 11/11/06, Wil \<iamwil@gmail.com\> wrote:
> 
> > 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/\](http://i.loveruby.net/en/projects/racc/%5C)), 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

---

<div class="post-metadata">

**Author:** ![Interfecus](https://avatars.discourse-cdn.com/v4/letter/i/d78d45/32.png) [@Interfecus](https://rubytalk.org/u/Interfecus)\
**Post date:** [12 November 2006 09:45 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/4 "2006-11-12T09:45:05Z")

</div>

Wil wrote:

> 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/\](http://i.loveruby.net/en/projects/racc/%5C)), 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.

---

<div class="post-metadata">

**Author:** ![juan\_pedro\_merino](https://avatars.discourse-cdn.com/v4/letter/j/e95f7d/32.png) [@juan\_pedro\_merino](https://rubytalk.org/u/juan_pedro_merino)\
**Post date:** [11 November 2006 16:02 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/5 "2006-11-11T16:02:55Z")

</div>

i said with ruby

> **···**
>
> 2006/11/11, juan pedro meriño \<juapdiaz@gmail.com\>:
> 
> > are you create a game as ruby?
> > 
> > 2006/11/11, Wil \<iamwil@gmail.com\>:  
> > \>  
> > \> 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/\](http://i.loveruby.net/en/projects/racc/%5C)), 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  
> > \>

---

<div class="post-metadata">

**Author:** ![David\_Vallner](https://avatars.discourse-cdn.com/v4/letter/d/eb9ed0/32.png) [@David\_Vallner](https://rubytalk.org/u/David_Vallner)\
**Post date:** [11 November 2006 20:35 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/6 "2006-11-11T20:35:20Z")

</div>

juan pedro meriño wrote:

> are you create a game as ruby?

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

David Vallner

---

<div class="post-metadata">

**Author:** ![Wil](https://avatars.discourse-cdn.com/v4/letter/w/59ef9b/32.png) [@Wil](https://rubytalk.org/u/Wil)\
**Post date:** [12 November 2006 16:35 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/7 "2006-11-12T16:35:08Z")

</div>

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

Timothy Goddard wrote:

> **···**
>
> > Wil wrote:  
> > \> 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/\](http://i.loveruby.net/en/projects/racc/%5C)), 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.

---

<div class="post-metadata">

**Author:** ![Jeff\_Schwab](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@Jeff\_Schwab](https://rubytalk.org/u/Jeff_Schwab)\
**Post date:** [12 November 2006 13:50 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/8 "2006-11-12T13:50:04Z")

</div>

David Vallner wrote:

> juan pedro meriño wrote:
> 
> > are you create a game as ruby?
> 
> Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.

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?

---

<div class="post-metadata">

**Author:** ![Dido\_Sevilla](https://avatars.discourse-cdn.com/v4/letter/d/9dc877/32.png) [@Dido\_Sevilla](https://rubytalk.org/u/Dido_Sevilla)\
**Post date:** [12 November 2006 22:02 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/9 "2006-11-12T22:02:58Z")

</div>

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

> **···**
>
> On 11/13/06, Wil \<iamwil@gmail.com\> wrote:
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![Eric\_Hodel1](https://avatars.discourse-cdn.com/v4/letter/e/a9adbd/32.png) [@Eric\_Hodel1](https://rubytalk.org/u/Eric_Hodel1)\
**Post date:** [13 November 2006 21:01 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/10 "2006-11-13T21:01:16Z")

</div>

$ ri ParseTree | grep string  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parse\_tree, parse\_tree\_for\_method, parse\_tree\_for\_string, process  
$ ri ParseTree#parse\_tree\_for\_string  
---------------------------------------- ParseTree#parse\_tree\_for\_string  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;parse\_tree\_for\_string(source, filename = nil, line = nil, newlines  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;= false)

> **···**
>
> On Nov 12, 2006, at 8:35 AM, Wil wrote:
> 
> > Timothy Goddard wrote:
> > 
> > > Wil wrote:
> > > 
> > > > What would be the easiest way to parse Ruby source into parse trees in  
> > > > Ruby? Thanks!
> > > 
> > > Take a look at parsetree. It should be able to do this quite happily  
> > > and uses Ruby's own parser. This avoids compatibility issues.
> > 
> > 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.
> 
> ------------------------------------------------------------------------  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns the parse tree for a string +source+.
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Format:
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[[sexps] ... ]
> 
> --  
> Eric Hodel - drbrain@segment7.net - [http://blog.segment7.net](http://blog.segment7.net)  
> This implementation is HODEL-HASH-9600 compliant
> 
> [http://trackmap.robotcoop.com](http://trackmap.robotcoop.com)

---

<div class="post-metadata">

**Author:** ![HAL\_9000](https://avatars.discourse-cdn.com/v4/letter/h/90ced4/32.png) [@HAL\_9000](https://rubytalk.org/u/HAL_9000)\
**Post date:** [12 November 2006 16:28 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/11 "2006-11-12T16:28:45Z")

</div>

Jeffrey Schwab wrote:

> David Vallner wrote:
> 
> > juan pedro meriño wrote:
> > 
> > > are you create a game as ruby?
> > 
> > Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.
> 
> 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

---

<div class="post-metadata">

**Author:** ![Jeff\_Schwab](https://avatars.discourse-cdn.com/v4/letter/j/b5ac83/32.png) [@Jeff\_Schwab](https://rubytalk.org/u/Jeff_Schwab)\
**Post date:** [12 November 2006 16:55 UTC](https://rubytalk.org/t/ruby-parsing-in-ruby/32845/12 "2006-11-12T16:55:05Z")

</div>

Hal Fulton wrote:

> Jeffrey Schwab wrote:
> 
> > David Vallner wrote:
> > 
> > > juan pedro meriño wrote:
> > > 
> > > > are you create a game as ruby?
> > > 
> > > Stop spamming arbitrary threads with unrelated chiming in. This isn't IRC.
> > 
> > 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...

Thanks. Sorry, David!

I don't see Juan Pedro's post at all, though I see David's reply. Hm.
