Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between parse trees and abstract syntax trees? [duplicate]

Are they generated by different phases of a compiling process? Or are they just different names for the same thing?

like image 257
Thomson Avatar asked Feb 17 '11 08:02

Thomson


People also ask

Is parse tree and syntax tree same?

The main difference between parse tree and syntax tree is that parse tree is a hierarchical structure that represents the derivation of the grammar to obtain input strings while syntax tree is a way of representing the syntax of a programming language as a hierarchical tree similar structure.

How does it differ from an AST?

CST(Concrete Syntax Tree) is a tree representation of the Grammar(Rules of how the program should be written). Depending on compiler architecture, it can be used by the Parser to produce an AST. AST(Abstract Syntax Tree) is a tree representation of Parsed source, produced by the Parser part of the compiler.

What is 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 tree represents a construct occurring in the source code.

What is the difference between abstract syntax and concrete syntax?

It consists of a set of rules (productions) that define the way programs look like to the programmer. The concrete syntax of Pico is defined by the productions in the file ConcrSyn. pco . The abstract syntax of an implementation is the set of trees used to represent programs in the implementation.


1 Answers

This is based on the Expression Evaluator grammar by Terrence Parr.

The grammar for this example:

grammar Expr002;  options  {     output=AST;     ASTLabelType=CommonTree; // type of $stat.tree ref etc... }  prog    :   ( stat )+ ;  stat    :   expr NEWLINE        -> expr         |   ID '=' expr NEWLINE -> ^('=' ID expr)         |   NEWLINE             ->         ;  expr    :   multExpr (( '+'^ | '-'^ ) multExpr)*         ;   multExpr         :   atom ('*'^ atom)*         ;   atom    :   INT          |   ID         |   '('! expr ')'!         ;  ID      : ('a'..'z' | 'A'..'Z' )+ ; INT     : '0'..'9'+ ; NEWLINE : '\r'? '\n' ; WS      : ( ' ' | '\t' )+ { skip(); } ; 

Input

x=1 y=2 3*(x+y) 

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.

Parse Tree

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.

AST

For a more through explanation see Compilers and Compiler Generators pg. 23
or Abstract Syntax Trees on pg. 21 in Syntax and Semantics of Programming Languages

like image 151
Guy Coder Avatar answered Nov 03 '22 05:11

Guy Coder