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

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.

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.

Ambiguity Elimination Compiler Design

Ambiguity Elimination Ambiguity elimination makes the sentence clear and readable. A sentence is grammatically ambiguous if it can produce more than one parse tree for a particular grammar. In this article,...

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.

Three-Address Code

Three-Address Code If there is at most one operator on the right side of the instruction, then the instruction will be the three-address code so that no arithmetic expressions are permitted....

2 minutes read.

Errors in Compiler Design

Introduction Errors in compiler design refer to mistakes or issues that arise during the process of execution of the program. A compiler is a program that translates source code written in...

4 minutes read.

LR Parser in Compiler Design

LR Parser The most popular type of bottom-up parsing is LR(K) parsing. The LR() parser scans the input from left – to – right, which is the actual abbreviation of L...

2 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.

Evolution of Programming Languages in Compiler Design

The Evolution of Programming Languages The first computer came in the 1940s and was programmed in a binary language that told the computer what operations are to be performed and in...

4 minutes read.

Boolean Expression in Compiler Design

Boolean Expression The translation of conditional statements such as if-else statements and while-do statements is associated with Boolean expression's translation. The main use of the Boolean expression is the following: Boolean expressions...

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.

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.

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.

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.

Derivation and Parse Tree in Compiler Design

Derivation and Parse Tree In this article, we will learn Derivation and Parse Tree. Derivations The parse tree can be constructed by taking a derivational view in which production is treated as rewriting...

4 minutes read.

S-attributed and L-attributed SDTs

S-attributed and L-attributed SDTs STD stands for Syntax Directed Translation. When we associate some informal notations called semantic rules and the grammar, they are known as STD. So we can say...

2 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.

Target Machine

Target Machine A target machine is a byte-addressable machine. This machine has n general-purpose registers, R0, R1,.....Rn-1. A Simple Target Machine Model has three-address instruction. A full-edged assembly language would have...

3 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.