impera Open Impera

A levels Computer Science · Computer systems

Translators

1Why programs need translating

The processor runs only machine code. Programs written in assembly or a high-level language (such as Python, Java or C) must be translated first.

Low-level languagesHigh-level languages
machine code and assemblyPython, Java, C# and many more
close to the hardwareclose to human language and maths
one instruction per CPU operationone statement may become many instructions
processor-specificportable between processors
fast and compacteasier and quicker to write, read and debug
Check yourself

Which is an advantage of high-level languages?

  • They give direct control of the CPU's registers and memory

  • They are easier to write and debug and can run on different processors

  • They need no translation before they can be run

  • Each statement translates into exactly one machine instruction

Show the answer

They are easier to write and debug and can run on different processors

Human-friendly.

2Assemblers, compilers and interpreters

TranslatorTranslatesWhenOutput
assemblerassembly → machine codeall at onceobject code
compilerwhole high-level program → machine codebefore runningan executable file
interpreterhigh-level code, one statement at a timewhile runningno separate file
CompilerInterpreter
the executable runs fastslower: each line is translated every time it runs
source code is not needed to run, so it can be kept secretthe source code and interpreter are needed to run it
errors reported after compiling the whole programstops at the first error, making debugging easier
must recompile after every changechanges can be tested at once
Check yourself

Why might a developer use an interpreter while writing a program?

  • The finished program runs faster than a compiled version would

  • It produces an executable file that can be shared with users

  • It runs code immediately and stops at the line with an error, making testing easier

  • It hides the source code from anyone who uses the program

Show the answer

It runs code immediately and stops at the line with an error, making testing easier

Quick feedback.

3Stages of compilation

The stages of compilation. Each stage takes the output of the one before.

  1. Lexical analysis: whitespace and comments are removed, and the code is split into tokens (keywords, identifiers, operators, literals). Identifiers are added to the symbol table.
  2. Syntax analysis: the tokens are checked against the language's grammar rules, building an abstract syntax tree. Syntax errors are reported.
  3. Code generation: machine code (or intermediate code) is produced from the tree.
  4. Optimisation: the code is improved so it runs faster or uses less memory, without changing what it does.
Worked example

Lexical analysis of total = total + 5

  1. Tokens: identifier(total), operator(=), identifier(total), operator(+), literal(5).
  2. The symbol table records "total" and later its type and memory address.
Check yourself

At which stage is "pritn(x)" likely to be caught as an error in a compiled language?

  • Syntax or semantic analysis, as an undeclared or unknown name

  • It is never caught

  • Lexical analysis, because tokens cannot contain letters

  • Code generation

Show the answer

Syntax or semantic analysis, as an undeclared or unknown name

The tokens are fine; the meaning is not.

4Optimisation

Optimisation makes the generated code more efficient. Examples:

  • removing code that can never run (dead code);
  • working out constant expressions at compile time: x = 60 * 60 becomes x = 3600;
  • moving a calculation that gives the same result each time out of a loop;
  • keeping frequently used values in registers instead of memory.

Optimising takes longer to compile and can make debugging harder, because the machine code no longer matches the source line by line.

Check yourself

Which is an example of compile-time optimisation?

  • Renaming variables to longer names

  • Running the program twice

  • Replacing 24 * 7 with 168

  • Adding comments

Show the answer

Replacing 24 * 7 with 168

Constant folding.

5Intermediate code and bytecode

Some languages compile to intermediate code instead of machine code. Java compiles to bytecode, which is run by a virtual machine (the JVM) on each kind of computer.

AdvantagesDisadvantages
the same bytecode runs on any platform with a virtual machineslower than native machine code
the source code stays privatea virtual machine must be installed
the virtual machine can check the code for safety

Modern virtual machines use just-in-time (JIT) compilation: frequently used parts of the bytecode are compiled to machine code while the program runs, for speed.

Check yourself

Why does Java compile to bytecode?

  • Bytecode runs faster than machine code on every type of processor

  • It removes the need for a compiler or any other translator at all

  • So the same compiled program can run on any computer with a Java virtual machine

  • Bytecode can be edited easily by users to change the program

Show the answer

So the same compiled program can run on any computer with a Java virtual machine

Write once, run anywhere.

6Libraries, linkers and loaders

A library is a collection of ready-made, tested subroutines (for example, maths or graphics functions) that programs can use.

  • Using libraries saves time, and the code is already tested and efficient.
  • A linker combines the compiled program with the library code it uses into one executable.
  • Static linking copies the library code into the executable: bigger file, but it runs anywhere.
  • Dynamic linking keeps the library separate (for example a DLL) and links it when the program runs: smaller files, and several programs share one copy, but the library must be present and the right version.
  • A loader copies the program from storage into memory and gets it ready to run.
Check yourself

What does a linker do?

  • Finds syntax errors in the source code before compiling

  • Translates assembly language into machine code instructions

  • Loads the finished program into RAM so it can be run

  • Combines compiled code with the library code it needs into one executable

Show the answer

Combines compiled code with the library code it needs into one executable

Joins the pieces.

Now test yourself

This lesson has a 8-question quiz and 5 exam-style questions with mark schemes in Impera. Free, with your progress saved.

Practise in Impera