ICT|程式翻譯器 Program Translators

Source code

Compiler

  • Translate the source code of the whole program at a time
  • Producing object code stored in hard disk

Object code

[in hard disk]

Linker

  • The Linker links the object files and libraries together to form an executable file

Executable code

[in hard disk]

Loader





What is the function of loader?

  • Loader is used to load the executable file into the main memory

Features of loader:

  • Loader is a part of the operating system
  • It is loaded when the computer is booted and it resides in memory

Executable code

[in main memory]

Translators

What is the function of translator?

  • Translators are used to convert source codes into object codes.
  • Only the code format will be changed, the purpose and function will not be affected.
  • There are three types of translators:
  • Assemblers: it is used to translate assembly language to machine language. (i.e., it is designed for low level language)
  • Interpreters / Compilers: They are used by high level languages, such as C++ and Python etc.

Interpreters [DSE 2018 P2D Q2(e)]

  • It is not necessary to compile the source code in advance
  • An interpreter will translate a source code into immediate instruction / efficient intermediate representation and immediately execute it
  • If there is any syntax error during the execution
    → the program may stop executing (e.g., Python)

Compliers

  • Translates the source code of the whole program to object code at a time

Interpreters and Compilers

What are the differences between an interpreter and a compiler?

Interpreter

Compiler

Mode of translation

  • Translates program one statement at a time
    → Translating a source code into immediate instructions and immediately executing it
    → Not necessary to compile the source code in advance
  • Scans the entire program and translates it as a whole into machine codes
    Translation can be done once only (especially for some iterations)

Execution time

  • Interpreters usually take less time to analyze the source code
  • However, the execution code is less optimized
    → The overall execution time is comparatively slower than compilers
  • Compilers usually take more time to analyze the source code
  • However, the execution code can be optimized better during compilation stage
    The execution time of the program is shorter

Storage space

  • No object code is generated
    more memory efficient (need more storage space)
  • Generates object code, hence requires more memory

Advantages of interpreters and compilers

[Compiler: DSE 2017 P2D Q2(e), DSE 2021 P2D Q3(d), DSE 2022 P2D Q2(e)(ii)]
[Interpreter: DSE 2021 P2D Q3(d)]

Interpreters

Compilers

Advantages

  • More memory efficient (save storage space) as no intermediate object code is generated
  • Convenient for programmers to test and debug during the development of the program
    [As interpreters execute codes line-by-line, programmers can modify and test their code in real-time, without having to recompile the whole program]
  • No translation is needed during the execution of the program
  • Compiled code runs faster / execution time is shorter
  • Optimization can be done during the compilation stage

封面圖:Image by Freepik