Saturday, October 12, 2019

Compiler-Interpreter-Assembler

Translators

The program that converts source program into object program is called translator program. A program written in high-level language is called as source program and a program written in machine language is called as object program
A translator takes a program written in source language as input and converts it into a program in target language as output. It also detects and reports the error during translation by providing diagnostic messages wherever the programmer violates specification of the high-level language program.
The different types of translator are as follows:
  • 1. Assembler
  • 2. Compiler
  • 3. Interpreter

Computers only understand machine code (binary), this is an issue because programmers prefer to use a variety of high and low-level programming languages instead.

To get around the issue, the high-level and low-level program code (source code) needs to pass through a translator.

A translator will convert the source code into machine code (object code).
There are several types of translator programs, each able to perform different tasks.

Compiler in C

Compiler is a translator program (system software) which is used to convert programs in high-level language to low-level language (code into binary format in single steps). 
It translates the entire program and also reports the errors in source program encountered during the translation. 
In other words, Compiler is a system software which can take input from other any programming language and convert it into lower level machine dependent language.



How it works-

  • 1.It analyses all of the language statements for its correctness. If incorrect, throws an error.
  • 2.If no error, the compiler will convert source code to machine code.
  • 3.It links different code files into a runnable program(know as exe). 
  • 4.Run the Program
 A compiler is likely to perform many or all of the following operations: pre-processing, lexical analysis, parsing, semantic analysis, conversion of input programs to an intermediate representation, code optimization and code generation. 
Advantages of Compiler:
  • 1. It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.
  • 2.Generates output program which can be run independently from the original program.
Disadvantages of Compiler:
  • 1.Compiler displays all errors and warning at the compilation time. Therefore, you can’t run the program without fixing errors. 
  • 2.It generates the error message only after scanning the whole program. Hence debugging is comparatively hard. 
  • 3. Generates intermediate object code which further requires linking, hence requires more memory.

Interpreter



Interpreter is a translator program which is used to convert programs in high-level language to low-level language. Interpreter translates line by line and reports the error once it encountered during the translation process. It directly executes the operations specified in the source program when the input is given by the user. It gives better error diagnostics than a compiler.
Advantages of Interpreter:
  • 1.Interpreters are easier to use, especially for beginners.
  • 2.No intermediate object code is generated, hence are memory efficient.
  • 3.Continues translating the program until the first error is met, in which case it stops. Hence debugging is easy.
Disadvantages of Interpreter:
  • 1.It takes less amount of time to analyze the source code but the overall execution time is slower.
  • 2. Do not generate output program. So they evaluate the source program at every time during execution. 

Assembler

Assemblers are used to translate a program written in a low-level assembly language into a machine code (object code) file so it can be used and executed by the computer.

Once assembled, the program file can be used again and again without re-assembly.

Functions of assembler:
  • 1. Convert mnemonic operation codes to machine language equivalents. 
  • 2. Convert symbolic operands to machine addresses. 
  • 3. Build machine instructions.
  • 4. Convert data constants to internal representations.
  • 5. If there is any error in program, notify with error message.  

Summary of translators

CompilerInterpreterAssembler
Translates high-level languages into machine codeTemporarily executes high-level languages, one statement at a timeTranslates low-level assembly code into machine code
An executable file of machine code is produced (object code)No executable file of machine code is produced (no object code)An executable file of machine code is produced (object code)
Compiled programs no longer need the compilerInterpreted programs cannot be used without the interpreterAssembled programs no longer need the assembler
Error report produced once entire program is compiled.  These errors may cause your program to crashError message produced immediately (and program stops at that point)One low-level language statement is usually translated into one machine code instruction
Compiling may be slow, but the resulting program code will run quick (directly on the processor)Interpreted code is run through the interpreter (IDE), so it may be slow, e.g. to execute program loops
One high-level language statement may be several lines of machine code when compiled
The difference between an interpreter and a compiler is given below:
CompilerInterpreter
Scans the entire program and translates it as a whole into machine code.Translates program one statement at a time.
It takes large amount of time to analyze the source code but the overall execution time is comparatively faster.It takes less amount of time to analyze the source code but the overall execution time is slower.
Generates intermediate object code which further requires linking, hence requires more memory.No intermediate object code is generated, hence are memory efficient.
Compiler displays all errors and warning at the compilation time. Therefore, you can’t run the program without fixing errors. The interpreter reads a single statement and shows the error if any. You must correct the error to interpret next line.
It generates the error message only after scanning the whole program. Hence debugging is comparatively hard.Continues translating the program until the first error is met, in which case it stops. Hence debugging is easy.
Store machine language as machine code on the disk.Not saving machine code at all. 
Generates output program which can be run independently from the original program.Do not generate output program. So they evaluate the source program at every time during execution. 
Programming language like C, C++ use compilers.Programming language like Python, Ruby use interpreters.

Compiler Vs Interpreter

NoCompilerInterpreter
1Compiler takes Entire program as input at a time.Interpreter takes Single instruction as input at a time.
2Intermediate Object code is generatedNo Intermediate Object code is generated
3It execute conditional control statements fastly.It execute conditional control statements slower than Compiler
4More memory is required.Less memory is required.
5Program need not to be compiled every timeEvery time higher level program is converted into lower level program
6It display error after entire program is checkedIt display error after each instruction interpreted (if any)
7Example: CExample: BASIC

No comments:

Post a Comment