I think you just do a depth-first traversal of the parse tree.   For example 
something sort of like this (shown for a node representing a binary operator):

class BinaryOperator(object):
      def __init__(self, operator, left, right):
              self.operator = operator
              self.left = left
              self.right  = right
      def collapse(self):
              return self.left.collapse() + self.operator + 
self.right.collapse()

Note:  You'll need to careful about too much recursion depending on the depth 
of your parse tree.

Cheers,
Dave


On Sep 27, 2011, at 7:01 AM, Tarek Ziadé wrote:

> On Tue, Sep 27, 2011 at 1:29 PM, Michael Foord <[email protected]> wrote:
> ..
>> 
>> If the AST contains (stores) the original tokens the collapsing an AST back
>> to "code" should be trivial. At resolver systems we used PLY to rewrite a
>> custom language to Python via AST transformations. Each AST node had a
>> "collapse" method (recursive) that would return a string representing the
>> node.
>> 
>> Having parsed you could get code back by calling collapse on the top level
>> node.
> 
> That sounds like the best idea. I'll try this.
> 
> Do you happen to have a public example ?
> 
> Cheers
> 
> 
> 
>> 
>> Michael Foord
>> 
>> 
>> 
>> 
>> 
>>> 
>>> Thanks
>>> Tarek
>>> 
>>> --
>>> Tarek Ziadé | http://ziade.org
>>> 
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "ply-hack" group.
>>> To post to this group, send email to [email protected].
>>> To unsubscribe from this group, send email to
>>> [email protected].
>>> For more options, visit this group at
>>> http://groups.google.com/group/ply-hack?hl=en.
>>> 
>> 
>> 
>> 
>> --
>> 
>> http://www.voidspace.org.uk/
>> 
>> May you do good and not evil
>> May you find forgiveness for yourself and forgive others
>> 
>> May you share freely, never taking more than you give.
>> -- the sqlite blessing http://www.sqlite.org/different.html
>> 
>> --
>> You received this message because you are subscribed to the Google Groups
>> "ply-hack" group.
>> To post to this group, send email to [email protected].
>> To unsubscribe from this group, send email to
>> [email protected].
>> For more options, visit this group at
>> http://groups.google.com/group/ply-hack?hl=en.
>> 
> 
> 
> 
> -- 
> Tarek Ziadé | http://ziade.org
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "ply-hack" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to 
> [email protected].
> For more options, visit this group at 
> http://groups.google.com/group/ply-hack?hl=en.
> 

-- 
You received this message because you are subscribed to the Google Groups 
"ply-hack" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/ply-hack?hl=en.

Reply via email to