[http://pysch.sourceforge.net/] [index.html] [user.html] [developer.html] [ast.html]

Python AST as XML

XML can be considered as an external representation of in-memory tree-like structures, and XML-related standards — as methods of processing such data. Some types of applications may benefit from this approach. One of the examples are compilers with theirs parse trees.

The Pysch distribution contains an example of representing Python parse trees (abstract syntax trees, AST) as XML and applying an XSLT transformation to these trees.

At the moment the whole code should be considered as a toy, not as a production version. Anyway, it works.

The code and sample data are located in the folder ast. See the file readme.txt in this folder for details.

Indeed, AST is binded not to XML but to SXML. SXML is a representation of the XML Infoset in the form of S-expressions. The program ast2sexp.py takes a name of a Python program and exports its AST as SXML. In order to get usual XML use the program ast2xml.py.

A transformation example is the XSLT code ast2dotty.xsl or the Scheme code ast2dotty.scm. Both do conversion of an AST tree to a graph description for the dotty program from the GraphViz graph drawing tools. The XSLT code uses XML exported by the ast2xml.py, the Scheme code is interpreted by the program ast2dotty.py.

dotty screenshots


A part of the hello.py AST


The ast2dotty.py AST


Author: Oleg Paraschenko
Hosted by:
SourceForge.net Logo