Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is a Compiler
2.1.
High-Level Language
2.2.
Low-Level Language
3.
Working of a Compiler
4.
Types of Compiler
4.1.
Traditional Compiler
4.2.
Incremental Compiler
4.3.
JIT Compiler
4.4.
Source to Source Compiler
4.5.
Cross Compilers 
4.6.
Single-pass Compiler
4.7.
Two pass Compiler
4.8.
Multi pass Compiler
5.
Frequently Asked Questions
5.1.
What is the difference between a high-level and a low-level programming language?
5.2.
What is the difference between Single-pass and Multi-pass Compilers?
5.3.
What are the different stages of Compiler Design? 
5.4.
What are the 4 steps of compiler?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Types of Compiler

Author Jay Dhoot
0 upvote
Master Python: Predicting weather forecasts
Speaker
Ashwin Goyal
Product Manager @

Introduction

We have written many programmable codes on compilers, but have you ever thought about what a compiler does? What are the different types of Compilers?

In simple terms, a compiler converts high-level instructions into low-level instructions that a machine understands.

Types of Compilers

In this article, we will learn about types of compiler and their working. Moving forward, let's understand about compilers briefly. 

What is a Compiler

A compiler is a software or a computer program that converts code from one programming language to another language without changing the definition of code. It converts a source code (High-Level-Language) into a language that a machine understands (Low-Level-Language). A Compilation Error occurs when the compiler fails to compile or convert the source code due to errors in the source code or in the compiler itself.

Compiler

High-Level Language

A High-Level Language is a programming language that allows humans to create computer programs and interact with the computer system. These languages are considered ‘High-Level’ as they are similar to human languages and use keywords and syntax, making them easier to learn and understand. They are independent of the computer systems and offer development tools like built-in functions and libraries. While writing a high-level language code, full attention needs to be put on the logic of the problem. 

C++, Java, and Python are popular high-level programming languages.

Low-Level Language

A Low-Level Language deals with the computer’s hardware system and its components. It is machine language that provides no abstraction from the hardware. It generally provides specific instructions to the computer processor and is represented in binary forms (‘0’ or ‘1’). While high-level languages are independent of computer systems, low-level languages can only be executed by the processor for which it is written. These languages do not require programming ideas and concepts.

Binary, machine, and assembly codes are typical examples of Low-Level programming languages.

Also see, Cross Compiler

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

Working of a Compiler

There are six significant steps involved in the working of a compiler, namely:

  1. Lexical Analysis: This is the first stage that involves scanning the source code. The compiler scans the source code character by character and performs tokenization. Tokenization refers to breaking the source code into tokens like keywords, operators, and identifiers.
     
  2. Syntax Analysis: Parsing is done in this phase. In this stage, the compiler verifies the syntax of the code and ensures that the proper rules of the programming language are followed or not. A parse tree is created by the compiler based on the tokens of the program. A parse helps in checking for syntax errors.
     
  3. Semantic Analysis: This is the third stage, where the compiler checks whether the parse tree follows the required rule of language. Type checking is performed, which ensures that the operations are performed correctly and on compatible data types. The compiler also looks for errors like incorrect function calls or undeclared variables. An annotated syntax tree is produced as an output.
     
  4. Intermediate Code Generation: The compiler generates an intermediate code from the source code. An intermediate code is in between source code and machine code. The intermediate code is generated in a way that makes it easier to convert this code into target machine code.
     
  5. Code Optimization: In this phase, the intermediate code is optimized. The optimization involves changing the organization of statements, removing unnecessary code lines, etc., which improves the overall code performance.
     
  6. Code Generation: This is the final stage in which an optimized machine code is generated. In this stage, the intermediate code is taken as input and mapped with the machine code. The code generator translates the intermediate code to machine code.

Types of Compiler

There are many different types of Compilers. A few of them are mentioned below.

Traditional Compiler

Traditional Compilers simply convert a high-level language program code into its corresponding machine code. 

For example, a traditional compiler converts C++ source code to machine or assembly code.

Incremental Compiler

Incremental Compilers generate machine code for the statements independent of the machine code generated for other statements. It recompiles only for those lines of source code which are modified, and this recompiled code is merged with previously combined code to develop a new target code. 

For example, C/C++ GNU compiler.

JIT Compiler

Just In Time Compilers or JIT Compilers are run-time compilers that help form executable code (machine code) from intermediate code (byte code). These compilers perform specific optimizations while compiling a series of bytecodes. They also implement type-based verification, which makes the machine code more authentic and optimized.

Source to Source Compiler

It translates the high-level language code (source code) from one programming language to another. It is a transcompiler. For example, a source-to-source compiler converts a C program source code to another source code like C++, Java, etc. It is majorly used for converting the old code of one programming language to its newer versions.

Cross Compilers 

A cross-compiler creates executable code for a platform other than the one on which it is running. For example, a cross-compiler running on a machine using C programming language can produce executable code for a machine using Java programming language.

Single-pass Compiler

A Single-pass Compiler combines all the compiler phases in a single module.

Single Pass Compiler working

The phases involve extracting the tokens from the source code, and then the syntax of the programming language used in the source code is checked. A parse tree is created, and semantic analysis is done, which checks the meaning and correctness of the source code. Finally, an optimized machine code gets created.

Single Pass Compiler

Two pass Compiler

A Two pass compiler processes the source code into machine code in two passes.

Two Pass Compiler working

It requires two passes to scan the source code and perform its translation.

Two Pass Compiler

The compiler is broadly divided into two phases namely:

  1. Front End: This is the analysis Phases of Compiler which involves scanning the source code, performing lexical analysis and finding the syntax errors. This phase generates an Intermediate Code which is passed to the Back end.
     
  2.  Back End: Back end or the synthesis phase generates the machine code with symbol table representation and intermediate code.

Multi pass Compiler

A multi-pass compiler processes the source code in multiple passes. A huge program is broken into various small programs, and all of these programs run simultaneously. Numerous intermediate codes are generated, and the output of the last phase is an input of the current phase.

Multi pass Compiler


Also see,  cousins of compiler

Frequently Asked Questions

What is the difference between a high-level and a low-level programming language?

A High-Level programming language allows humans to create computer programs and interact with the computer system while a Low-Level programming language deals with the computer’s hardware system and its components. Low-Level programming languages  generally provide specific instructions to the computer processor and is represented in binary forms (‘0’ or ‘1’). While high-level languages are independent of computer systems, low-level languages can only be executed by the processor for which it is written.

What is the difference between Single-pass and Multi-pass Compilers?

A Single-pass Compiler combines all the compiler phases in a single module, and the source code is translated in a single pass. Multi-pass compilers process the source code in more than one pass or multiple passes. A huge program is broken into various small programs, and all of these programs run simultaneously.

What are the different stages of Compiler Design? 

There are many different stages involved in compiler design: Lexical Analysis, Syntax Analysis, Semantic Analysis, Code Generation, and Optimization.

What are the 4 steps of compiler?

In C, the compilation process involves four steps which are: Preprocessing, Compiling, Assembling, and Linking.

  • Preprocessing: All statements with a # symbol are processed by pre-processor
  • Compiling: Source code is converted into machine-level code
  • Assembling: Assembler is responsible for converting assembly-level code into machine-level code
  • Linking: It is a process of including the library files into a program.

Conclusion

In this article, we have discussed different types of Compiler. To learn more about compilers, you can refer to the below-mentioned articles:


We hope this article has helped you understand compiler and their different types. If this article helped you in any way, then you can read more such articles on our platform, Coding Ninjas Studio. You will find articles on almost every topic on our platform. Also, for cracking good product-based companies, you can practise coding questions at Coding Ninjas. For interview preparations, you can read the Interview Experiences of popular companies.
 

Happy Coding !!

Previous article
Bootstrapping in Compiler Design
Next article
Cousins of Compiler
Live masterclass