You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
This lesson covers the three types of translators used to convert programming languages into machine code. This is part of OCR J277 Section 1.5.3.
CPUs can only execute machine code (binary instructions). However, most programmers write code in high-level or assembly languages. A translator is a program that converts source code into machine code so the CPU can execute it.
There are three types of translator:
The following diagram compares how each translator converts source code:
graph LR
SC["High-level\nSource Code"] --> COMP["Compiler"]
SC --> INT["Interpreter"]
ASM["Assembly\nCode"] --> ASMR["Assembler"]
COMP --> EXE["Executable\n(Machine Code)"]
INT --> RUN["Executes line\nby line"]
ASMR --> MC["Machine Code"]
A compiler translates the entire source code of a high-level language program into machine code all at once, before the program is run.
graph LR
A["Source code (e.g. Python, C++)"] --> B["COMPILER"] --> C["Executable machine code file"]
| Feature | Detail |
|---|---|
| Translation | Translates the entire program at once |
| Output | Produces a standalone executable file |
| Error reporting | Reports all errors after compilation; program won't compile until all errors are fixed |
| Execution speed | Fast — the executable is already in machine code |
| Distribution | The executable can be distributed without sharing the source code |
| Re-compilation | Any change to the source code requires the entire program to be re-compiled |
An interpreter translates and executes high-level source code one line at a time, as the program runs.
graph LR
A["Source code"] --> B["INTERPRETER"] --> C["Translates and executes line by line"]
| Feature | Detail |
|---|---|
| Translation | Translates and executes one line at a time |
| Output | No standalone executable is produced |
| Error reporting | Stops at the first error found, making debugging easier |
| Execution speed | Slower — must re-translate the code every time the program runs |
| Distribution | Source code must be shared (and the interpreter must be available) |
| Development | Useful for testing and debugging during development |
An assembler translates assembly language (mnemonics) into machine code.
graph LR
A["Assembly code (e.g. MOV, ADD)"] --> B["ASSEMBLER"] --> C["Machine code (binary)"]
| Feature | Detail |
|---|---|
| Input | Assembly language (mnemonics) |
| Output | Machine code |
| Relationship | There is a one-to-one mapping between assembly instructions and machine code instructions |
| Use | Used for programs that need direct hardware access |
| Feature | Compiler | Interpreter |
|---|---|---|
| When does translation happen? | Before execution (all at once) | During execution (line by line) |
| Executable produced? | Yes — standalone executable file | No — must be re-translated each time |
| Error handling | All errors reported after compilation | Stops at first error |
| Execution speed | Fast (already translated) | Slow (re-translates every run) |
| Debugging | Harder — all errors shown at once | Easier — error shown on exact line |
| Source code distribution | Not needed (only executable shared) | Required (interpreter needed too) |
| Memory use | Executable uses less memory at runtime | Interpreter must be in memory during execution |
OCR Exam Tip: A very common exam question asks you to compare a compiler and an interpreter. Learn at least 3-4 differences from the table above, and be able to explain when each would be used.
| Translator | Best For |
|---|---|
| Compiler | Distributing finished software (faster execution, source code hidden) |
| Interpreter | Development and testing (easier debugging, immediate error feedback) |
| Assembler | Low-level programming for device drivers, embedded systems |
During development, a programmer might use an interpreter for quick testing:
# Testing with an interpreter — runs immediately, error on line 3
x = 10
y = 20
print(x + z) # NameError: 'z' is not defined — interpreter stops here
Once the program is complete and bug-free, a compiler is used to create an executable for distribution.
Key Vocabulary: compiler, interpreter, assembler, source code, executable, machine code, mnemonic, syntax error, one-to-one mapping.
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.