Chapter 1 Introduction To Compiler Design
Chapter 1 Introduction To Compiler Design
Chapter 1 Introduction To Compiler Design
✓ We have learned that Computers are a mix of software and hardware. Hardware is just a piece
of mechanical device and its functions are being controlled by a compatible software.
✓ Hardware understands instructions in the form of electronic charge, which is the counterpart
of binary language in software programming. Binary, 0 and 1.
✓ To instruct, the hardware codes must be written in binary format, which is simply a series of
1s and 0s. It would be a difficult for computer programmers to write such codes, which is why
we have compilers to write such codes
✓ Seeing the machine code generated for language constructs helps understand performance
issues for languages
✓ These programs are then fed into a series of tools and OS components to get the desired
code that can be used by the machine. This is known as Language Processing System.
Assembler
An assembler translates assembly language programs into machine code. The output of an
assembler is called an object file, which contains a combination of machine instructions as
well as the data required to place these instructions in memory.
Compiler
A compiler is a program takes a program written in a source language and translates it into
an equivalent program in a target language.
It is computer program that links and merges various object files together in order to make
an executable file.
Loader
▪ Loader is a part of operating system and is responsible for loading executable files into
memory and execute them.
▪ It calculates the size of a program (instructions and data) and creates memory space for it.
Let us first understand how a program, using C compiler, is executed on a host machine.
✓ The C/C++ compiler compiles the program and translates it to assembly program (low-
level language).
✓ An assembler then translates the assembly program into machine code (object).
✓ A linker tool is used to link all the parts of the program together for execution (executable
machine code).
✓ A loader loads all of them into memory and then the program is executed.
Cross-compiler
✓ A compiler that runs on platform (A) and is capable of generating executable code for
platform (B) is called a cross-compiler.
Source-to-source Compiler
✓ A compiler that takes the source code of one programming language and translates it into
the source code of another programming language is called a source-to-source compiler.
There are two major phases of compilation, which in turn have many parts. Each of
them takes input from the output of the previous level and works in a coordinated
way.
Analysis phase (front -end), An intermediate representation is created from the given source
code:
✓ Lexical Analyzer
✓ Syntax Analyzer
✓ Semantic Analyzer
✓ Intermediate Code Generator
The lexical analyzer divides the program into “tokens”, the Syntax analyzer recognizes
“sentences” in the program using the syntax of the language and the Semantic analyzer checks the
static semantics of each construct. Intermediate Code Generator generates “abstract” code
The process of converting the source code into machine code involves several phases or
stages, which are collectively known as the phases of a compiler. The typical phases of a
compiler are:
1. Lexical Analysis: The first phase of a compiler is lexical analysis, also known as
scanning. This phase reads the source code and breaks it into a stream of tokens, which
are the basic units of the programming language. The tokens are then passed on to the
next phase for further processing.
2. Syntax Analysis: The second phase of a compiler is syntax analysis, also known as
parsing. This phase takes the stream of tokens generated by the lexical analysis phase
The compiler has two modules namely the front end and the back end. Front-end constitutes the
Lexical analyzer, semantic analyzer, syntax analyzer, and intermediate code generator. And the
rest are assembled to form the back end.
Operations of Compiler
These are some operations that are done by the compiler.
® It breaks source programs into smaller parts.
® It enables the creation of symbol tables and intermediate representations.
® It helps in code compilation and error detection.
® it saves all codes and variables.
® It analyses the full program and translates it.
® Convert source code to machine code.
Advantages of Compiler Design
1. Efficiency: Compiled programs are generally more efficient than interpreted
programs because the machine code produced by the compiler is optimized for the
specific hardware platform on which it will run.
2. Portability: Once a program is compiled, the resulting machine code can be run on
any computer or device that has the appropriate hardware and operating system,
making it highly portable.
3. Error Checking: Compilers perform comprehensive error checking during the
compilation process, which can help catch syntax, semantic, and logical errors in the
code before it is run.
4. Optimizations: Compilers can make various optimizations to the generated
machine code, such as eliminating redundant instructions or rearranging code for
better performance.
Both High level language and low-level language are the programming language’s types. The
main difference between high level language and low-level language is that, Programmers can
easily understand or interpret or compile the high-level language in comparison of machine. On
the other hand, Machine can easily understand the low-level language in comparison of human
beings. Examples of high-level languages are C, C++, Java, Python, etc. Let’s see the difference
between high level and low-level languages:
HLL LLL
✓ It is programmer friendly language. ✓ It is a machine friendly language.
✓ High level language is less memory ✓ Low level language is high memory
efficient. efficient.
✓ It is easy to understand. ✓ It is tough to understand.
✓ Debugging is easy. ✓ Debugging is complex comparatively.
✓ It is simple to maintain. ✓ It is complex to maintain
✓ It is portable. comparatively.
✓ It can run on any platform. ✓ It is non-portable.
✓ It is machine-dependent.
1. Scanner Generator – It generates lexical analyzers from the input that consists of
regular expression description based on tokens of a language. It generates a finite