Chapter 1 Introduction
Chapter 1 Introduction
Introduction
Outlines
Phases of a Compiler.
Computer Language Representation.
Compiler Construction Tools.
2
Introduction
Compiler is a translator that converts high-level language into the
machine language.
High-level language is written by a developer and machine language
can be understood by the processor.
In the first part, the source program compiled and translated into the
object program (low level language).
In the second part, object program translated into the target program
through the assembler.
3
Cont.…
6
Cont.…
7
Cont.…
8
Cont.…
9
Cont.…
10
Phases of a Compiler
12
Cont.…
Lexical Analysis:-
Lexical analyzer phase is the first phase of compilation process. It takes
source code as input. It reads the source program one character at a
time and converts it into meaningful lexemes. Lexical analyzer
represents these lexemes in the form of tokens.
Consider the following source codes and various phases.
14
After Lexical Analysis it is
converted into Lexemes and
tokens.
15
Cont.…
Syntax Analysis:-
Syntax analysis is the second phase of compilation process. It takes
tokens as input and generates a parse tree as output.
In syntax analysis phase, the parser checks that the expression made
by the tokens is syntactically correct or not as shown below.
16
Cont.…
17
Cont.…
Semantic Analysis:-
Semantic analysis is the third phase of compilation process. It checks
whether the parse tree follows the rules of language. Semantic analyzer
keeps track of identifiers, their types and expressions. The output of
semantic analysis phase is the annotated tree syntax.
18
Cont.…
19
Cont.…
20
Cont.…
21
Cont.…
Code Optimization:-
Code optimization is an optional phase. It is used to improve the
intermediate code so that the output of the program could run faster
and take less space. It removes the unnecessary lines of the code and
arranges the sequence of statements in order to speed up the program
execution.
22
Cont.…
23
Cont.…
Code Generation:-
Code generation is the final stage of the compilation process. It takes
the optimized intermediate code as input and maps it to the target
machine language. Code generator translates the intermediate code into
the machine code of the specified computer.
24
Cont.…
25
Computer Language Representation
Language Processors:-
A special translator system software is used to translate the program
written in a high-level language into machine code is called Language
Processor and the program after translated into machine code is object
program/object code.
Compilers, interpreters, translate programs written in high-level
languages into machine code that a computer understands. And
assemblers translate programs written in low-level or assembly language
into machine code. In the compilation process, there are several stages. To
help programmers write error-free code, tools are available.
26
Cont.…
Assembly language is machine-dependent, yet mnemonics used to
represent instructions in it are not directly understandable by machine
and high-Level language is machine-independent.
A computer understands instructions in machine code, i.e. in the form
of 0s and 1s. It is a tedious task to write a computer program directly in
machine code. The programs are written mostly in high-level languages
like Java, C++, Python etc. and are called source code. These source
code cannot be executed directly by the computer and must be
converted into machine language to be executed.
27
Types of Language Processors
Compiler :-
It reads the complete source program written in High-Level Language
and translates it into an equivalent Machine Language. Example: C, C+
+, C#, Java.
In a compiler, the source code is translated to object code successfully if
it is free of errors.
The compiler specifies the errors at the end of the compilation with line
numbers when there are any errors in the source code. The errors must be
removed before the compiler can successfully recompile the source code
again the object program can be executed number of times without
translating it again.
28
Cont.…
Assembler:-
Assembler is used to translate the program written in Assembly Language
into machine code. The source program is an input of an assembler that
contains assembly language instructions. The output generated by the
assembler is the object code or machine code understandable by the
computer. Assembler is basically the 1st interface that is able to
communicate humans with the machine. We need an Assembler to fill the
gap between human and machine so that they can communicate with each
other. code written in assembly language is some sort of
mnemonics(instructions) like ADD, MUL, MUX, SUB, DIV, MOV and
so29on.
Cont.…
Interpreter:-
The translation of a single statement of the source program into machine
code is done by a language processor and executes immediately before
moving on to the next line is called an interpreter. If there is an error in
the statement, the interpreter terminates its translating process at that
30
Cont.…
The interpreter moves on to the next line for execution only after the
removal of the error. An Interpreter directly executes instructions written
in a programming or scripting language without previously converting
them to an object code or machine code. An interpreter translates one line
at a time and then executes it. Example:- Perl, Python and MATLAB.
31
Difference between Compiler and Interpreter
Compiler Interpreter
A compiler is a program
that converts the entire
An interpreter takes a
source code of a
source program and runs it
programming language
line by line, translating
into executable machine
each line as it comes to it.
code for a CPU.
The compiler writer can use some specialized tools that help in
implementing various phases of a compiler. These tools assist in the
creation of an entire compiler or its parts. Some commonly used
compiler construction tools include:-
Parser Generator:- It produces syntax analyzers (parsers) from the
input that is based on a grammatical description of programming
language or on a context-free grammar.
It is useful as the syntax analysis phase is highly complex and
consumes more manual and compilation time. Example: PIC, EQM
34
Cont.….
38
Features of Compiler Construction Tools
39
Cont.….
Code Generation Tools:- These tools help in generating the target code
for the compiler. They take as input the abstract syntax tree produced by
the parser and produce code that can be executed on the target machine.
Optimization Tools:- These tools help in optimizing the generated
code for efficiency and performance. They can perform various
optimizations such as dead code elimination, loop optimization, and
register allocation.
Debugging Tools:- These tools help in debugging the compiler itself or
the programs that are being compiled. They can provide debugging
information such as symbol tables, call stacks, and runtime errors.
40
Cont.….
Profiling Tools:- These tools help in profiling the compiler or the
compiled code to identify performance bottlenecks and optimize the code
accordingly.
Documentation Tools:- These tools help in generating documentation
for the compiler and the programming language being compiled. They
can generate documentation for the syntax, semantics, and usage of the
language.
Language Support:- Compiler construction tools are designed to
support a wide range of programming languages, including high-level
languages such as C++, Java, and Python, as well as low-level languages
such
41
as assembly language.
Cont.….
42
End.
43