# Abstract Syntax Tree (AST)

> A tree that shows how the parts of a piece of code fit together.

**Field:** Software Development  
**Category:** Compilers & Runtimes  
**Also known as:** AST, Syntax Tree

## Think of it like

Think of it like a family tree for a line of code. Each part hangs under the part it belongs to.

## Definition

An abstract syntax tree is a tree that shows the structure of a piece of code. The parser builds it from the text. Each node is one part, like a sum, a name or a number, with its parts hanging below it. Spaces, brackets and comments are left out, so only the meaning stays. Compilers, linters and formatters all work on this tree, not on the raw text.

## Example

Python turns x = 1 + 2 into a tree. At the top is the assignment. Under it are the name x and an addition, with 1 and 2 below that.

## Example code

```console
$ python3 -c 'import ast; print(ast.dump(ast.parse("x = 1 + 2").body[0]))'
Assign(targets=[Name(id='x', ctx=Store())], value=BinOp(left=Constant(value=1), op=Add(), right=Constant(value=2)))
```

One line of code, shown as the tree Python builds from it.

## Why it matters

Tools that fix, check or change code safely all read the tree. That is how a rename touches only the right names.

## Common mistake

Changing code with find and replace when a tool could change the tree. Text search also hits names in strings and comments.

## Three principles

- **Built by the parser.** From the raw text.
- **Meaning only.** No spaces or brackets.
- **Tools work on it.** Compilers, linters, formatters.

## Related terms

- [Parser](https://learn.benibix.com/term/development/parser)
- [Compiler](https://learn.benibix.com/term/development/compiler)
- [Linter](https://learn.benibix.com/term/development/linter)
- [Syntax](https://learn.benibix.com/term/development/syntax)
- [Tree-sitter](https://learn.benibix.com/term/development/tree-sitter)

---

Source: https://learn.benibix.com/term/development/abstract-syntax-tree
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
