3.3.2 - Compilers and interpreters
The same high-level source code can take two very different routes before a processor can follow it. You will learn how compilation and interpretation work, compare their consequences, and justify which approach fits a given situation.
The translation job
A programmer writes source code in a high-level programming language. A processor executes machine code, so the high-level source must be translated into machine-readable instructions before or during execution.
Translator
Software that converts program code from one language or representation into another. In this lesson, the two translators are a compiler and an interpreter.
The shared starting point is:
high-level source code -> translator -> machine-readable instructions -> execution
The key difference is not whether translation happens: both approaches translate high-level code. The difference is how much is translated at once, when translation happens in relation to execution, and whether a separate executable file is produced.
For example, imagine a program containing instructions to read two prices, add them and display the total. A processor cannot directly follow the human-readable source statements. A compiler can translate the complete program before it is run, whereas an interpreter can translate and carry out one statement at a time as the run proceeds.
Compilation
Compiler
A translator that translates the whole high-level source program in one go before the resulting program is executed.
The compilation route can be represented as an ordered process:
1. Complete high-level source program
2. Compiler translates the whole program in one go
3. Translation errors are reported after compilation
4. If compilation succeeds, an executable machine-code file is produced
5. The executable is run separately
The executable is built for a particular processor architecture or target platform. A suitable build can be run repeatedly without translating the source again and without needing the compiler or original source code for each run. If the source code is changed, however, it must be compiled again to produce an updated executable.
This leads to several consequences:
- The executable generally runs faster because translation has already taken place.
- The developer can distribute the executable without distributing the original source code.
- The developer receives translation-error information after the whole-program compilation process, rather than as each statement is executed.
- A different target architecture may need a different compiled build.
Do not confuse translation with execution. Translating the whole program in one go does not mean that every instruction executes simultaneously. Compilation creates the machine-code program; execution happens afterwards according to the program's control flow.
Worked example
A school creates a revision game for 180 laptops that use the same target architecture.
- The developer gives the compiler the complete source program.
- The compiler translates the whole program and reports any translation errors after the compilation attempt.
- Once the errors are corrected and compilation succeeds, it produces an executable for those laptops.
- The executable can be copied to the laptops and run many times without recompiling it each time.
- If the developer changes the scoring rules in the source, the program must be compiled again before the executable contains that change.
Interpretation
Interpreter
A translator that translates and executes the high-level source one statement at a time as the program runs.
The interpretation route repeats a short cycle:
translate current statement -> execute current statement -> move to next statement
If the interpreter reaches a statement it cannot translate, it reports the error and stops at that point. Valid earlier statements may already have executed, while later statements are not reached. The interpreter does not first leave behind the same kind of standalone machine-code executable as a compiler, so the source is processed again on another run and a compatible interpreter is required.
This also has consequences:
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account