Stages of compilation Flashcards
When a compiler is used, high-level code goes through…
four stages before it is turned into
object code that is ready to be executed.
What are the 4 stages
Lexical Analysis
Syntax Analysis
Code Generation
Optimisation
Lexical Analysis
In the first stage of compilation, whitespace and comments are removed from the code.
The remaining code is analysed for keywords and names of variables and constants.
These are replaced with tokens and information about the token associated with each keyword or identifier is stored in a symbol table
Syntax Analysis
In this stage, tokens are analysed against the grammar and rules of the programming language.
Any tokens that break the rules of the programming language are flagged up as syntax errors and added to a list of errors.
An abstract syntax tree is produced, which is a representation of the source code in the form of a tree.
Further detail about identifiers is also added to the symbol table.
Semantic analysis is also carried out at the syntax analysis stage, where logic mistakes within the program are detected.
Examples of syntax errors:
undeclared variable type, incomplete set of brackets.
Examples of semantic errors:
multiple declaration,
undeclared identifiers.
Code Generation
The abstract syntax tree produced in the syntax analysis stage is used to produce machine code.
Optimisation
This stage of compilation searches through the code for areas it could be made more efficient, reducing execution time. Although this stage can significantly add to the overall time taken for compilation.
Insignificant, redundant parts of code are detected and removed.
Repeated sections of code may be grouped and replaced with a more efficient piece of code which produces the same result.
There is a danger, however, that excessive optimisation may alter the way in which the program behaves.