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
- The programmer writes instructions in assembly language.
- The assembler checks the instructions and translates them into machine-code instructions.
- 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 instructionADD R1, R2This 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
- The programmer writes source code.
- The compiler analyses the code and reports errors it detects.
- It translates the source into a target representation.
- Other build steps, such as linking, may combine the compiled code with required libraries.
- 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
- The program is provided to the interpreter.
- The interpreter reads or processes the program.
- It carries out the program's operations.
- 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 programprint("Hello")
print("World")When run in a Python environment, the program displays:
Hello
WorldIf 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
| Feature | Assembler | Compiler | Interpreter |
|---|---|---|---|
| Main input | Assembly language | Source code in a programming language | Source code or an intermediate representation |
| Main role | Converts assembly instructions into machine code | Translates source code into a target representation before execution in a typical compiled workflow | Executes code through an interpreter or runtime |
| Typical output | Machine code or an object file | Machine code, object code, bytecode, or another target form | Usually executes through the runtime; a separate native executable is not necessarily produced |
| Error handling | Reports invalid assembly instructions and related errors | Reports errors detected during compilation | Reports errors encountered during processing or execution |
| Example context | Processor-specific low-level programming | Native C/C++ builds; Java bytecode compilation | Python 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
- Assembler: Converts assembly-language instructions into machine code.
- Compiler: Translates source code into a target form before execution in a typical compiled workflow.
- 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.