On 27 September 2011 11:44, Tarek Ziadé <[email protected]> wrote:

> Hey
>
> I am happy with my DSL, I can yacc.parse() it it and create an AST in
> memory.
>
> I am now trying to add a feature where, for a given AST, I can
> generate the corresponding portion of DSL.
>
> The AST I've created in memory is a simple tuple for every expression
> I am visiting, and I was wondering what is the best way to do the
> revert operation.
>
> The problem I am facing is that I don't want to maintain on one side a
> grammar to parse my DSL, and on the other side a function to parse the
> AST and do the revert operation.
>
> I was wondering how this is usually solved, or if PLY provides some
> helpers for this
>

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.

All the best,

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.

Reply via email to