<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>3.14.2 Converting AST Objects</TITLE>
<META NAME="description" CONTENT="3.14.2 Converting AST Objects">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="Querying_ASTs.html">
<LINK REL="previous" HREF="Creating_ASTs.html">
<LINK REL="up" HREF="module-parser.html">
<LINK REL="next" HREF="Querying_ASTs.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="Querying_ASTs.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-parser.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="Creating_ASTs.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="Querying_ASTs.html">3.14.3 Queries on AST</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-parser.html">3.14 parser  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="Creating_ASTs.html">3.14.1 Creating AST Objects</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0051420000000000000000"></A>
<BR>
3.14.2 Converting AST Objects
</H2>

<P>
AST objects, regardless of the input used to create them, may be
converted to parse trees represented as list- or tuple- trees, or may
be compiled into executable code objects.  Parse trees may be
extracted with or without line numbering information.

<P>
<dl><dt><b><a name="l2h-484"><tt class=function>ast2list</tt></a></b> (<var>ast</var><big>[</big><var>, line_info</var><big>]</big>)
<dd>
This function accepts an AST object from the caller in
<tt><var>ast</var></tt> and returns a Python list representing the
equivelent parse tree.  The resulting list representation can be used
for inspection or the creation of a new parse tree in list form.  This
function does not fail so long as memory is available to build the
list representation.  If the parse tree will only be used for
inspection, <tt class=function>ast2tuple()</tt> should be used instead to reduce memory
consumption and fragmentation.  When the list representation is
required, this function is significantly faster than retrieving a
tuple representation and converting that to nested lists.

<P>
If <tt><var>line_info</var></tt> is true, line number information will be
included for all terminal tokens as a third element of the list
representing the token.  Note that the line number provided specifies
the line on which the token <i>ends</i>.  This information is
omitted if the flag is false or omitted.
</dl>

<P>
<dl><dt><b><a name="l2h-485"><tt class=function>ast2tuple</tt></a></b> (<var>ast</var><big>[</big><var>, line_info</var><big>]</big>)
<dd>
This function accepts an AST object from the caller in
<tt><var>ast</var></tt> and returns a Python tuple representing the
equivelent parse tree.  Other than returning a tuple instead of a
list, this function is identical to <tt class=function>ast2list()</tt>.

<P>
If <tt><var>line_info</var></tt> is true, line number information will be
included for all terminal tokens as a third element of the list
representing the token.  This information is omitted if the flag is
false or omitted.
</dl>

<P>
<dl><dt><b><a name="l2h-486"><tt class=function>compileast</tt></a></b> (<var>ast</var><big>[</big><var>, filename<tt> = '&lt;ast&gt;'</tt></var><big>]</big>)
<dd>
The Python byte compiler can be invoked on an AST object to produce
code objects which can be used as part of an <tt>exec</tt> statement or
a call to the built-in <tt class=function>eval()</tt><a name="l2h-487"></a> function.
This function provides the interface to the compiler, passing the
internal parse tree from <tt><var>ast</var></tt> to the parser, using the
source file name specified by the <tt><var>filename</var></tt> parameter.
The default value supplied for <tt><var>filename</var></tt> indicates that
the source was an AST object.

<P>
Compiling an AST object may result in exceptions related to
compilation; an example would be a <tt class=exception>SyntaxError</tt> caused by the
parse tree for <tt>del f(0)</tt>: this statement is considered legal
within the formal grammar for Python but is not a legal language
construct.  The <tt class=exception>SyntaxError</tt> raised for this condition is
actually generated by the Python byte-compiler normally, which is why
it can be raised at this point by the <tt class=module>parser</tt> module.  Most
causes of compilation failure can be diagnosed programmatically by
inspection of the parse tree.
</dl>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="Querying_ASTs.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-parser.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="Creating_ASTs.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="Querying_ASTs.html">3.14.3 Queries on AST</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-parser.html">3.14 parser  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="Creating_ASTs.html">3.14.1 Creating AST Objects</A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
