Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / node767.html
CommitLineData
920dae64
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="node768.html" />
13<link rel="prev" href="module-parser.html" />
14<link rel="parent" href="module-parser.html" />
15<link rel="next" href="node768.html" />
16<meta name='aesop' content='information' />
17<title>18.1.1 Creating 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 parser "
25 href="module-parser.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.2 Converting AST Objects"
31 href="node768.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="module-parser.html">18.1 parser </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="node768.html">18.1.2 Converting AST Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H2><A NAME="SECTION0020110000000000000000"></A><A NAME="Creating_ASTs"></A>
56<BR>
5718.1.1 Creating AST Objects
58</H2>
59
60<P>
61AST objects may be created from source code or from a parse tree.
62When creating an AST object from source, different functions are used
63to create the <code>'eval'</code> and <code>'exec'</code> forms.
64
65<P>
66<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
67 <td><nobr><b><tt id='l2h-4938' xml:id='l2h-4938' class="function">expr</tt></b>(</nobr></td>
68 <td><var>source</var>)</td></tr></table></dt>
69<dd>
70The <tt class="function">expr()</tt> function parses the parameter <var>source</var>
71as if it were an input to "<tt class="samp">compile(<var>source</var>, 'file.py',
72'eval')</tt>". If the parse succeeds, an AST object is created to hold the
73internal parse tree representation, otherwise an appropriate exception
74is thrown.
75</dl>
76
77<P>
78<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
79 <td><nobr><b><tt id='l2h-4939' xml:id='l2h-4939' class="function">suite</tt></b>(</nobr></td>
80 <td><var>source</var>)</td></tr></table></dt>
81<dd>
82The <tt class="function">suite()</tt> function parses the parameter <var>source</var>
83as if it were an input to "<tt class="samp">compile(<var>source</var>, 'file.py',
84'exec')</tt>". If the parse succeeds, an AST object is created to hold the
85internal parse tree representation, otherwise an appropriate exception
86is thrown.
87</dl>
88
89<P>
90<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
91 <td><nobr><b><tt id='l2h-4940' xml:id='l2h-4940' class="function">sequence2ast</tt></b>(</nobr></td>
92 <td><var>sequence</var>)</td></tr></table></dt>
93<dd>
94This function accepts a parse tree represented as a sequence and
95builds an internal representation if possible. If it can validate
96that the tree conforms to the Python grammar and all nodes are valid
97node types in the host version of Python, an AST object is created
98from the internal representation and returned to the called. If there
99is a problem creating the internal representation, or if the tree
100cannot be validated, a <tt class="exception">ParserError</tt> exception is thrown. An AST
101object created this way should not be assumed to compile correctly;
102normal exceptions thrown by compilation may still be initiated when
103the AST object is passed to <tt class="function">compileast()</tt>. This may indicate
104problems not related to syntax (such as a <tt class="exception">MemoryError</tt>
105exception), but may also be due to constructs such as the result of
106parsing <code>del f(0)</code>, which escapes the Python parser but is
107checked by the bytecode compiler.
108
109<P>
110Sequences representing terminal tokens may be represented as either
111two-element lists of the form <code>(1, 'name')</code> or as three-element
112lists of the form <code>(1, 'name', 56)</code>. If the third element is
113present, it is assumed to be a valid line number. The line number
114may be specified for any subset of the terminal symbols in the input
115tree.
116</dl>
117
118<P>
119<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
120 <td><nobr><b><tt id='l2h-4941' xml:id='l2h-4941' class="function">tuple2ast</tt></b>(</nobr></td>
121 <td><var>sequence</var>)</td></tr></table></dt>
122<dd>
123This is the same function as <tt class="function">sequence2ast()</tt>. This entry point
124is maintained for backward compatibility.
125</dl>
126
127<P>
128
129<DIV CLASS="navigation">
130<div class='online-navigation'>
131<p></p><hr />
132<table align="center" width="100%" cellpadding="0" cellspacing="2">
133<tr>
134<td class='online-navigation'><a rel="prev" title="18.1 parser "
135 href="module-parser.html"><img src='../icons/previous.png'
136 border='0' height='32' alt='Previous Page' width='32' /></A></td>
137<td class='online-navigation'><a rel="parent" title="18.1 parser "
138 href="module-parser.html"><img src='../icons/up.png'
139 border='0' height='32' alt='Up One Level' width='32' /></A></td>
140<td class='online-navigation'><a rel="next" title="18.1.2 Converting AST Objects"
141 href="node768.html"><img src='../icons/next.png'
142 border='0' height='32' alt='Next Page' width='32' /></A></td>
143<td align="center" width="100%">Python Library Reference</td>
144<td class='online-navigation'><a rel="contents" title="Table of Contents"
145 href="contents.html"><img src='../icons/contents.png'
146 border='0' height='32' alt='Contents' width='32' /></A></td>
147<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
148 border='0' height='32' alt='Module Index' width='32' /></a></td>
149<td class='online-navigation'><a rel="index" title="Index"
150 href="genindex.html"><img src='../icons/index.png'
151 border='0' height='32' alt='Index' width='32' /></A></td>
152</tr></table>
153<div class='online-navigation'>
154<b class="navlabel">Previous:</b>
155<a class="sectref" rel="prev" href="module-parser.html">18.1 parser </A>
156<b class="navlabel">Up:</b>
157<a class="sectref" rel="parent" href="module-parser.html">18.1 parser </A>
158<b class="navlabel">Next:</b>
159<a class="sectref" rel="next" href="node768.html">18.1.2 Converting AST Objects</A>
160</div>
161</div>
162<hr />
163<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
164</DIV>
165<!--End of Navigation Panel-->
166<ADDRESS>
167See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
168</ADDRESS>
169</BODY>
170</HTML>