The Wayback Machine - https://web.archive.org/web/20240605144105/https://www.geeksforgeeks.org/introduction-to-compilers/
Open In App

Introduction To Compilers

Last Updated : 11 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Compiler is a software that typically takes a high level language (Like C++ and Java) code as input and converts the input to a lower level language at once. It lists all the errors if the input code does not follow the rules of its language. This process is much faster than interpreter but it becomes difficult to debug all the errors together in a program. 

 
A compiler is a translating program that translates the instructions of high level language to machine level language. A program which is input to the compiler is called a Source program. This program is now converted to a machine level language by a compiler is known as the Object code.

 

How does a compiler work?

The process of compiling code involves several steps.Basically, Code passes through these steps sequentially and if there is any mistake in code then it will be examined through these steps and  thus compilation process stops in-between and show compilation error.Oherwise if everything is OK then compiler does not show any error and compile the source code . There are the following steps which are involved during compiling:

Lexical Analysis

The source code is first tested by the compiler’s lexer, which breaks the source code into tokens, such as keywords, identifiers, operators, and punctuation.

Syntax Analysis

The next step is syntax analysis, where the compiler’parser checks the code for syntax errors and ensures that it follows the rules of the programming language. The compiler generates an Abstract Syntax Tree (AST) that represents the structure of the code.

Semantic Analysis

Once the code is syntactically correct, the compiler performs semantic analysis on parsed code to find the meaning. The compiler check for logical errors, such as type mismatches, undeclared variables, and incorrect usage of operators.

Optimization

The compiler may perform various optimizations to improve the performance of the resulting code. It is an optional phase of compiler. It removes dead code and arranges the sequence of instructions in order to boost the program execution.

Code generations

The last step is code generation, where the compiler translates the AST into machine-readable code. The code generator creates assembly language code, which is then translated into binary code that can be executed by the computer.

Why are compilers important?

There are the following reasons why compilers are important to us:

1)Enabling concept of High Level Language

 They enable developers to write code in high-level programming languages, which are easier to understand and  more human-readable than machine code.

2)One time effort(Efficiency)

 Compilers convert high-level code  into machine code .Now, this machine code is stored in a file having  extension(.exe) which is directly  executed by computer. So,programmer need only one time effort to compile code  into  machine code and after that  they  can  use code  whenever they want using (.exe) file. This process allows  programs to run much faster and  more  efficiently than  if  they were interpreted at runtime.

3)Portability

Compilers translates source code which is written in high-level programming language  into machine code.And thus,it enable portability feature because this machine code can be runned  on many different operating systems and hardware architectures because it is platform independent.

4)Security 

Compiler can provide programmer secuirity by  preventing memory-related errors, such as buffer overflows, by analyzing and optimizing the code. It can also generate warnings or errors if it detects potential memory issues.

There are different Compilers : 
 

  • Cross-Compiler – The compiled program can run on a computer whose CPU or Operating System is different from the one on which the compiler runs. 
     
  • Bootstrap Compiler – The compiler written in the language that it intends to compile. 
     
  • Decompiler – The compiler that translates from a low-level language to a higher level one. 
     
  • Transcompiler – The compiler that translates high level languages. 
     

A compiler can translate only those source programs which have been written in the language for which the compiler is meant. Each high level programming language requires a separate compiler for the conversion. 

For Example, a FORTRAN compiler is capable of translating into a FORTRAN program. A computer system may have more than one compiler to work for more than one high level languages. 

Top most Compilers used according to the Computer Languages – 
 

  • C– Turbo C, Tiny  C Compiler, GCC, Clang, Portable C Compiler 
     
  • C++ -GCC, Clang, Dev C++, Intel C++, Code Block 
     
  • JAVA– IntelliJ IDEA, Eclipse IDE, NetBeans, BlueJ, JDeveloper 
     
  • Kotlin– IntelliJ IDEA, Eclipse IDE 
     
  • Python– CPython, JPython, Wing, Spyder 
     
  • JavaScript– WebStorm, Atom IDE, Visual Studio Code, Komodo Edit 
     

 Conclusion:

 compilers are critical tools for software development. They enable developers to write code in high-level programming languages, ensure that the code is correct and efficient, and make it possible to develop software for different platforms and architectures. Understanding compilers is essential for any programmer who wants to develop efficient, reliable, and scalable software.


Similar Reads

Compiler Design - Science of Building a Compilers
The purpose of this article is to provide an introduction to the science of compiler design, by describing how code generation and optimization work, as well as modeling in compiler design and implementation. Below, these topics are important to understand before building a compiler. Code Generation and Optimization:Code generation is the process o
8 min read
Single Pass vs Two-Pass (Multi-Pass) Compilers
This article explores the concept of compiler passes in the field of software development, focusing on two types: the Single Pass Compiler and the Two-Pass Compiler (Multi-Pass Compiler). It explains their differences, advantages, and use cases, providing insights into the world of compiler design. What is a Compiler Pass?A Compiler pass refers to
4 min read
Parsing | Set 1 (Introduction, Ambiguity and Parsers)
Parsing is performed at the syntax analysis phase where a stream of tokens is taken as input from the lexical analyzer and the parser produces the parser tree for the tokens while checking the stream of tokens against the syntax errors. Role of ParserIn the syntax analysis phase, a compiler verifies whether or not the tokens generated by the lexica
3 min read
Introduction to YACC
Each translation rule input to YACC has a string specification that resembles a production of a grammar-it has a nonterminal on the LHS and a few alternatives on the RHS. For simplicity, we will refer to a string specification as a production. YACC generates an LALR(1) parser for language L from the productions, which is a bottom-up parser. The par
4 min read
Introduction to Interpreters
Prerequisite - Assembler, Compiler, and Interpreter All high-level languages need to be converted to machine code so that the computer can understand the program after taking the required inputs. The software by which the conversion of the high-level instructions is performed line-by-line to machine-level language, other than compiler and assembler
3 min read
Introduction to Intermediate Representation(IR)
Intermediate Representation(IR), as the name suggests, is any representation of a program between the source and target languages. The intermediate form of the program that is being compiled is the central data structure in a compiler. A compiler may have a single IR or a series of IRs. The decisions that are made during the design of IR affect the
3 min read
Introduction to Register Descriptor
What is Register Descriptor?A register descriptor is a data structure that provides information about a register in a computer's processor. The register descriptor is used by the compiler and runtime system to manage the use of registers in the processor. It allows the compiler to select the appropriate registers for different operations and helps
5 min read
Introduction to Address Descriptor
What is Address Descriptor?An address descriptor is a data structure that provides information about an address in a computer's memory. The address descriptor is used by the operating system and runtime system to manage access to different regions of memory. It allows the operating system to set up and maintain memory protection boundaries and help
4 min read
Introduction of Compiler Design
The compiler is software that converts a program written in a high-level language (Source Language) to a low-level language (Object/Target/Machine Language/0, 1's). A translator or language processor is a program that translates an input program written in a programming language into an equivalent program in another language. The compiler is a type
9 min read
Introduction to Syntax Analysis in Compiler Design
When an input string (source code or a program in some language) is given to a compiler, the compiler processes it in several phases, starting from lexical analysis (scans the input and divides it into tokens) to target code generation. Syntax Analysis or Parsing is the second phase, i.e. after lexical analysis. It checks the syntactical structure
7 min read
Article Tags :