2.2.6

Compiling a Program

Test yourself

Compiling a Program

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

Illustrative background for Compiling a programIllustrative background for Compiling a program ?? "content

Compiling a program

  • When code is compiled through a compiler, it has to go through 4 stages before it is ready to run.
Illustrative background for Stages of compilationIllustrative background for Stages of compilation ?? "content

Stages of compilation

  • Those stages are:
    1. Lexical analysis.
    2. Syntax analysis.
    3. Code generation.
    4. Code optimisation.

Lexical Analysis

Lexical analysis is the first stage a compiled language goes through to compile code.

Illustrative background for White spaceIllustrative background for White space ?? "content

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.
Illustrative background for TokensIllustrative background for Tokens ?? "content

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.
Illustrative background for Symbol tableIllustrative background for Symbol table ?? "content

Symbol table

  • These tokens are assembled into a symbol table ready for the next step of the compiling process.

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.

Illustrative background for Step 2: syntax analysisIllustrative background for Step 2: syntax analysis ?? "content

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.
Illustrative background for Step 3: code generationIllustrative background for Step 3: code generation ?? "content

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.
Illustrative background for Step 4: optimisationIllustrative background for Step 4: optimisation ?? "content

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.

Jump to other topics

1Components of a Computer

2Software & Software Development

3Exchanging Data

4Data Types, Data Structures & Algorithms

5Legal, Moral, Cultural & Ethical Issues

6Elements of Computational Thinking

6.1Thinking Abstractly

6.2Thinking Procedurally

6.3Thinking Logically

7Problem Solving & Programming

8Algorithms

Go student ad image

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

Book a free trial lesson