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.
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:
- Compilation speed
- Error reporting and Handling
- Preservation of meaning of original code
- Code debugging help
Types of Compilers
Compilers are classified into broadly three types:
- 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
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
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.
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:
- Lexical Analyzer
- Syntax Analyzer
- Semantic Analyzer
- IR Code Generator
- Code Optimizer
- 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.