Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="node769.html" /> | |
13 | <link rel="prev" href="node767.html" /> | |
14 | <link rel="parent" href="module-parser.html" /> | |
15 | <link rel="next" href="node769.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>18.1.2 Converting AST Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="18.1.1 Creating AST Objects" | |
25 | href="node767.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="18.1 parser " | |
28 | href="module-parser.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="18.1.3 Queries on AST" | |
31 | href="node769.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="node767.html">18.1.1 Creating AST Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-parser.html">18.1 parser </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node769.html">18.1.3 Queries on AST</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0020120000000000000000"></A><A NAME="Converting_ASTs"></A> | |
56 | <BR> | |
57 | 18.1.2 Converting AST Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | AST objects, regardless of the input used to create them, may be | |
62 | converted to parse trees represented as list- or tuple- trees, or may | |
63 | be compiled into executable code objects. Parse trees may be | |
64 | extracted with or without line numbering information. | |
65 | ||
66 | <P> | |
67 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
68 | <td><nobr><b><tt id='l2h-4942' xml:id='l2h-4942' class="function">ast2list</tt></b>(</nobr></td> | |
69 | <td><var>ast</var><big>[</big><var>, line_info</var><big>]</big><var></var>)</td></tr></table></dt> | |
70 | <dd> | |
71 | This function accepts an AST object from the caller in | |
72 | <var>ast</var> and returns a Python list representing the | |
73 | equivalent parse tree. The resulting list representation can be used | |
74 | for inspection or the creation of a new parse tree in list form. This | |
75 | function does not fail so long as memory is available to build the | |
76 | list representation. If the parse tree will only be used for | |
77 | inspection, <tt class="function">ast2tuple()</tt> should be used instead to reduce memory | |
78 | consumption and fragmentation. When the list representation is | |
79 | required, this function is significantly faster than retrieving a | |
80 | tuple representation and converting that to nested lists. | |
81 | ||
82 | <P> | |
83 | If <var>line_info</var> is true, line number information will be | |
84 | included for all terminal tokens as a third element of the list | |
85 | representing the token. Note that the line number provided specifies | |
86 | the line on which the token <em>ends</em>. This information is | |
87 | omitted if the flag is false or omitted. | |
88 | </dl> | |
89 | ||
90 | <P> | |
91 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
92 | <td><nobr><b><tt id='l2h-4943' xml:id='l2h-4943' class="function">ast2tuple</tt></b>(</nobr></td> | |
93 | <td><var>ast</var><big>[</big><var>, line_info</var><big>]</big><var></var>)</td></tr></table></dt> | |
94 | <dd> | |
95 | This function accepts an AST object from the caller in | |
96 | <var>ast</var> and returns a Python tuple representing the | |
97 | equivalent parse tree. Other than returning a tuple instead of a | |
98 | list, this function is identical to <tt class="function">ast2list()</tt>. | |
99 | ||
100 | <P> | |
101 | If <var>line_info</var> is true, line number information will be | |
102 | included for all terminal tokens as a third element of the list | |
103 | representing the token. This information is omitted if the flag is | |
104 | false or omitted. | |
105 | </dl> | |
106 | ||
107 | <P> | |
108 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
109 | <td><nobr><b><tt id='l2h-4944' xml:id='l2h-4944' class="function">compileast</tt></b>(</nobr></td> | |
110 | <td><var>ast</var><big>[</big><var>, filename<code> = '<ast>'</code></var><big>]</big><var></var>)</td></tr></table></dt> | |
111 | <dd> | |
112 | The Python byte compiler can be invoked on an AST object to produce | |
113 | code objects which can be used as part of an <tt class="keyword">exec</tt> statement or | |
114 | a call to the built-in <tt class="function">eval()</tt><a id='l2h-4945' xml:id='l2h-4945'></a> function. | |
115 | This function provides the interface to the compiler, passing the | |
116 | internal parse tree from <var>ast</var> to the parser, using the | |
117 | source file name specified by the <var>filename</var> parameter. | |
118 | The default value supplied for <var>filename</var> indicates that | |
119 | the source was an AST object. | |
120 | ||
121 | <P> | |
122 | Compiling an AST object may result in exceptions related to | |
123 | compilation; an example would be a <tt class="exception">SyntaxError</tt> caused by the | |
124 | parse tree for <code>del f(0)</code>: this statement is considered legal | |
125 | within the formal grammar for Python but is not a legal language | |
126 | construct. The <tt class="exception">SyntaxError</tt> raised for this condition is | |
127 | actually generated by the Python byte-compiler normally, which is why | |
128 | it can be raised at this point by the <tt class="module">parser</tt> module. Most | |
129 | causes of compilation failure can be diagnosed programmatically by | |
130 | inspection of the parse tree. | |
131 | </dl> | |
132 | ||
133 | <P> | |
134 | ||
135 | <DIV CLASS="navigation"> | |
136 | <div class='online-navigation'> | |
137 | <p></p><hr /> | |
138 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
139 | <tr> | |
140 | <td class='online-navigation'><a rel="prev" title="18.1.1 Creating AST Objects" | |
141 | href="node767.html"><img src='../icons/previous.png' | |
142 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
143 | <td class='online-navigation'><a rel="parent" title="18.1 parser " | |
144 | href="module-parser.html"><img src='../icons/up.png' | |
145 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
146 | <td class='online-navigation'><a rel="next" title="18.1.3 Queries on AST" | |
147 | href="node769.html"><img src='../icons/next.png' | |
148 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
149 | <td align="center" width="100%">Python Library Reference</td> | |
150 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
151 | href="contents.html"><img src='../icons/contents.png' | |
152 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
153 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
154 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
155 | <td class='online-navigation'><a rel="index" title="Index" | |
156 | href="genindex.html"><img src='../icons/index.png' | |
157 | border='0' height='32' alt='Index' width='32' /></A></td> | |
158 | </tr></table> | |
159 | <div class='online-navigation'> | |
160 | <b class="navlabel">Previous:</b> | |
161 | <a class="sectref" rel="prev" href="node767.html">18.1.1 Creating AST Objects</A> | |
162 | <b class="navlabel">Up:</b> | |
163 | <a class="sectref" rel="parent" href="module-parser.html">18.1 parser </A> | |
164 | <b class="navlabel">Next:</b> | |
165 | <a class="sectref" rel="next" href="node769.html">18.1.3 Queries on AST</A> | |
166 | </div> | |
167 | </div> | |
168 | <hr /> | |
169 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
170 | </DIV> | |
171 | <!--End of Navigation Panel--> | |
172 | <ADDRESS> | |
173 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
174 | </ADDRESS> | |
175 | </BODY> | |
176 | </HTML> |