System Software

Compiler

Compiler

A compiler is a software that converts the source code to the object code. In other words, we can say that it converts the high-level language to machine/binary language. Moreover, it is necessary to perform this step to make the program executable. This is because the computer understands only binary language.

Some compilers convert the high-level language to an assembly language as an intermediate step. Whereas some others convert it directly to machine code. This process of converting the source code into machine code is called compilation. Let us learn more about it in detail.

Analysis of a Source Program

We can analyze a source code in three main steps. Moreover, these steps are further divided into different phases. The three steps are:

1. Linear Analysis

Here, it reads the character of the code from left to right. The characters having a collective meaning are formed. We call these groups tokens.

2. Hierarchical Analysis

According to collective meaning, we divide the tokens hierarchically in a nested manner.

3. Semantic Analysis

In this step, we check if the components of the source code are appropriate in meaning.

Phases/Structure of Compiler

The compilation process takes place in several phases. Moreover, for each step, the output of one step acts as the input for the next step. The phases/structure of the compilation process are is follows:

1. Lexical Analyzer

  • It takes the high-level language source code as the input.
  • It scans the characters of source code from left to right. Hence, the name scanner also.
  • It groups the characters into lexemes. Lexemes are a group of characters which has some meaning.
  • Each lexeme corresponds to form a token.
  • It removes white spaces and comments.
  • It checks and removes the lexical errors.

2. Syntax Analyzer

  • ‘Parser’ is the other name for the syntax analyzer.
  • The output of the lexical analyzer is its input.
  • It checks for syntax errors in the source code.
  • It does this by constructing a parse tree of all the tokens.
  • For the syntax to be correct, the parse tree should be according to the rules of source code grammar.
  • The grammar for such codes is context-free grammar.

3. Semantic Analyzer

  • It verifies the parse tree of the syntax analyzer.
  • It checks the validity of the code in terms of programming language. Like, compatibility of data types, declaration, and initialization of variables, etc.
  • It also produces a verified parse tree. Furthermore, we also call this tree an annotated parse tree.
  • It also performs flow checking, type checking, etc.

4. Intermediate Code Generator (ICG)

  • It generates an intermediate code.
  • This code is neither in high-level language nor in machine language. It is in an intermediate form.
  • It is converted to machine language but, the last two phases are platform dependent.
  • The intermediate code is the same for all the compilers. Further, we generate the machine code according to the platform.
  • An example of an intermediate code is three address code.

5. Code Optimizer

  • It optimizes the intermediate code.
  • Its function is to convert the code so that it executes faster using fewer resources (CPU, memory).
  • It removes any useless lines of code and rearranges the code.
  • The meaning of the source code remains the same.

6. Target Code Generator

  • Finally, it converts the optimized intermediate code into the machine code.
  • This is the final stage of the compilation.
  • The machine code which is produced is relocatable.
compiler

Phases of Compiler

All these phases of a compiler divide into two sections:

a) Front End

The phases of lexical analysis, syntax analysis, semantic analysis, and intermediate code generation comes under this category.

b) Back End

While the other last two phases come under the back end.

Browse more Topics under System Software

Types of Compiler

1. Cross Compilers

They produce an executable machine code for a platform but, this platform is not the one on which the compiler is running.

2. Bootstrap Compilers

These compilers are written in a programming language that they have to compile.

3. Source to source/transcompiler

These compilers convert the source code of one programming language to the source code of another programming language.

4. Decompiler

Basically, it is not a compiler. It is just the reverse of the compiler. It converts the machine code into high-level language.

Features of a Compiler

The features are as follows:

  • Compilation speed.
  • The correctness of machine code.
  • The meaning of code should not change.
  • Speed of machine code.
  • Good error detection.
  • Checking the code correctly according to grammar.

Uses/Application of Compilers

  • Helps to make the code independent of the platform.
  • Makes the code free of syntax and semantic errors.
  • Generate executable files of code.
  • Translates the code from one language to another.

Difference Between Compiler and Interpreter

A compiler checks the whole program at once. It displays all the errors at a place once the whole program is checked. On the other hand, an interpreter checks the program line by line. If an error is detected the execution stops.

Frequently Asked Questions (FAQs)

Q1. What is a compiler?

A1. It is software that converts the source code into machine code. The process is called compilation.

Q2. What are the phases/structure of a compiler?

A2. The phases are:

  • lexical analyzer
  • syntax analyzer
  • semantic analyzer
  • intermediate code generator
  • code optimizer
  • target code generator

Q3. What is a symbol table?

A3. It helps to find the names of identifiers easily. It consists of identifiers and their types.

Q4.  What is the difference between compiler and interpreter?

A4. The compiler checks the code as a whole whereas, the interpreter checks it line by line.

Q5. What is a decompiler?

A5. It converts machine code to the source code. It is the reverse of the compiler.

Share with friends
Customize your course in 30 seconds

Which class are you in?

5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Download the App

Watch lectures, practise questions and take tests on the go.
Customize your course in 30 seconds

Which class are you in?

No thanks.