Compilers
A compiler translates source code written in a high-level language into machine code.
An interpreter directly executes source code without pre-compilation into machine code.
Learn
- #todo https://craftinginterpreters.com
- #todo Compilers: Principles, Techniques, and Tools
Terminology
- AST: Abstract Syntax Tree, a data structure used to represent the structure of a program
- IR: Intermediate Representation, a data structure or a code written in an intermediate language
- AOT: Ahead-of-time compilation (before the execution of a program)
- JIT: Just-in-time compilation (during the execution of a program)
Phases
Front end:
- Preprocessing: macro substitution, conditional compilation (e.g. C preprocessor)
- Lexical analysis: converting source code into lexical tokens (keywords, identifiers, operators, etc)
- Syntax analysis: parsing lexical tokens, checking syntax correctness
- Semantic analysis: checking semantic rules (e.g. checking types or ensuring that a variable is declared before use), building the symbol table (mapping each symbol to its type, scope, location)
Middle end:
- Machine-independent optimizations (e.g. removing dead/unreachable code, evaluating constants)
Back end:
- Machine-dependent optimizations
- Code generation: generating native machine code from IR
The front/middle/back-end approach allows combining front ends for different languages with back ends for different CPUs, while sharing the optimizations of the middle end: