# Working with the parse tree in Ruby?

**URL:** <https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541>\
**Category:** Ruby\
**Created:** [January 27, 2007, 1:21am UTC](https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541 "2007-01-27T01:21:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_CSMartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@Martin\_CSMartin](https://www.ruby-forum.com/u/Martin_CSMartin)\
**Post date:** [January 27, 2007, 1:21am UTC](https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541/1 "2007-01-27T01:21:33Z")

</div>

Hi,

In Lisp, it’s easy to get the parse tree of an expression, and to  
manipulate those parse trees. That means Lisp code tends to use macros,  
i.e. lisp functions whose return values are parse trees. These macros  
are evaluated at compile time and inserted into the place where the  
macro call appears.

Is there anything equivalent in Ruby? Is there a way in Ruby to get the  
parse tree of, say, x + y? Is there a way to execute parse trees?

Best,  
Martin

---

<div class="post-metadata">

**Author:** ![Martin\_CSMartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@Martin\_CSMartin](https://www.ruby-forum.com/u/Martin_CSMartin)\
**Post date:** [January 27, 2007, 2:15am UTC](https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541/2 "2007-01-27T02:15:37Z")

</div>

On Sat, Jan 27, 2007 at 09:20:55AM +0900, Martin C. Martin wrote:

> Hi,
> 
> In Lisp, it’s easy to get the parse tree of an expression, and to  
> manipulate those parse trees. That means Lisp code tends to use macros,  
> i.e. lisp functions whose return values are parse trees. These macros  
> are evaluated at compile time and inserted into the place where the  
> macro call appears.
> 
> Is there anything equivalent in Ruby? Is there a way in Ruby to get the  
> parse tree of, say, x + y? Is there a way to execute parse trees?

Yes, you can get the parse tree using “ParseTree”

[http://rubyforge.org/projects/parsetree/](http://rubyforge.org/projects/parsetree/)

Then convert the parse tree back to ruby with ruby2ruby:

[http://rubyforge.org/projects/seattlerb/](http://rubyforge.org/projects/seattlerb/)

HTH!

---

<div class="post-metadata">

**Author:** ![Martin\_CSMartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@Martin\_CSMartin](https://www.ruby-forum.com/u/Martin_CSMartin)\
**Post date:** [January 27, 2007, 3:44pm UTC](https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541/3 "2007-01-27T15:44:48Z")

</div>

Martin C. Martin schrieb:

> In Lisp, it’s easy to get the parse tree of an expression, and to  
> manipulate those parse trees. That means Lisp code tends to use macros,  
> i.e. lisp functions whose return values are parse trees. These macros  
> are evaluated at compile time and inserted into the place where the  
> macro call appears.
> 
> Is there anything equivalent in Ruby? Is there a way in Ruby to get the  
> parse tree of, say, x + y? Is there a way to execute parse trees?

Martin, in addition to what Aaron has written, I’d like to ask you what  
you need this for. I’ve been playing with implementing macros in Ruby a  
few times, but I found no compelling reason to support them in Ruby.  
Yes, I could do some nice tricks, but I think they haven’t been worth  
the trouble. So I’d be interested in your use cases.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Martin\_CSMartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@Martin\_CSMartin](https://www.ruby-forum.com/u/Martin_CSMartin)\
**Post date:** [January 30, 2007, 12:40pm UTC](https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541/4 "2007-01-30T12:40:56Z")

</div>

Vladimir K. schrieb:

> well, i have a use case (but this is doable, i think, with existing ruby  
> using code generation and eval).  
> (…)

Vlad, thanks for your example. I too think that you can use eval to  
build the code. Further, it seems that you could implement a recursive  
solution without using eval at all.

The macros I have toyed with took some existing Ruby code and then  
changed it according to some rules.

Regards,  
Pit

---

<div class="post-metadata">

**Author:** ![Martin\_CSMartin](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ccd318/32.png) [@Martin\_CSMartin](https://www.ruby-forum.com/u/Martin_CSMartin)\
**Post date:** [January 29, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/working-with-the-parse-tree-in-ruby/83541/5 "2007-01-29T22:20:43Z")

</div>

> Martin, in addition to what Aaron has written, I’d like to ask you what  
> you need this for. I’ve been playing with implementing macros in Ruby a  
> few times, but I found no compelling reason to support them in Ruby.

well, i have a use case (but this is doable, i think, with existing ruby  
using code generation and eval).

i wanted to write a “universal netcdf dump” procedure for netcdf files  
(the netcdf library for ruby exists). the format i have has few  
dimensions and one multi-dimensional array. the problem is that the  
number of dimensions can vary - e.g. you can have value(dim1) or  
value(dim1,dim2,dim3…).

what i wanted was a general way to dump the multidimensional variable  
value + each related value in each dimension as a table for import to  
SQL. this translates to nested iterators (where number of iterators is  
the number of dimensions) e.g for 2 dimensions something like:

# this is pseudo code, dim1, dim2, value are arrays from netcdf

dim1[].length.times{|dim1\_i|  
dim2[].length.times{|dim2\_i|  
print dim1[dim1\_i], dim2[dim2\_i], value[dim1\_i,dim2\_i]  
}}

i could not imagine to do this without a macro (or without code  
generation and eval).

vlad

ps: i have not done the “universal netcdf dump” procedure as i do not  
really need it for the import but it was an interesting thing to think  
about .
