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
AST
为了体现和parser tree的对比
一般AST都表示为
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
AST只关注program construction。
AST
reference 和blog :
Parse tree
What’s the difference between parse tree and AST?
What’s the difference between parse tree and AST?