0984

Programming Languages and Translators

Software · 4 question types

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.
Common exam question

Naming a translator or describing how a compiler works

Question: Identify or name the translator from what it does or the language it translates, or describe how a compiler translates a program and reports errors (1–3 marks).

Asked in 3 of the 17 papers. To name the translator, match the clue: converting and executing line by line, stopping at an error, is the interpreter; producing an executable file, or one error report of every error before execution, is the compiler; an assembly-language program needs the assembler. If asked what the high-level code is translated into, write machine code (low-level language or object code also earns the mark).

Describing how a compiler translates earns three marks: it converts the high-level code into machine code, translates all of it before execution, and creates an executable file. Describing how it reports errors earns two: it produces an error report after the attempt to compile, listing every error, which must be corrected before the program can run. Line-by-line checking belongs to the interpreter.