1.2.2 Applications generation
Stages of compilation
Compilation is split into several stages:
Lexical analysis
The source code is tokenised into a stream of tokens, which are the basic building blocks of the language (e.g. keywords, identifiers, literals, operators, etc.).
Syntax analysis
The stream of tokens is parsed into a syntax tree, which represents the structure of the program according to the grammar of the language.
Semantic analysis
The syntax tree is checked for semantic errors (e.g. type checking, variable declaration, etc.) and annotated with additional information (e.g. symbol tables, type information, etc.).
Intermediate code generation
An intermediate representation of the program is generated, which is often a lower-level representation that is easier to optimize and translate into machine code.
Optimization
The intermediate code is optimized to improve performance, reduce memory usage,or achieve other goals (e.g. inlining, loop unrolling, dead code elimination, etc.).
Code generation
The optimized intermediate code is translated into machine code or assembly language, which can be executed by the target platform.