What is Abstract Syntax Tree (AST)?
An abstract syntax tree (AST) is a way of representing the syntax of a programming language as a hierarchical tree structure. This structure is used to generate symbol tables for compilers and later for code generation. The tree represents all constructs in the language and their subsequent rules.
An abstract syntax tree represents all of the syntactic elements of a programming language, similar to the syntax trees that linguists use for human languages. The tree focuses on the rules rather than elements like parentheses or semicolons that end statements in some languages. The tree is hierarchical, with the elements of the programming instructions broken down into their parts. For example, a tree for a conditional statement contains the rules for variables that depend on the required operator.
ASTs are often used in compilers to check the code for accuracy. If the generated tree contains errors, the compiler issues an error message. ASTs are used because some constructs cannot be represented in context-free grammar, such as implicit typing. ASTs are very specific to programming languages, but research on universal syntax trees is ongoing.