ITKokka
Lesson 5 · Note 5

Language Translators

📌 The Short Note

Download

📖 Explanation

Language Translators

Computers execute instructions using machine code, which is represented in binary form. Programmers often write programs using assembly language or high-level programming languages because these are easier for humans to read and write. Language translators convert source code into a form the computer can execute.

The three language translators covered in this note are assemblers, compilers, and interpreters. They differ in the type of source code they process and how they translate or execute it.

1. What Is a Language Translator?

A language translator is system software that converts a program written in one programming language into another form, often machine code or another executable representation that a computer can run.

The source program is the code written by the programmer. The translated result may be machine code, an object file, bytecode, or another form, depending on the language and tool.

The exact process depends on the type of translator and the programming language being used.

2. Assembler

An assembler translates a program written in assembly language into machine code. Assembly language uses short symbolic instructions, such as MOV, ADD, and HLT, that correspond to operations supported by a particular processor or instruction set.

How an assembler works

  1. The programmer writes instructions in assembly language.
  2. The assembler checks the instructions and translates them into machine-code instructions.
  3. The resulting machine code can be used by the system to run the program. In many development workflows, the assembler produces an object file that is then linked with other files or libraries.

Assembly language is closely related to the processor's instruction set, so it is generally processor-specific. Assembly programs can offer detailed control over hardware operations, but they are usually more difficult to write and maintain than programs in high-level languages.

Example: Assembly instruction
ADD R1, R2

This represents an assembly-language instruction to add values associated with registers R1 and R2, according to the processor's instruction set. The assembler converts the instruction into its corresponding machine-code representation.

Common uses

Assemblers are used in contexts such as:

  • Embedded systems
  • Low-level programming
  • Some device drivers and hardware-related software
  • Situations requiring direct use of processor instructions

3. Compiler

A compiler translates source code from a programming language into another form before the resulting program is run. In a traditional compiled workflow, it translates the source program into machine code or an intermediate/object representation, and the build process may then link the result to create an executable program.

How a compiler works

  1. The programmer writes source code.
  2. The compiler analyses the code and reports errors it detects.
  3. It translates the source into a target representation.
  4. Other build steps, such as linking, may combine the compiled code with required libraries.
  5. The resulting program can be executed in the target environment.

A compiler commonly analyses the program as a whole rather than interpreting each source statement directly as the program runs. However, the exact stages and output depend on the language and compiler.

Example: C program
#include <stdio.h>

int main(void) {
    printf("Hello");
    return 0;
}

A C compiler translates this source code into a target representation. In a typical native build, the compiler and linker produce an executable program that can display Hello when run.

Characteristics of compilers

  • Translation generally happens before the program is executed.
  • Compilation can report syntax and other errors detected during translation.
  • A compiled program may run without needing the original source code or compiler at runtime, although it still depends on the target platform and any required runtime libraries.
  • The translated output and performance depend on the language, compiler, options, and execution environment.

Examples and uses

Compiled languages and workflows include C and C++. Java is commonly compiled into bytecode, which is then run by the Java Virtual Machine. Python can also be compiled into bytecode internally, so it is not accurate to classify every language as using only one translation method.

Compilers are widely used to build large applications and programs where translation, optimisation, and repeatable deployment are important.

4. Interpreter

An interpreter is a program that executes source code, or an intermediate representation of it, by carrying out the instructions through an interpreter or runtime environment rather than necessarily producing a separate native executable in advance.

How an interpreter works

  1. The program is provided to the interpreter.
  2. The interpreter reads or processes the program.
  3. It carries out the program's operations.
  4. If an error prevents execution from continuing, the interpreter reports the error.

Some interpreters process source statements incrementally, while others use parsing, intermediate representations, or bytecode. Therefore, “line by line” is a useful introductory simplification, not a rule that describes every interpreter implementation.

Example: Python program
print("Hello")
print("World")

When run in a Python environment, the program displays:

Hello
World

If an error occurs, the interpreter or runtime reports it. Depending on the error and program structure, execution may stop before later instructions are carried out.

Characteristics of interpreters

  • They execute code through an interpreter or runtime environment.
  • They commonly make it convenient to test and debug code interactively.
  • They do not necessarily create a separate native executable file.
  • Their performance and error-handling behaviour depend on the language implementation and runtime.

Examples

Python and JavaScript are commonly run through language runtimes. PHP is also commonly executed by a runtime in web-server environments. These languages may use internal compilation or bytecode techniques as part of execution.

5. Assembler vs Compiler vs Interpreter

FeatureAssemblerCompilerInterpreter
Main inputAssembly languageSource code in a programming languageSource code or an intermediate representation
Main roleConverts assembly instructions into machine codeTranslates source code into a target representation before execution in a typical compiled workflowExecutes code through an interpreter or runtime
Typical outputMachine code or an object fileMachine code, object code, bytecode, or another target formUsually executes through the runtime; a separate native executable is not necessarily produced
Error handlingReports invalid assembly instructions and related errorsReports errors detected during compilationReports errors encountered during processing or execution
Example contextProcessor-specific low-level programmingNative C/C++ builds; Java bytecode compilationPython or JavaScript runtime execution

Key Idea: An assembler translates assembly language, a compiler translates source code before a typical execution stage, and an interpreter executes code through a runtime. Real language implementations may combine compilation and interpretation techniques.

6. Why Language Translators Are Needed

Computers' processors execute machine instructions. Translators allow programmers to use more readable programming languages while still enabling programs to run on a computer.

Language translators can:

  • Convert symbolic or high-level instructions into a form the computer can execute.
  • Detect and report certain errors.
  • Support different processor architectures and execution environments.
  • Help organise the process of building, testing, and running software.

A translator cannot guarantee that a program is logically correct. A program may translate successfully and still produce incorrect results because of errors in its design or logic.

7. Exam-Focused Points

  1. Assembler: Converts assembly-language instructions into machine code.
  2. Compiler: Translates source code into a target form before execution in a typical compiled workflow.
  3. Interpreter: Executes code through an interpreter or runtime environment.

When comparing the three, focus on the input language, translation or execution method, and result produced. Avoid assuming that every programming language uses only one technique; some languages combine compilation and runtime interpretation.

Summary

Language translators help convert programs written by people into forms that computers can execute. An assembler works with assembly language, a compiler translates source code into a target representation, and an interpreter executes code through a runtime. Knowing their roles and differences helps explain how programs move from source code to execution.

Share:
← Utility SoftwareApplication Software →