Abstract Syntax Tree

An Abstract Syntax Tree, or AST, is a tree representation of the source code of a computer program that conveys the structure of the source code. Each node in the syntax tree represents a construct occurring in the source code.

During conversion to it’s abstract syntax tree, only the structural and content-related details of the source code are preserved, and any additional details are discarded. Information that are preserved, and are vital to the syntax tree purpose, are:

  • Variable types, and location of each variable declaration
  • Order and definition of executable statements
  • Left and right components of binary operations
  • Identifiers and their assigned values

What is Abstract Syntax Tree used for?

Abstract syntax trees are used to represent the structure of a program’s source code for the compiler to use. An abstract syntax tree is usually the result of the syntax analysis phase of a compiler. It often serves as an intermediate representation of the program through several stages that the compiler requires, and has a strong impact on the final output of the compiler.

ASTs are also used for uses cases like static code analysis. Automated tools can traverse the AST of a program to find syntax errors and bad patterns in the code without actually executing it.

Automate objective parts of code reviews

Automate objective parts of code reviews

Get started