A compiler is a program that transforms a program written in the source language - taken as input - and produces a file in a different language. Usually, it’s a binary file that can be executed by the machine, but it doesn’t need to.
There are many types of language processors:
- Compilers convert from a high-level language to a low-level language. For example, from C to machine language.
- Decompiler convert from executables to high-level languages.
Generically, the compilation process can be divided into two major steps: analysis and synthesis. Analysis takes the source code, easily readable by a human, as input and creates an intermediate representation of that source code. Synthesis takes that representation and transforms it into the final target program.
graph LR a(Source) --> lex[Lexical<br>Analysis] lex --> syn[Syntactic<br>Analysis] syn --> sem[Semantical<br>Analysis] sem --> int[Intermediate<br>Code Generator] int --> opt[Code Optimizer] opt --> gen[Code Generator] gen --> ex(Executable) lex --> err[Error<br>Handling] syn --> err sem --> err int --> err opt --> err gen --> err
- Lexical Analysis
- Syntactic Analysis
- Semantic Analysis
- Intermediate Code Generation
- After the semantic analysis, the compiler generates an intermediate code of the source code that is note coupled to a certain machine. It is somewhat translate to connect between this code and the target machine code.
- Code Optimization
- Code Generation
- Finally, the code generator takes the optimized representation of the intermediate code and maps it to the target machine language. The output of this phase can be executed by the target machine.