Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Translator?
2.1.
Compiler
2.2.
Interpreter
2.3.
Assembler
3.
Advantages 
4.
Disadvantages 
5.
Frequently Asked Questions
5.1.
Which kind of errors can a translator program detect?
5.2.
Which comes first, compiler or assembler?
5.3.
What is the difference between a compiler and a translator in compiler design?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Translator in Compiler Design

Author Muskan Sharma
1 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

What do you understand by a translator? In layman's language, a translator translates one language into another language. But what do we have to do with this in computers? 
Let me tell you about this, computers understand machine language, and we (users) understand high-level language called source code.

So a translator in compiler design translates this high-level language into machine code. The translator takes input as the source code (high-level language) and gives output as the machine code.
This article will help you understand the translators in compiler design and why there is a need for translators.

Translator in Compiler Design

What is Translator?

A translator is a program that converts instructions written in source code to object code or from high-level to machine language.
Suppose you speak English and want to talk to a Japanese person. Then how will you communicate? You need someone to translate who is called a translator, who can speak English and Japanese both. So you both can communicate. The translator on a computer does the same job. It converts the source code into machine code, which the machine can understand.

Here are types of translators that will do the same in a computer and help to communicate from user to computer.

  • Compiler
     
  • Interpreter
     
  • Assembler
     

Now let us discuss each translator in depth one by one.

Compiler

The compiler is a translator that converts source code from high-level languages to machine code.

Compilers are much faster than interpreters, but error debugging is a little difficult process. Languages that use compilers are C++, C, and Java.

It works in three stages, explained below.

Flowchart of Compiler

Stage 1: It takes the source code as input.
 

Stage 2: It checks the source code and validates it. If some error occurs(when the rule of the language is not followed), it will send an error message.
 

Stage 3: If no error occurs, it sends the machine code as output to the machine.

Interpreter

Interpreter changes the source code to machine code, but the process is a bit changed here than in compilers.

An Interpreter is a translator that effectively accepts a source program and executes it directly without producing any object code first. It does this by fetching the source program instructions one by one, analyzing them one by one, and then "executing" them one by one. 

Here solving errors is very easy, but it increases the execution time. Languages in which interpreters are used: Python, Ruby, and BASIC.

The process of translating the source code to machine code using an interpreter is as follows:

Flowchart of Interpreter

Stage 1: It takes the source code as input.

Stage 2: It checks the source code line-by-line and validates it. If some error occurs(when the rule of the language is not followed), it will stop the code execution and send an error.

Stage 3: If no error occurs, it sends the machine code as output to the machine.


Now, in the article's next section, you'll learn about assemblers.

Assembler

Assembler is said to be the fastest translator among the three, as the assembler translates the low-level assembly code to machine code.

It translates by evaluating the symbols in the source code and generates the machine code accordingly. If an assembler does this in one scan, it is called a single-pass assembler; if it takes multiple scans, it is called a multi-pass assembler.

The process of translating the source code to machine code using assemblers is as follows:

Flowchart of Assembler

Stage 1: It defines symbols of the assembly code.

Stage 2: It processes some pseudocode operations.

Stage 3: Generates object code.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Advantages 

Let us look at the advantages of different types of translator in compiler design.


Compilers: 

  1. It helps to validate the code so that there is no system error.
     
  2. The user needs not to run the code on the same machine used during the code's creation.
     
  3. The execution of the code is faster with the help of the compilers as they optimize the code to take less time and space complexity.
     

Interpreters:

  1.  Error detection is easy and simultaneously when the user is writing the code.
     
  2. Once the code gets some error, it will notify the user so more accuracy in the code is there.
     
  3. Users can work on the small code and join or merge them, which can be done with more accuracy and less error.
     

Assemblers:

  1. Efficiency in code execution.
     
  2. Ease of code maintenance and error fixing.
     
  3. Less time in code execution as the code written in assembly language is closer to machine code, so it takes less time to convert to machine code, saving time.

Disadvantages 

Let us now look at the disadvantages of the different types of translator in compiler design.
 

Compiler:

  1. Execution time is more as it executes the whole code at a time.
     
  2. The whole source code needs to be executed again if some changes are made.
     
  3. The program compiled on platform 'A' may not be executed/run on platform 'B' without being recompiled.
     

Interpreters:

  1. Interpreted codes need more time for execution as the processor has to wait till the instructions are translated.
     
  2. The code is not optimized. Only the translated code is executed.
     
  3. A syntax error is possible on unverified scripts.
     

Assembler:

  1. Modifying or changing code is difficult as it is not in a high-level language.
     
  2. Assembler codes are machine-dependent and can only be executed on specific machines.
     
  3. The chance of getting errors in this code is higher than in high-level language code.


Also see,  cousins of compiler 

Also see, Cross Compiler

 

Frequently Asked Questions

Which kind of errors can a translator program detect?

The translator is used to translate the source code into machine language, which the machine can understand. While translating the source code, the machine detects the errors that are known as syntax errors.

Which comes first, compiler or assembler?

First, the compiler comes into the role. It takes the Preprocessed source code as the input and converts it into the assembly code. Then, this assembly code works for the assembler as the input, and the assembler converts it into the machine code.

What is the difference between a compiler and a translator in compiler design?

A compiler translates the entire source code into machine code in one step, while a translator can do this either line by line or in one step. Translators include both compilers and interpreters. Translators can either translate line by line or in one step.

Conclusion

In this article, you have learned about translator in compiler design. The different types of translators and their advantages and disadvantages. We hope this article helped you know more about translator in compiler design.

If you want to learn more, refer to these articles:

 

You may refer to our Guided Path on Code Studios for enhancing your skill set on DSACompetitive ProgrammingSystem Design, etc. Check out essential interview questions, practice our available mock tests, look at the interview bundle for interview preparations, and so much more!

Happy Learning, Ninja!!

Previous article
Attributes in Compiler Design
Next article
Phases of a Compiler
Live masterclass