Compilers are computer programs that translate one language to another.

Interpreters vs compilers

Interpreters executes this source code instantly after translating the source code to byte code. (Bytecode is a lower level language that that is ran on a virtual machine that translates this bytecode to machine code).

Compiler phases

The front-end phases of the compiler are:

  1. Scanner and Lexical analysis
  2. Parser that does Context-free grammar and Parsing
  3. Semantic Analyzer
  4. Source code optimizer: It does optimization to the code like constant folding (replace constant operations with a single constant) and constant propagation (propagate a constant value through the program and replace the operation with a single constant).

The output of the front end phase is intermediate code (i don’t know if it is the syntax tree or not) which then is passed to the back-end of the compiler. The back-end consists of the code-generator that outputs the assembly of the target machine or whatever the target language is. After that, it may be passed to an optimizer stage that can optimize the target language more.

What does the optimizer do: For example: replacing MUL $r2, 2 by SLL $r2, 1 which is much faster

Compiler T-diagram, Cross-compiling and Bootstrapping

What is meant by machine here is not literal machine, a more accurate meaning would be machine code H or language H, so the first T diagram means a compilers that is written in language or machine code H and compilers language A to language B.