0984

Programming Languages and Translators

Software · 4 question types

Exam Frequency Analysis

Past paper frequency (2018 to 2024)

This topic accounts for approximately 4% of your exam marks.

stable
Rare
Stable4%

Compiler vs interpreter differences are a recurring 3-mark question.

Every programming language sitting above raw machine code must first be converted into machine code before the CPU can execute it. A translator is the piece of software that performs this conversion. Three kinds appear in the syllabus:

TranslatorTranslatesUsed for
AssemblerAssembly language → machine codeLow-level (2nd generation) source code
CompilerHigh-level language → machine code, all in one goPrograms that are finished and ready to distribute
InterpreterHigh-level language → machine code, line by line, running each line straight after translationPrograms that are being developed or run interactively

Assembler

The simplest of the three. An assembler takes an assembly-language source file and produces an equivalent machine-code file. Because the languages are one-to-one, the job comes down to a lookup table: each mnemonic is replaced with its binary equivalent.

For example, the assembler sees LDA 5, looks LDA up in its table, finds the binary opcode 10000001, and produces the machine-code bytes 10000001 00000101.

Compiler

A reads the whole high-level program once, translates the entire thing into machine code, and produces a standalone executable file that can be run later without the compiler being present.

How a compiler is used:

  1. The programmer writes a source file (e.g. program.cpp).
  2. The compiler reads the file and translates it into machine code.
  3. The compiler produces a separate executable file (e.g. program.exe).
  4. The executable can then be distributed and run on any compatible machine.
  5. If the source is changed, the compiler must be run again before the new version can be tested.

Interpreter

An works through the high-level source line by line. After translating a line, it executes that line straight away before moving on to the next.

How an interpreter is used:

  1. The programmer writes a source file (e.g. program.py).
  2. The interpreter starts at the first line, translates it and runs it.
  3. It moves on to the second line and does the same.
  4. If a line contains an error, execution stops at that line, before the rest of the program is processed.
  5. The interpreter has to be installed on any machine that runs the program, since the source is never bundled into a standalone executable.

Side-by-side

FeatureCompilerInterpreter
Translates...The whole program in one goOne line at a time
Produces an executable?Yes, a separate fileNo
Runs the program?No; the executable is run laterYes; translates and runs in one step
Speed of executionFast (already in machine code)Slower (translates as it runs, every time)
Reports errors...At the end of compilation (or at the first one found that stops compilation)Stops at the line where the error is found
Easier to debug?Harder (must recompile after every change)Easier (run, see error, fix, run again immediately)
DistributionEasy; share the executableHarder; the recipient also needs the interpreter
Source code visible to user?No (only the executable is shared)Yes (the source must be shared with the interpreter)
Typical useReleased, polished softwareActive development and scripting

Which to use when

  • During development, an interpreter is convenient because each change can be tried immediately.
  • For distribution, a compiler is usually preferred because the program runs faster and the source stays private.
  • Some languages (Java, C#) use both: a compiler turns the source into a low-level intermediate code (bytecode), and an interpreter runs the bytecode on each user's machine.
Exam tip

How a compiler works

Describing how a compiler translates (and reports errors) comes up often, so you need to know it translates the whole program at once into machine code, produces a standalone executable, and reports all errors together at the end — unlike an interpreter, it does not work line by line.

Exam tip

Identifying which translator is which

What comes up: a description ("converts and executes one line at a time", "creates an executable file") asks you to name the translator.

Watch out: match by the one defining property — interpreter = translates/executes one line at a time (stops at the first error); compiler = translates the whole program and produces an executable (all errors reported together); assembler = assembly language → machine code.