blob: 9f561562755b40812d2bfa1aad19524846ef27d7 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001.. _ast:
2
3Abstract Syntax Trees
4=====================
5
6.. module:: _ast
7 :synopsis: Abstract Syntax Tree classes.
8
9.. sectionauthor:: Martin v. Lรถwis <martin@v.loewis.de>
10
11
Georg Brandl116aa622007-08-15 14:28:22 +000012The ``_ast`` module helps Python applications to process trees of the Python
13abstract syntax grammar. The Python compiler currently provides read-only access
14to such trees, meaning that applications can only create a tree for a given
Georg Brandl9afde1c2007-11-01 20:32:30 +000015piece of Python source code; generating :term:`bytecode` from a (potentially modified)
Georg Brandl116aa622007-08-15 14:28:22 +000016tree is not supported. The abstract syntax itself might change with each Python
17release; this module helps to find out programmatically what the current grammar
18looks like.
19
20An abstract syntax tree can be generated by passing ``_ast.PyCF_ONLY_AST`` as a
21flag to the :func:`compile` builtin function. The result will be a tree of
22objects whose classes all inherit from ``_ast.AST``.
23
24The actual classes are derived from the ``Parser/Python.asdl`` file, which is
25reproduced below. There is one class defined for each left-hand side symbol in
26the abstract grammar (for example, ``_ast.stmt`` or ``_ast.expr``). In addition,
27there is one class defined for each constructor on the right-hand side; these
28classes inherit from the classes for the left-hand side trees. For example,
29``_ast.BinOp`` inherits from ``_ast.expr``. For production rules with
30alternatives (aka "sums"), the left-hand side class is abstract: only instances
31of specific constructor nodes are ever created.
32
33Each concrete class has an attribute ``_fields`` which gives the names of all
34child nodes.
35
36Each instance of a concrete class has one attribute for each child node, of the
37type as defined in the grammar. For example, ``_ast.BinOp`` instances have an
38attribute ``left`` of type ``_ast.expr``. Instances of ``_ast.expr`` and
39``_ast.stmt`` subclasses also have lineno and col_offset attributes. The lineno
40is the line number of source text (1 indexed so the first line is line 1) and
41the col_offset is the utf8 byte offset of the first token that generated the
42node. The utf8 offset is recorded because the parser uses utf8 internally.
43
44If these attributes are marked as optional in the grammar (using a question
45mark), the value might be ``None``. If the attributes can have zero-or-more
46values (marked with an asterisk), the values are represented as Python lists.
47
48
49Abstract Grammar
50----------------
51
52The module defines a string constant ``__version__`` which is the decimal
53subversion revision number of the file shown below.
54
55The abstract grammar is currently defined as follows:
56
57.. literalinclude:: ../../Parser/Python.asdl