What are Compilers and Translators

A translator is a program that takes as input a program written in one programming language (called source language) and produces as output a program in another language (called object or target language).

if the source language is a high-level language (e.g. FORTRAN, COBOL) and the object language is a low-level language (e.g. assembly language, machine language), then such a translator is called a compiler.

Compilation and Execution

Executing a program written in a high-level programming language is basically a two-step process.

  1. The source program must first be compiled, i.e. translated into an object program.
  2. Then the resulting object program is loaded into memory and executed.

Interpreter:

There are other translators too, which transform a programming language into a simplified language called intermediate code, which can be directly executed using a program called an interpreter. Interpreters are often smaller than compilers. The main disadvantage of interpreters is the execution time of an interpreted program is usually slower than that of a corresponding compiled object program.

Assembler:

If the source language is assembly language and the target language is machine language, then the translator is called an assembler.

Preprocessor:

Translators that take programs in one high-level language and translate into equivalent programs in another high-level language are called preprocessors.

Compiler Vs Interpreter

  • Both are written in some high-level programming language and they are translated into machine code.
  • The interpreter source program is machine-independent as it does not generate machine code.
  • The interpreter is slower than the compiler because it processes and interprets each statement in the program as many times get executed.

Need of Translators

  • With machine language, we have to communicate directly in terms of bits, registers, and very primitive machine operations.
  • Machine language is nothing more than a sequence of 0’s and 1’s.
  • Programming an algorithm in such a language is a tedious task and there may be opportunities for mistakes.
  • Another disadvantage of machine language coding is that all operations and operands must be specified in a numeric code.

Symbolic Assembly Language

Because of difficulties with machine language programming, higher-level language has been invented to enable the programmer to code in a way that looks like his own thought processes. The most immediate step away from machine language is symbolic
assembly language.

In this language, the programmer uses mnemonic names for both operation codes and data addresses. To add values of X and Y, in assembly language programmer could write ADD X, Y that may be 0110 001110 010101 in machine language where 0110 is machine operation code for “add” and 001110 and 010101 are the address of X and Y resp. The computer cannot execute a program written in assembly language. The program has to be first translated to machine language, which the computer can understand. This task is performed by an assembler.

Macros

Many programming languages provide a macro facility where a macro statement will translate into a sequence of assembly language statements and other statements before being translated into machine code.

Two aspects of the macro: definition and use

Consider a situation if the machine does not have ADD X, Y as a single statement (add the contents of one memory address to another) Suppose the machine has an instruction.

• LOAD (moves datum from memory to a register)
• ADD (adds the contents of the memory address to that of register)
• STORE (moves data from a register to memory)

Using these instructions, we can create a macro as

MACRO ADD2 X, Y
LOAD Y
ADD X
STORE Y
END MACRO

The first statement gives the name ADD2 to the macro with X and Y as dummy arguments (formal parameters)

  • If ADD2 A, B encounters somewhere after the definition of ADD2, then we have a macro use, with A and B as actual parameters.
  • The macro processor substitute ADD2 A, B the three statements with actual parameters A and B for X and Y respectively.

Hence Add 2 A, B is translated to
LOAD B
ADD A
STORE B

Drawback

  • Programmers must know the details of how a specific computer operates.
  • The complex task to sequence low-level operation which includes only primitive data types that machine language provides.
  • Difficult to concern with how and where data is represented within the machine.
  • Detailed knowledge is required for efficiency (which leads to wasting time).

High-Level Languages

  • To avoid problems with assembly language, high-level programming languages are developed.
  • Allows programmers to write the expression in a natural way like A + B instead of ADD A, B.
  • We need a program to translate high-level language into a language the machine can understand.
  • Some compilers make use of assembler as an appendage.
  • Compiler producing assembly code is assembled and loaded before being. executed in machine language form.
  • COBOL, FORTRAN, PASCAL, LISP, and C are some high-level languages.

Leave a Reply