Compiler Design: Introduction

We must have surely come across word ‘Compiler’ in our journey of programming and solving problems. We will try to understand some basic concepts related to Compiler Design in this article.

Flowchart explaining the working of a Compiler

A compiler is responsible for translating a a source code written in a high level language to a low level machine language without changing the meaning or function of the original code. Compiler Design mainly involves basic translation mechanism and error detection & recovery.

Attributes of a Great Compiler

Like every utility designed to solve a problem there must be some attributes that must be desired of Compilers. The 5 key attributes of Compilers are:

  1. Correctness
  2. Compilation speed
  3. Error reporting and Handling
  4. Preservation of meaning of original code
  5. Code debugging help

Types of Compilers

Compilers are classified into broadly three types:

  1. One Pass Compilers

The One Pass compiler passes through the parts of each compilation unit only once, translating each part into final machine code. Therefore it is faster than other types of Compilers.

Eg: The earlier Pascal Compiler

2. Two Pass Compilers

Two Pass Compiler flowchart

The Two Pass compiler comprises of 2 sections which are:

a) Front-end: Here the legal code is mapped into Intermediate Representation(IR). The front-end consists of the HLL (High Level Language) getting converted into three address code. The work done in Front-end is Platform Independent.

b) Back-end: It maps the Intermediate Representation(IR) onto the target machine. The back-end consists of three address code getting converted to LLL (Low Level Machine Language) . The work done in Back-end is Platform Dependent because the final set of executable machine level instructions are dependent on the system.

3. Multi Pass Compilers

Multi Pass Compiler flowchart

The Multi Pass compiler processes the input source code several times, wherein each pass takes the result of the previous pass as the input and creates an Intermediate Representation(IR). This can be visualized as having a lot of Middle-ends between the Front-end and the Back-end each taking an IR and then mapping it into another IR. The main benefits of Multi Pass compilers are:

a) They are Machine Independent, since multiple passes require a modular structure.

b) They remove the need for having Forward Declarations, hence allowing for more expressive languages.

Eg: Java

Phases of Compiler Design

Flowchart depicting Phases of Compiler Design

The Compiler works in various phases with each phase transforming the source code from one form to another. There are mainly 6 phases in Compiler Design:

  1. Lexical Analyzer
  2. Syntax Analyzer
  3. Semantic Analyzer
  4. IR Code Generator
  5. Code Optimizer
  6. Code Generator

All these 6 phases convert the source code by dividing it into tokens and create parse trees to optimize the code in each phase. We will understand how these phases of Compiler Design work in the next article of the series.

Stay tuned!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Anurag Gupta

Data Scientist @ Innovaccer Inc., and Augmented Reality Aficionado, Brace yourself for engaging articles ;)