|
|
# What is the difference between a parse tree and an AST?
|
|
|
|
|
|
The parse tree is corresponding to the grammar, e.g. EBNF grammar or the ANTLR4 grammar. The parse tree corresponds 1:1 to the code, e.g. one tree corresponds to
|
|
|
The **parse tree** corresponds to the grammar, e.g. EBNF grammar or the ANTLR4 grammar. The parse tree corresponds 1:1 to the code, e.g. one tree corresponds to
|
|
|
```fortran
|
|
|
if (a == 1) print *, "OK"
|
|
|
```
|
... | ... | @@ -38,7 +38,7 @@ The parse tree then has nodes for each rule in the grammar. In both grammars the |
|
|
|
|
|
As such the parse tree depends on the variants of the "if" statement in the code as well as on how the grammar rules are constructed.
|
|
|
|
|
|
The next step is to take the given parse tree and produce the Abstract Syntax Tree (AST). The AST for the above example is unique:
|
|
|
The **Abstract Syntax Tree (AST)**, on the other hand, is essentially unique for the above example:
|
|
|
```
|
|
|
If(expr test, stmt* body, stmt* orelse)
|
|
|
```
|
... | ... | |