Compilers

The process of compilation can be split into five stages :

  1. Lexical Analysis
  2. Syntax Analysis
  3. Semantic Analysis
  4. Code Generation.
  5. Optimisation

Lexical Analysis

  • Superfluous characters and spaces are removed.
  • All comments are removed.
  • All keywords, constants and identifiers used in the source code are replaced by 'tokens'.
  • A symbol table is created which holds the addresses of variables, labels and subroutines.

Syntax Analysis

This is the process of determining whether the sequence of input characters, symbols, items or tokens obey the rules of the syntax of the language.

Parsing is the task of systematically applying the rules to each statement in the program to determine whether it is valid.

The syntax analyser will report syntax errors if they occur.

 

Semantic analysis is the process of checking the meaning of a program. The compiler decides how the program is to be executed. Eg - In what order to evaluate an arithmetic expression. 

  • checks are made to see if all variables used in the program have been declared.
  • type checks are carried out - Eg. check that real values have not been assigned to integer variables.
  • checks that appropriate operations have been carried out on variables - Eg that integer division has not been carried out on real variables. 

Code Generation.

The object code is generated. Most high level language statements will be translated into a number of machine code instructions.

A Link editor is a program that would make sure that previously compiled code sections from a library are loaded into the program at compile-time and required links created. The code sections become part of the executable object code.

A Linking loader would do a similar task when a program is loaded into memory. These library routines would also be loaded and links made to them.

 

Code optimisation techniques attempt to reduce the execution time of the object program by, for example, spotting redundant instructions and producing object code which achieves the same net effect as that specified by the source program but not by the same means.

Run-time support routines would also need to be incorporated. These would perform such tasks as run-time error reporting, array element accessing, managing a run-time stack or storage of dynamic data structures.