2.2.6
Compiling a Program
Compiling a Program
Compiling a Program
You need to know the process a piece of code undergoes when it is compiled by a program.


Compiling a program
Compiling a program
- When code is compiled through a compiler, it has to go through 4 stages before it is ready to run.


Stages of compilation
Stages of compilation
- Those stages are:
- Lexical analysis.
- Syntax analysis.
- Code generation.
- Code optimisation.
Lexical Analysis
Lexical Analysis
Lexical analysis is the first stage a compiled language goes through to compile code.


White space
White space
- Firstly, white space is removed from the code.
- This is the white space that is present to help humans read the code.
- The compiler doesn’t need the white space so it is removed.


Tokens
Tokens
- The compiler will then look through the code and create tokens out of the keywords and operators it finds (lexemes).
- A token is a string that is recognised by the computer.
- For example, the number 8 might be assigned the token 'number:8'.
- Any comments are ignored by the compiler.


Symbol table
Symbol table
- These tokens are assembled into a symbol table ready for the next step of the compiling process.
Syntax Analysis, Code Generation & Optimisation
Syntax Analysis, Code Generation & Optimisation
When code is compiled through a compiler, it has to go through 4 stages before it is ready to run.


Step 2: syntax analysis
Step 2: syntax analysis
- During syntax analysis, the tokenised code from the lexical analysis is compared to the rules of the programming language.
- If a section or part of the code is found to not fit with these rules, an error is generated with details of what and where it is.
- It will continue to perform syntax analysis of all of the code and generate more errors if found.
- A full error report will be given back to the user of all errors found.
- This differs to interpreted languages as they return one error at a time.


Step 3: code generation
Step 3: code generation
- Once the code has successfully passed the first two stages, it’s time to generate the code.
- During this stage object or machine code is generated.
- This is what the CPU actually works with to make your program run.


Step 4: optimisation
Step 4: optimisation
- After your code has been generated into machine or object code it will be optimised for efficiency.
- The purpose is to make the binary footprint as small as possible and will remove procedures and variables that are written into the code but never used.
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction
Jump to other topics
1Components of a Computer
1.1Structure & Function of the Processor
1.2Types of Processors
2Software & Software Development
2.1Systems Software
2.2Applications Generation
2.3Software Development
3Exchanging Data
3.1Compression, Encryption & Hashing
3.3Networks
4Data Types, Data Structures & Algorithms
4.1Data Types
5Legal, Moral, Cultural & Ethical Issues
5.1Computing Related Legislation
6Elements of Computational Thinking
6.1Thinking Abstractly
6.2Thinking Procedurally
6.3Thinking Logically
7Problem Solving & Programming
7.1Programming Techniques
7.2Programming Construction

Unlock your full potential with GoStudent tutoring
Affordable 1:1 tutoring from the comfort of your home
Tutors are matched to your specific learning needs
30+ school subjects covered