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 languages | High-level languages |
|---|---|
| machine code and assembly | Python, Java, C# and many more |
| close to the hardware | close to human language and maths |
| one instruction per CPU operation | one statement may become many instructions |
| processor-specific | portable between processors |
| fast and compact | easier and quicker to write, read and debug |
Check yourself
Which is an advantage of high-level languages?
Show the answer
They are easier to write and debug and can run on different processors
Human-friendly.
2Assemblers, compilers and interpreters
| Translator | Translates | When | Output |
|---|---|---|---|
| assembler | assembly → machine code | all at once | object code |
| compiler | whole high-level program → machine code | before running | an executable file |
| interpreter | high-level code, one statement at a time | while running | no separate file |
| Compiler | Interpreter |
|---|---|
| the executable runs fast | slower: each line is translated every time it runs |
| source code is not needed to run, so it can be kept secret | the source code and interpreter are needed to run it |
| errors reported after compiling the whole program | stops at the first error, making debugging easier |
| must recompile after every change | changes can be tested at once |
Check yourself
Why might a developer use an interpreter while writing a 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.
- 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.
- Syntax analysis: the tokens are checked against the language's grammar rules, building an abstract syntax tree. Syntax errors are reported.
- Code generation: machine code (or intermediate code) is produced from the tree.
- Optimisation: the code is improved so it runs faster or uses less memory, without changing what it does.
Lexical analysis of total = total + 5
- Tokens: identifier(total), operator(=), identifier(total), operator(+), literal(5).
- 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?
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?
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.
| Advantages | Disadvantages |
|---|---|
| the same bytecode runs on any platform with a virtual machine | slower than native machine code |
| the source code stays private | a 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?
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?
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