Learning Objectives
- Describe how a compiler translates high-level source code.
- Explain that a compiler translates the whole program before execution.
- Explain that successful compilation produces an executable or object-code file.
- Explain how a compiler reports errors.
- Evaluate advantages and disadvantages of compilation in a stated context.
Key Terms
- Compiler
- A translator that converts a complete high-level source program into machine or object code before execution.
- Compilation
- The process of translating a complete source program using a compiler.
- Source code
- The high-level program written by a programmer.
- Object code
- Machine-code output produced by translation, which may be combined into an executable.
- Executable file
- A translated program file that can be loaded and run without translating the source each time.
- Error report
- A list or set of messages identifying problems found during compilation.
- Syntax error
- A violation of the grammar rules of the programming language.
- Distribution
- Providing the program to users or systems that will run it.
- Target platform
- The processor and operating environment for which code is generated.

How A Compiler Operates
A compiler translates a complete high-level source program before the program is executed. It analyses the whole source code, checks it against language rules and generates machine or object code for a target platform.
The compiler does not normally execute each source statement as soon as it is read. Translation is a separate stage. If compilation succeeds, the output can be stored as an executable or used to build an executable file.
Because the output is machine code for a target system, the executable can later be loaded and run without repeating translation of every source statement. The source program may still need to be recompiled if it is changed or if a different target platform is required.
Error Reporting
A compiler examines the whole source program and can produce an error report containing problems detected throughout the code. Syntax errors and some other translation-time errors are reported so the programmer can correct the source.
If errors prevent valid translation, a usable executable is not produced. The programmer edits the source and compiles again. This compile-correct-compile cycle continues until the program is translated successfully.
A compiler cannot guarantee that the algorithm is correct. A program can compile successfully but still contain logic errors that produce the wrong result. Compilation checks whether code can be translated according to language rules, not whether it solves the intended problem.
Advantages Of A Compiler
After compilation, the executable normally runs without translating source statements one by one. This can make repeated execution efficient. It is useful when a completed program will be run many times or distributed to many users.
The source code does not have to be provided with the executable, which can make casual copying or reading of the original source more difficult. The syllabus focus is not software licensing, so this point should be used only when relevant to a question.
The compiler reports errors found across the whole program in one compilation attempt. A programmer can review several errors together rather than stopping at the first error encountered during execution.
Disadvantages And Suitable Use
Compilation takes time before the program can be executed. After every source change, the programmer may need to compile again. This can make rapid testing of small changes less immediate than line-by-line interpretation.
Compiler error reports may contain several messages, and one mistake can cause additional reported errors. The programmer must understand the messages and identify the original source problem.
A compiler is commonly used to translate the final version of a program. This matches the syllabus guidance: an interpreter is mostly used during development, while a compiler is used to translate the final program. The choice depends on whether rapid testing or repeated efficient execution is more important.
Compilation Process
| Stage | Action |
|---|---|
| 1. Source written | Programmer creates high-level source code. |
| 2. Whole program analysed | Compiler checks and translates the complete source. |
| 3. Errors reported | Problems found in the code are listed for correction. |
| 4. Output generated | If successful, object or executable code is produced. |
| 5. Program executed | The translated program is loaded and run without source translation each time. |
Compiler Advantages And Disadvantages
| Advantages | Disadvantages |
|---|---|
| Executable can be run repeatedly without translating each source line. | Compilation must occur before the changed program can be tested. |
| Suitable for distribution of a final program. | Output is generated for a particular target platform. |
| Can report errors found throughout the whole source program. | A long error report can be difficult to interpret. |
| Source code need not be supplied to run the executable. | Program must be recompiled after source changes. |
Worked Examples
Correcting A Compiled Program
Question: A programmer changes one statement in a compiled application. Explain what must happen before the changed version can be run.
- The source code has changed, so the existing executable still represents the old source.
- The compiler must analyse and translate the revised whole program.
- Any reported errors must be corrected.
- A new executable or object-code output is generated and then run.
Answer: The revised source must be compiled again before an executable containing the change can be tested.
Selecting Compilation For A Final Product
Question: A company has completed a program that will be run thousands of times by users. Explain one reason to compile it.
- The program is no longer changing frequently.
- The compiler creates an executable before distribution.
- Users can run the executable repeatedly without line-by-line translation.
Answer: Compilation is suitable because the final executable can be distributed and run repeatedly without translating the source each time.
Examination Guidance
- State that a compiler translates the whole program before execution.
- Mention the production of object or executable code when describing compiler operation.
- Explain that the compiler reports errors across the whole source program.
- Do not claim that successful compilation proves the program gives correct results.
- For evaluation questions, relate compilation to the development or final-use context.
Common Mistakes
- Saying a compiler translates and executes one line at a time.
- Claiming the executable is automatically machine independent.
- Saying the compiler finds every logic error.
- Forgetting that changed source code must be recompiled.
- Confusing source code with the executable file.
Knowledge Check
1. What does a compiler translate?
2. When does compilation occur relative to execution?
3. What is produced after successful compilation?
4. How are errors reported by a compiler?
5. Why is a compiler suitable for a final program?