Objectives

Students will be able to:

  • show understanding of the need for both high-level and low-level languages
  • show understanding of the need for compilers when translating programs written in a high-level language
  • show understanding of the use of interpreters with high-level language programs
  • show understanding of the need for assemblers when translating programs written in assembly language

languages and their translators

Translators

Most of us work with High Level programming languages when writing code such as:

  • Python
  • Java
  • C#

The CPU cannot directly execute the high level source code - it needs to be translated into machine code.

For example, one kind of translator is a compiler to compile the code into an executable file:

 

Note: the machine-code executable can only be created once all of the errors in the code have been fixed.

Advantages/Disadvantages of a compiler

  • + the translation process only has to happen once, then the executable file can always be used to run the program
  • + very fast execution because the executable file contains error-free machine code ie no further translation is required
  • + the compiler optimises the code during compilation
  • - compilation process may take time
  • - a different executable file may be required for different operating systems

Because the executable file does not need to be re-translated and runs extremely quickly, compiled code is most often used in performance critical scenarios, such as systems requiring real-time processing (eg edge devices), video games, scientific simulations...

Interpreters

An interpreter is another kind of translator.

It translates each line of source code (eg C++) into machine code and executes it. It then moves on to the next line of source code.

So, an interpreter does not produce a single executable file which can run on demand very quickly.

It simply translates and executes source code line by line. Hence errors are detected during runtime, not during compilation.

In systems where compilation takes a long time, finding errors can be time-consuming.

Using an interpreter means that errors can be found quickly and fixed. This is perfect for Rapid Development and Testing scenarios.

This means the translation process occurs everytime the source code needs to be executed - it is definitely a slower process than using a compiled executable file!

Bytecode Interpreters

The issue with compilers is that the compiled executable file will be specific to a certain OS eg Windows or Mac.

So, multiple executables will have to be developed.

By using a Bytecode Interpreter, an executable file containing bytecode is created.

This file can be executed by a Virtual Machine on any operating system.

So, as long as an appropriate Virtual Machine is installed on a system, the same bytecode file can be executed on different systems.

Hence bytecode interpreted programs are incredibly portable.

Advantages/Disdvantages of Bytecode Interpreters

  • + portable executable files - write once, run anywhere (as long as a Virtual Machine is available)
  • + source code is compiled into bytecode and so should be error free
  • - bytecode still needs to be compiled to machine code by the VM, so slower than purely compiled code
  • - a VM is required
  • - the VM itself will require extra memory and other additional resources

If a company runs different operating systems and requires the same program to run, bytecode interpreted files are a good choice.

Assemblers

A note on assemblers (not in our syllabus).

Assemblers translate Assembly Language into Machine Code.

Task

Read through the information above again. Create a graphic which clearly shows the advantages and disadvantages of each type of translator.