The first stage is lexical analysis which takes the source code as an input and: • Removes whitespace and comments; • Converts remaining code into tokens, by breaking down the code by recognising reserved words, operators and variables; • Creates a symbol table which includes information on variables such as scope and data type. The second stage is syntax analysis which checks the code to ensure it obeys the rules of syntax. An abstract syntax tree (AST) is created and if a part of the code does not fit into the AST then a syntax error has occurred and the compiler generates a syntax error report to send to the user. The third stage is code generation where the code is translated into object code or machine code. The final stage is code optimisation where the compiler attempts to optimise both space and time efficiency. This is achieved by: • Removing unused variables and subroutines; • Searching for more efficient alternatives to sets of instructions.