Stages of compilation Flashcards
What are the 4 stages of compilation in order?
- Lexical analysis
- Syntax analysis
- Code generation
- Code optimisation
What happens in lexical analysis of code?
The lexer analyses lexemes (words) in the source code, checks if it is valid against a pre defined set of rules and identifies the lexeme with a token (e.g. key words, constants, strings, numbers, operators…)
It then puts these tokens into a symbol table
What happens in syntax analysis of code?
Tokens from lexical analysis are received as inputs, the structure of the inputs are then analysed to ensure it is syntactically correct.
It then builds a parse tree (syntax tree) and updates the symbol table with data types.
What happens in code generation?
Machine code is generated
What happens in code optimisation?
The code is analysed to spot:
- Spotting redundant instruction that can be replaced in a more efficient way
- Removing subroutines that are never called
- Removing variables and constants that are never referenced