Compiler Design Tutorial

Our compiler design tutorial will provide all the information about compiler from basic to advanced level. This compiler tutorial will help the student for their semester as well as for competitive exams. This compiler tutorial is designed for both beginners and professionals as well.

What is Compiler?

The compiler is a software that converts high-level (Source Code) language to low-level language (Machine code) without changing the meaning of the Source code. In order to provide an optimized result, the compiler should be efficient enough that it takes less time and space while executing the code.

What is Compiler?

Why Compiler?

Language Processing System

The computer is built as a combination of hardware and software. The hardware cannot understand a human-readable language, or mainly, the code is written in the high-level language. So, in order to understand this code by machines, it goes through multiple transformations, and this is when the language processing system comes into play. It plays an important role in compiler design.

Whenever we write code in any high-level language, the high-level language goes through different phases and converts into binary language.

In the language procedure system, we'll see how a code written in any high-level language is converted into machine-level language.

Consider the following example;

Why Compiler?
  • A user writes code in a high-level language like c. 
  • The compiler of high-level language converts source code into assembly language.
  • An assembly language is an intermediate between source code and machine language.
  • Then assembler converts the code that is compiled before into relocatable machine code.
  • Lastly, the linker links all the files and loader puts all the executable program together and load into memory.

Before moving forward, we need to understand the few terms related to the compiler.

HLL – If any program consists #<include> or #<define>, then it is known as high-level language (HLL)

Pre-Processor – Pre-Processor will remove all the #<include> by file inclusion and #<define> by macro expansion.

Assembly Language – It's neither completely in the form of 0s and 1s nor in a high-level language. It is somewhere intermediate between the high-level language and machine code.

Assembler – Assembler is platform dependent. It means that for every platform (Hardware + OS), we'll have an assembler. It takes assembly language as an input and converts it into relocatable machine code.

Linker/Loader – Linker and loader is the component of the operating system. The linker is a program that combines different object files into a single executable file. The main task of the linker is to search and locate the reference in the main memory when an executable code will be loaded. Then loader loads all the programs into the main memory and executes them.

Interpreter – The interpreter also converts high-level language into low-level language. But interpreter is different from the compiler based on how they take the input. An interpreter takes input in different ways. The compiler reads all the input at one time and does the processing and execute them. But in the case of an interpreter, it takes input line by line and shows if the source code is error-free or has some error. The compiler takes the whole program at the same time and converts it into machine-level language, whereas the interpreter takes input line by line. Interpreter language is slower than compiler language.

Difference between Interpreter and Compiler

S.NOCOMPILERINTERPRETER
1.It scans the whole program at one time.It scans each line of the program line by line.
2.It shows the error at the end of the execution of the program.It shows the error line by line.
3.It is faster than the interpreter.It is much slower as compared to the compiler.
4.Error detection in the compiler is difficult.Error detection in the interpreter is easy.
5.It shows all the error after the execution of the programIt shows all the error line by line
6.Examples are C, C#, C++Examples are PHP, Python, Perl etc.

Compiler Design Index

Misc


Related Topics

LR Parser Compiler Design

LR Parser LR parsing is a type of bottom-up parsing that is used to parse the large class of grammars. Here "L" stands for left-to-right scanning of the input "R" stands...

6 minutes read.

Intermediate-Code Generator Compiler Design

Intermediate-Code Generator The process of translating a source language into machine code for a given target machine is done by intermediate-code. It lies between the high-level language and the machine language....

2 minutes read.

SLR 1 Parsing Compiler Design

SLR(1) Parsing It is a simple LR parsing. Most of the function of this parsing is the same as LR(0) parsing. The parsing table for both the parser vary. The SLR(1)...

5 minutes read.

LEX

LEX Lex is a tool/computer program that generates a Lexical analyzer. Lex is developed by Vern Paxson in C around 1987. Lex works together with the YACC parser generator. It allows...

3 minutes read.

Phases of Compiler

Phases of Compiler The compilation process of a compiler goes through various phases. Each phase of the compiler takes the output from the previous as an input. Here we will see all...

2 minutes read.

YACC in Compiler Design

YACC  YACC is known as Yet Another Compiler Compiler. It is used to produce the source code of the syntactic analyzer of the language produced by LALR (1) grammar. The input...

5 minutes read.

Bottom-Up Parsing in Compiler Design

Bottom-Up Parsing A bottom-up parsing constructs the parse tree for an input string beginning from the bottom (the leaves) and moves to work towards the top (the root). Bottom-up parsing is...

6 minutes read.

Finite State Machine

Finite State Machine A finite state machine is a simple machine to recognize patterns. It takes a string of symbols as the input and changes its state to another state, but...

3 minutes read.

Code Generation

Code Generation The last phase of the compiler is code generation. It is the compiler's back-end that makes multiple passes over the IR before generating the target program. The code generator's...

4 minutes read.

CLR Parsing Compiler Design

CLR Parsing CLR parsing refers to the canonical lookahead. We will use the canonical collection of LR(1) items for the construction of the CLR(1) parsing table. Generally, CLR(1) parsing has more...

6 minutes read.

Data Flow Analysis in Compiler Design

Data Flow Analysis All the optimization techniques we have learned earlier depend on data flow analysis. DFA is a technique used to know about how the data is flowing in any...

3 minutes read.

Symbol Table Compiler Design

Symbol Table It is an important data structure used by the compiler. It stores information about various entities such as object, class, variable names, functions name, interfaces, procedure, literals, string etc.  The...

3 minutes read.

Regular Expression | Compiler Design

Regular Expression A regular expression is a set of patterns that can match a character or string. It can also match alternative characters or strings. The grammar defined by the regular...

3 minutes read.

Parsing in Compiler Design

Parsing is the term used to describe the process of converting data between different formats. The parser can carry out this operation. The parser is a part of the translator...

4 minutes read.

Storage Allocation

Storage Allocation The storage allocation represents memory management. The allocation of memory can be done in the following ways: Static AllocationStack AllocationHeap Management Static Allocation: It is a procedure used for the allocation of...

1 minute read.

Translation of Array References in Compiler Design

Translation of Array References We can access the elements of an array stored in consecutive blocks very easily and quickly. In a programming language like C and Java, the size of...

2 minutes read.

Run-Time Storage Management

Run-Time Storage Management Every executing program has its own logical address space. Logical address space is partitioned into: Code: It is responsible for storing the executable target code. Static: It is used to...

3 minutes read.

Basic Blocks and Flow Graphs in Compiler Design

Basic Blocks and Flow Graphs In this section, we are going to learn how to work with basic block and flow graphs in compiler design. Basic Block The basic block is a set...

3 minutes read.

Lexical Analysis in Compiler Design

Lexical Analysis It is the first phase of the compiler. As we know, it is also known as a scanner. The input for lexical analysis is source code. After taking source...

2 minutes read.

Stack Allocation of Space

Stack Allocation of Space Almost all compilers for languages that use procedure, functions, or methods manage their run-time memory as a stack. Whenever a procedure is called, the local variable's space...

2 minutes read.