1 语法树(parse tree):

是在parsing阶段,derivation的图像化表示,parser tree focus on grammar的actual implemment,包括像white spaces, braces, keywords, parenthesis 等一些细节。 “parse tree” 也叫 “concrete syntax tree” ,它represents the syntactic structure of a string(or token stream)according to some context-free grammar.

2 抽象语法树(abstract syntax tree)

AST is a tree representation of the abstract syntactic structure of source codewritten in a programming language.它 focus on source code的 各个 components 之间的abstract relationships。 it doesn’t need to contain all the syntactical elements

3 What’s the difference

1.everal grammars for the same language will give different parse trees but should result to the same AST
2. AST 不focus on the ways they are generated by a grammar.。 AST focuses on programming constructs。也就是operators:computational operators( ±*/ [a, b]),for operator(for [ expr, expr, expr, stmnt ])

4 statement的parse tree & AST

java code snippet
for (int i = 0 ; i< 5; i=i+1)
{
 sum = sum+i;
}
parse tree

grammar: grammars-v4/java

语法树与抽象语法树(parse tree & abstract syntax tree)

AST

为了体现和parser tree的对比
语法树与抽象语法树(parse tree & abstract syntax tree)
一般AST都表示为
语法树与抽象语法树(parse tree & abstract syntax tree)

5 block的parse tree & AST

6 expression的parse tree & AST

7 class的parse tree & AST

java code snippet
package visualize_AST;
public class Student {  
    private String name; 
    private int age;
    public Student() {}
    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }
    public String getName() {
        return name;
    }
parse tree

语法树与抽象语法树(parse tree & abstract syntax tree)

AST只关注program construction。

AST

语法树与抽象语法树(parse tree & abstract syntax tree)

reference 和blog :
Parse tree
What’s the difference between parse tree and AST?
What’s the difference between parse tree and AST?

相关文章: