@@ -50,4 +50,10 @@ Some examples of parse trees and ASTs:
...
@@ -50,4 +50,10 @@ Some examples of parse trees and ASTs:
* Python: [parse tree](https://docs.python.org/3/reference/grammar.html) and [AST](https://docs.python.org/3/library/ast.html#abstract-grammar).
* Python: [parse tree](https://docs.python.org/3/reference/grammar.html) and [AST](https://docs.python.org/3/library/ast.html#abstract-grammar).
* C: [parse tree](https://www.lysator.liu.se/c/ANSI-C-grammar-y.html) and [AST](https://www.cs.utah.edu/flux/flick/current/doc/guts/gutsch6.html).
* C: [parse tree](https://www.lysator.liu.se/c/ANSI-C-grammar-y.html) and [AST](https://www.cs.utah.edu/flux/flick/current/doc/guts/gutsch6.html).
* Fortran: [parse tree](http://docs.cray.com/books/007-3694-003/html-007-3694-003/faxalchri.html)(see also the Appendix D in the [Fortran 2008 Standard](http://www.j3-fortran.org/doc/year/10/10-007.pdf) for another parse tree) and [AST]().
* Fortran: [parse tree](http://docs.cray.com/books/007-3694-003/html-007-3694-003/faxalchri.html)(see also the Appendix D in the [Fortran 2008 Standard](http://www.j3-fortran.org/doc/year/10/10-007.pdf) for another parse tree) and [AST]().
\ No newline at end of file
# Does every compiler need an AST?
No, one can just use the parse tree as generated by, e.g., ANTLR, and generate LLVM IR directly from that, without ever constructing an AST. The advantage of that approach is that the compiler is faster, and perhaps easier/quicker to write initially. The disadvantage is that since the parse tree is not unique and has different rules for equivalent code (see the "if" example above), one has to deal with this in all the other stages of the compiler, and change those every time the grammar is changed. Also one then depends on a particular parser and how it represents the parse tree.
By using an AST, the parser's job is then well defined: just produce an AST. The AST is well defined and does not change (unless you change the language). One can keep extending the parser to cover more syntax corner cases, or one can change the parser library. At the end of the day, the only result that is needed for the rest of the compiler is an AST, and so the rest of the compiler does not depend on the parser at all. So the compiler is easier to maintain.