This article includes alist of references,related reading, orexternal links,but its sources remain unclear because it lacksinline citations. Please helpimprove this article byintroducing more precise citations.(February 2013) (Learn how and when to remove this message) |

whileb!=0:ifa>b:a:=a-belse:b:=b-areturna
Anabstract syntax tree (AST) is a data structure used incomputer science to represent the structure of a program or code snippet. It is atree representation of theabstract syntactic structure of text (oftensource code) written in aformal language. Each node of the tree denotes a construct occurring in the text. It is sometimes called just asyntax tree.
The syntax is "abstract" in the sense that it does not represent every detail appearing in the real syntax, but rather just the structural or content-related details. For instance, groupingparentheses are implicit in the tree structure, so these do not have to be represented as separate nodes. Likewise, a syntactic construct like an if-condition-then statement may be denoted by means of a single node with three branches.
This distinguishes abstract syntax trees from concrete syntax trees, traditionally designatedparse trees. Parse trees are typically built by aparser during the source code translation andcompiling process. Once built, additional information is added to the AST by means of subsequent processing, e.g.,contextual analysis.
Abstract syntax trees are also used inprogram analysis andprogram transformation systems.
Abstract syntax trees aredata structures widely used incompilers to represent the structure of program code. An AST is usually the result of thesyntax 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 affect on the final output of the compiler.
An AST has several properties that aid in the additional steps of the compilation process:
Languages are oftenambiguous by nature. In order to avoid this ambiguity, programming languages are often specified as acontext-free grammar (CFG). However, there are often aspects of programming languages that a CFG can't express, but are part of the language and are documented in its specification. These are details that require a context to determine their validity and behavior. For example, if a language allows new types to be declared, a CFG cannot predict the names of such types nor the way in which they should be used. Even if a language has a predefined set of types, enforcing proper usage usually requires some context. Another example isduck typing, where the type of an element can change depending on context.Operator overloading is yet another case where correct usage and final function are context-dependent.
The design of an AST is often closely linked with the design of a compiler and its expected features.
Core requirements include the following:
These requirements can be used to design the data structure for the AST.
Some operations will always require two elements, such as the two terms for addition. However, some language constructs require an arbitrarily large number of children, such as argument lists passed to programs from thecommand shell. As a result, an AST used to represent code written in such a language has to also be flexible enough to allow for the quick addition of an unknown quantity of children.
To support compiler verification it should be possible to unparse an AST into source code form. The source code produced should be sufficiently similar to the original in appearance and identical in execution, upon recompilation.The AST is used intensively duringsemantic analysis, where the compiler checks for correct usage of the elements of the program and the language. The compiler also generatessymbol tables based on the AST during semantic analysis. A complete traversal of the tree allows verification of the correctness of the program.
After verifying correctness, the AST serves as the base for code generation. The AST is often used to generate an intermediate representation (IR), sometimes called anintermediate language, for the code generation.
AST differencing, or for short tree differencing, consists of computing the list of differences between two ASTs.[1] This list of differences is typically called an edit script. The edit script directly refers to the AST of the code. For instance, an edit action may result in the addition of a new AST node representing a function.
An AST is a powerful abstraction to perform codeclone detection.[2]
Let be a set ofsorts, anarity is a tuple, for, also written as. More precisely,.
Let be an-indexed family of disjoint sets ofoperators. If is an operator arity we say that has sort and has arguments of sorts.
Fix be a finite set of sorts, and an-indexed family of disjoint sets ofoperators. Let be an-indexed family of disjoint sets of variables. The family ofabstract syntax trees, orASTs, is the smallest-indexed family of disjoint sets closed under the following conditions: