Save
...
Unit 2 = Software and Software development
2.2 = Applications Generation
Stages of Compilation
Save
Share
Learn
Content
Leaderboard
Share
Learn
Created by
Rocco
Visit profile
Cards (7)
What is
Compilation
?
Compilation is a process that translates a program written in a high-level programming language into
machine code
Only machine code can be executed by a computer
There are four stages involved in this process:
Lexical Analysis
Syntax Analysis
Code Generation
Optimisation
Lexical Analysis
Lexical analysis means studying the words or vocabulary of a language
This stage involves identifying
lexical
'tokens' in the code
Syntax Analysis #1
Now that tokens have been identified, syntax analysis makes sure they all adhere to the syntax rules of the programming language
A symbol, e.g. '$' could be a valid token but not a valid character according to particular programming languages
The dollar symbol would be flagged as breaking the syntax rules
Other syntax errors programmers commonly make include mismatched parentheses or missing semicolons
Syntax Analysis
#2
If the code passes the syntax analysis, the compiler can create an
Abstract Syntax Tree
(
AST
)
An AST is a graph-based representation of the code being compiled
An AST is an efficient way to represent the code for the next step
Example
abstract syntax tree
Code Generation
This step takes the
AST
and traverses it to generate
object code
that can be executed by the computer
Optimisation
This step modifies the
code
to make it more efficient without changing its functionality
This is important to attempt because it reduces the memory required to run the code, which leads to faster
execution
A common optimisation action is removing
duplicate
code
If an
'add'
function is written twice in the
source
code, a sophisticated compiler will notice this and include it only once in the
object
code