Computer ScienceNEB 2076 (old course)

Explain the compiler and interpreter.

5

Answer

Source Code (.c/.py)Compiler/InterpreterObject Code (.exe/.pyd)LoaderExecutionfrom source to executable
Compiler vs. Interpreter: Program Translation Process (C vs. Python)

Compiler

A compiler is a special software program that converts the entire source code written in a high-level programming language (e.g., C, C++, Java) into machine code (object code) all at once before execution. The compiled program can then be run multiple times without recompilation.

Key Features:

  • Translation Phase: The compiler performs several passes (lexical analysis, syntax analysis, semantic analysis, optimization, and code generation) to produce an executable file (e.g., .exe).
  • Portability: The compiled code is machine-specific and must be recompiled for different platforms.
  • Efficiency: The compiled program runs faster because it is already in machine code.
  • Error Reporting: Compilers report all errors at once after the entire translation process.

Example:

When you compile a C program using gcc, the compiler generates an executable file (e.g., a.out), which can be run directly.

08162431Opcode8 bitsOperand24 bits
Example Python bytecode (LOAD_CONST, CALL_FUNCTION)

Interpreter

An interpreter is another type of translator that executes the source code line by line without generating a separate machine code file. It translates and executes each statement immediately.

Key Features:

  • Translation Phase: The interpreter reads one statement, translates it into machine code, executes it, and then moves to the next statement.
  • Portability: Interpreted programs (e.g., Python, JavaScript) are platform-independent as long as the interpreter is available.
  • Efficiency: Slower execution because translation happens at runtime.
  • Error Reporting: Errors are reported as soon as they are encountered during execution.

Example:

When you run a Python script using the Python interpreter, it executes each line sequentially without creating a separate executable file.


Comparison Table

Feature Compiler Interpreter
Translation Time Entire program at once Line by line (runtime)
Execution Speed Faster (pre-compiled) Slower (translation at runtime)
Error Reporting All errors at once Errors reported during execution
Portability Machine-specific (needs recompile) Platform-independent (with interpreter)
Memory Usage Generates standalone executable Requires interpreter to run
Examples C, C++, Rust Python, JavaScript, PHP

Practical Example

Compiler Workflow (C Program):

#include <stdio.h>
int main() {
    printf("Hello, World!");
    return 0;
}
  1. Source Code: hello.c
  2. Compile: gcc hello.c -o hello
  3. Output: Executable file hello (runs directly).

Interpreter Workflow (Python Program):

print("Hello, World!")
  1. Source Code: hello.py
  2. Run: python hello.py
  3. Output: Executes line by line without generating an executable.

Compiler (C)1. Fulltranslation → 2. ExecuInterpreter (Python)1. Line-by-linetranslation → 2. Immed
Execution Flow: Compiler (C) vs. Interpreter (Python)

Discussion

Loading…

More Computer Science questions

All Computer Science old questions