In Python, the ast.parse()
function splits the source code into tokens based on the grammar. These tokens are then transformed to build an Abstract Syntax Tree (AST). It is a tree representation of a source code. In other words, the ast.parse()
method allows the conversion of the source code into a collection of AST nodes linked together as a tree.
ast.parse(source, filename='<unknown>', mode='exec', *, type_comments=False, feature_version=None)
source
: The source code in a string, which is to be converted into an AST.filename
: The file from which the code is read.mode
: The mode in which the code is compiled. It is an exec
, which takes a code block that has statements, class, functions, and so on. It can also be an exec
that only accepts a single expression.type_comments
: The default value is False
. If True
is returned, the parser is modified to check. It returns type comments specified by PEP 484 , and PEP 526.feature_version
: The default value is None
. If it is set to a tuple, say (3,4)
, it attempts to parse using the Python 3.4 version’s grammar.The ast.parse()
method converts the source code to return a collection of AST nodes.
import astfrom pprint import pprinttree = ast.parse("""def add(a, b):return a + b""")pprint(ast.dump(tree))
add(a,b)
that returns the sum of the input numbers.add
function is passed as a string to the ast.parse()
method. It transforms the source code into a collection of AST nodes.The output has several AST nodes, such as:
FunctionDef:
Function definition.BinOp:
Binary operation.We can also observe arguments inside these nodes. For instance, the value of the Op
argument as Add()
in the BinOp
node.