... | ... | @@ -44,7 +44,7 @@ If(expr test, stmt* body, stmt* orelse) |
|
|
```
|
|
|
Both `if_single_line` and `if_multi_line` and both grammars (i.e., 4 combinations) all produce exactly the same AST node. The AST lost information about which of the two ways the if statement was written, as well as how the code was formatted (white space, parentheses, semicolons, ...).
|
|
|
|
|
|
The parse tree is the most detailed tree which corresponds 1:1 with how exactly the code is written (usually it ignores white space, but that's about it). AST on the other hand is the most abstract tree that ignores as much about the code as it can, the only condition is that the code generated from AST, while inevitably different from the original code, must produce exactly the same results as the original when compiled (by some other compiler that, say, generates machine code directly from the parse tree, skipping AST). An AST however is still able to represent different algorithms that produce the same results, such as array vs element-wise operations. In other words, AST abstracts equivalent syntax, but not equivalent algorithms.
|
|
|
The parse tree is the most detailed tree which corresponds 1:1 with how exactly the code is written (usually it ignores white space, but that's about it). AST on the other hand is the most abstract tree that ignores as much about the code as it can, the only condition is that the code generated from AST, while inevitably different from the original code, must produce exactly the same results as the original when compiled by some compiler that correctly implements the language. An AST however is still able to represent different algorithms that produce the same results, such as array vs element-wise operations. In other words, AST abstracts equivalent syntax, but not equivalent algorithms.
|
|
|
|
|
|
Examples of parse trees and ASTs:
|
|
|
|
... | ... | |