0% found this document useful (0 votes)
15 views

Chapter 1 Introduction

Uploaded by

shemse
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Chapter 1 Introduction

Uploaded by

shemse
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 43

Chapter 1

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.

 Compiler is used to show errors to the programmer.


 The main purpose of compiler is to change the code written in one
language without changing the meaning of the program.
 When you execute a program which is written in HLL programming
language then it executes into two parts.

 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.…

 However writing Programs in Assembler is tedious therefore


4 programmers design another Language translator as follows.
Cont.…

 Languages written in Php, Rubby, JavaScript and Python requires


Interpreter as language translator.
 However programs written in C, C++ and Erlang can be converted directly
into Machine codes.
 Compilers come into picture since compilers tend to be faster and more
5
efficient than Interpreters .
Internal Architecture of Language Translator

 Example:- Consider conversions of the following source code into


machines code.

6
Cont.…

 Preprocessor:- Converts High Level Languages into pure high level


language by removing comments and link it into preprocessor
directives as shown below.

7
Cont.…

 Compiler:- Converts Pure high level Languages into Assembly


Languages.

8
Cont.…

 Assembler:- is responsible to convers Assembly Languages to


relocatable machine codes.

9
Cont.…

 Linker/Loader:- Generates Absolute Machine Codes which is


actually loaded into computer RAM and ready for execution.

10
Phases of a Compiler

 The compilation process contains


the sequence of various phases.
Each phase takes source program
in one representation and produces
output in another representation.
 Each phase takes input from its
previous stage.

Fig: phases of compiler


11
Cont.…

12
Cont.…

 Information's gathered in analysis phase is stored in Symbol table and


used by Synthesis phase.
 Error handlers are used for error detections and recovery.
13
Various Phases of Compiler

 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.…

 Intermediate Code Generation:-


 In the intermediate code generation, compiler generates the source
code into the intermediate code.
 Intermediate code is generated between the high-level language and
the machine language. The intermediate code should be generated in
such a way that you can easily translate it into the target machine
code.

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

 And the assembler is basically able to convert these mnemonics in


Binary code. Here, these mnemonics also depend upon the architecture
of the machine.

 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 takes a large


amount of time to analyze  An interpreter takes less
the entire source code but amount of time to analyze
the overall execution time the source code but the
of the program is overall execution time of
comparatively
32
faster. the program is slower.
Cont.…
 The compiler generates
the error message only  Its Debugging is easier as
after scanning the whole
it continues translating the
program, so debugging is
program until the error is
comparatively hard as the
met.
error can be present
anywhere in the program.
 It requires less memory
 The compiler requires a lot
than a compiler because
of memory for generating
no object code is
object codes.
generated.
 Generates intermediate  No intermediate object
object code. code is generated.
 The interpreter is a little
 For Security purpose
vulnerable in case of
compiler is more useful.
33
security.
Compiler Construction Tools

 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.….

 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 automaton to recognize the regular expression.
Example: Lex
35
Cont.….

 Syntax Directed Translation Engines (SDTE):- It generates


intermediate code with three address format from the input that
consists of a parse tree. These engines have routines to traverse the
parse tree and then produces the intermediate code. In this, each node
of the parse tree is associated with one or more translations.
36
Cont.….

 Automatic Code Generators:- It generates the machine language for


a target machine. Each operation of the intermediate language is
translated using a collection of rules and then is taken as an input by
the code generator. A template matching process is used. An
intermediate language statement is replaced by its equivalent machine
language statement using templates.
 Data-flow Analysis Engines:- It is used in code optimization. Data
flow analysis is a key part of the code optimization that gathers the
information, that is the values that flow from one part of a program to
another.
37
Cont.….

 Compiler Construction Toolkits:- It provides an integrated set of


routines that aids in building compiler components or in the
construction of various phases of compiler.

38
Features of Compiler Construction Tools

 Lexical Analyzer Generator:- This tool helps in generating the lexical


analyzer or scanner of the compiler. It takes as input a set of regular
expressions that define the syntax of the language being compiled and
produces a program that reads the input source code and tokenizes it
based on these regular expressions.
 Parser Generator:- This tool helps in generating the parser of the
compiler. It takes as input a context-free grammar that defines the
syntax of the language being compiled and produces a program that
parses the input tokens and builds an abstract syntax tree.

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

 Cross-Platform Support:- Compiler construction tools may be


designed to work on multiple platforms, such as Windows, Mac, and
Linux.
 User Interface:- Some compiler construction tools come with a user
interface that makes it easier for developers to work with the compiler
and its associated tools.

42
End.

43

You might also like