Compilation Stages

Computer Science: OCR A Level, topic 2.2.15

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.

Test yourself on Compilation Stages

  • During which stage are errors found in the code?

Practise this topic for free