I found the two terms in a compiler design book, and I'd like to know what each stands for, and how they are different.
I searched on the internet and found that parse trees are also called concrete syntax trees (CSTs).
I found the two terms in a compiler design book, and I'd like to know what each stands for, and how they are different.
I searched on the internet and found that parse trees are also called concrete syntax trees (CSTs).
Here's an explanation of parse trees (concrete syntax trees, CSTs) and abstract syntax trees (ASTs), in the context of compiler construction. They're similar data structures, but they're constructed differently and used for different tasks.
Parse trees
Parse trees are usually generated as the next step after lexical analysis (which turns the source code into a series of tokens that can be viewed as meaningful units, as opposed to just a sequence of characters).
They are tree-like data structures that shows how an input string of terminals (source code tokens) has been generated by the grammar of the language in question. The root of the parse tree is the most general symbol of the grammar - the start symbol (for example, statement), and the interior nodes represent nonterminal symbols that the start symbol expands to (can include the start symbol itself), such as expression, statement, term, function call. The leaves are the terminals of the grammar, the actual symbols which appear as identifiers, keywords, and constants in the language / input string, e.g. for, 9, if, etc.
While parsing the compiler also performs various checks to ensure the correctness of syntax - and and syntax error reports can be imbedded into parser code.
They can be used for syntax-directed translation via syntax-directed definitions or translation schemes, for simple tasks such as converting an infix expression to a postfix one.
Here's a graphical representation of a parse tree for the expression
9 - 5 + 2
(note the placement of the terminals in the tree and the actual symbols from the expression string):Abstract syntax trees
ASTs represent the syntactic structure of the some code. The trees of programming constructs such as expressions, flow control statements, etc - grouped into operators (interior nodes) and operands (leaves). For example, the syntax tree for the expression
i + 9
would have the operator+
as root, the variablei
as the operator's left child, and the number9
as the right child.The difference here is that nonterminals and terminals don't play a role, as ASTs don't deal with grammars and string generation, but programming constructs, and thus they represent relationships between such constructs, and not the ways they are generated by a grammar.
Note that the operators themselves are programming constructs in a given language, and don't have to be actual computational operators (like
+
is):for
loops would also be treated in this way. For example, you could have a syntax tree such asfor [ expr, expr, expr, stmnt ]
(represented inline), wherefor
is an operator, and the elements inside the square brackets are its children (representing C'sfor
syntax) - also composed out of operators etc.ASTs are usually generated by compilers in the syntax analysis (parsing) phase as well, and are used later for semantic analysis, intermediate representation, code generation, etc.
Here's a graphical representation of an AST:
I found this on the web, maybe helpful:
Wikipedia says
An answer on Quora says
Combining the above two definitions,
An
Abstract Syntax Tree
describes the parse tree logically. It does not need to contain all the syntactical constructs required to parse some source code (white spaces, braces, keywords, parenthesis etc). That's whyParse Tree
is also calledConcrete Syntax Tree
while the AST is calledSyntax Tree
. The output of syntax analyser is, thus, syntax tree actually.An AST describes the source code conceptually, it doesn't need to contain all the syntactical elements required to parse some source code (curly braces, keywords, parenthesis etc.).
A Parse tree represents the source code more closely.
In an AST the node for an IF statement could contain just three children:
For a C-like language the Parse Tree would need to contain nodes for the 'if' keyword, parenthesis, curly braces also.
This is based on the Expression Evaluator grammar by Terrence Parr.
The grammar for this example:
Input
Parse Tree
The parse tree is a concrete representation of the input. The parse tree retains all of the information of the input. The empty boxes represent whitespace, i.e. end of line.
AST
The AST is an abstract representation of the input. Notice that parens are not present in the AST because the associations are derivable from the tree structure.
EDIT
For a more through explanation see Compilers and Compiler Generators by P.D. Terry pg. 23. Also see the authors home page for more items such as source code.