57 lines · 2.6 KB · TeX
RawDownload
% XXX Label can't be _ast?% XXX Where should this section/chapter go?\chapter{Abstract Syntax Trees\label{ast}} \sectionauthor{Martin v. L\"owis}{martin@v.loewis.de} \versionadded{2.5} The \code{_ast} module helps Python applications to processtrees of the Python abstract syntax grammar. The Python compilercurrently provides read-only access to such trees, meaning thatapplications can only create a tree for a given piece of Pythonsource code; generating byte code from a (potentially modified)tree is not supported. The abstract syntax itself might change witheach Python release; this module helps to find out programmaticallywhat the current grammar looks like. An abstract syntax tree can be generated by passing \code{_ast.PyCF_ONLY_AST}as a flag to the \function{compile} builtin function. The result will be a treeof objects whose classes all inherit from \code{_ast.AST}. The actual classes are derived from the \code{Parser/Python.asdl} file,which is reproduced below. There is one class defined for each left-handside symbol in the abstract grammar (for example, \code{_ast.stmt} or \code{_ast.expr}).In addition, there is one class defined for each constructor on theright-hand side; these classes inherit from the classes for the left-handside trees. For example, \code{_ast.BinOp} inherits from \code{_ast.expr}.For production rules with alternatives (aka "sums"), the left-hand sideclass is abstract: only instances of specific constructor nodes are evercreated. Each concrete class has an attribute \code{_fields} which gives thenames of all child nodes. Each instance of a concrete class has one attribute for each child node,of the type as defined in the grammar. For example, \code{_ast.BinOp}instances have an attribute \code{left} of type \code{_ast.expr}.  Instances of \code{_ast.expr} and \code{_ast.stmt} subclasses alsohave lineno and col_offset attributes.  The lineno is the line numberof source text (1 indexed so the first line is line 1) and thecol_offset is the utf8 byte offset of the first token that generatedthe node.  The utf8 offset is recorded because the parser uses utf8 internally. If these attributes are marked as optional in the grammar (using aquestion mark), the value might be \code{None}. If the attributescan have zero-or-more values (marked with an asterisk), thevalues are represented as Python lists. \section{Abstract Grammar} The module defines a string constant \code{__version__} whichis the decimal subversion revision number of the file shown below. The abstract grammar is currently defined as follows: \verbatiminput{../../Parser/Python.asdl}