0% found this document useful (0 votes)
2 views57 pages

Introduction To Compiler

This document provides an introduction to compilers, detailing their functions as translators that convert source code from one language to another. It outlines the different types of translators, including compilers, interpreters, and assemblers, and describes the phases of compilation, such as lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. Additionally, it discusses the roles of various components like the symbol table and error detection mechanisms within the compilation process.

Uploaded by

strikerpatel3336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views57 pages

Introduction To Compiler

This document provides an introduction to compilers, detailing their functions as translators that convert source code from one language to another. It outlines the different types of translators, including compilers, interpreters, and assemblers, and describes the phases of compilation, such as lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. Additionally, it discusses the roles of various components like the symbol table and error detection mechanisms within the compilation process.

Uploaded by

strikerpatel3336
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Department of CE

CD : COMPILER DESIGN
Unit no : 1
Introduction to Introduction to
Compilers
Compilers (01CE0714)

Prof. Dhara Joshi


Outline :
Translator
Department of CE
Compiler, Interpreter, Assembler
Cousins of Compiler Unit no : 1
Phases of Compiler Introduction to
Compilers
Grouping of Phases
(01CE0714)
Types of Compiler
Compiler construction tools
Pass and Phase
Prof. Dhara Joshi
What is
Compiler?
Why we
Basic need it?
Terminology
Translator
Translator :
• A translator is a program which takes one program
(written in one language) as input and converts it
into another language (Target language).
• Input Language is Source Language and Output
Language is Target Language.
Translator • There are Three types of Translator :
1) Compiler
2) Interpreter
3) Assembler
 It also detects and reports error during translation.
Compiler :
 Compiler is a program that reads a program written in
one language that means source language and
translates it into equivalent program into another
language that is target language.
 Compiler is a program which translates higher level
language into functionally equivalent lower level
Compiler language.
 It also detects and reports error.

Source Target
Compiler
Program Program
Error
Interpreter :
 Interpreter is a translator which is used to convert
programs in high-level language to low-level language.
 Interpreter translates line by line and reports the error
once it encountered during the translation process.

Interpreter Source Target


Interpreter
Program Program
Error
Assembler :
 An assembler is a translator used to translate
assembly language to machine language.
 An assembler translates a low-level language, an
assembly language to an even lower-level language,
which is the machine code. The machine code can be
directly understood by the CPU.
Assembler
Source Target
Assembler
Program Program
Error
Compiler Interpreter
It takes whole program as input It takes single instruction as input
It generates intermediate object It does not generate any
code intermediate object code
Memory requirement is more due Memory requirement is less
to creation of object code. (memory efficient) as it does not
Compiler create intermediate object code.
It takes large amount of time to It takes less amount of time to
Vs analyze the source code but overall analyze the source code but overall
Interpreter execution is comparatively faster.
Compilation is done before
execution is slower.
Compilation and execution take
execution. place simultaneously.
Display all errors after entire Display errors of each line one by
program is checked. one.
Ex : C , C ++ Ex : BASIC , Python
Compiler Assembler
Input for compiler is Pre processed Input for Assembler is Assembly
Source code. language code.
It generates assembly language It generates the relocatable
code or directly the executable machine code.
code.

Compiler There are 6 Phases of compiler It makes Two passes over given
input.
Vs Ex : C , C ++ Ex : 8085 , 8086
Assembler
HLL

Pre Processor
Language
Processing Pure HLL

System Compiler
/ Translation Assembly Language
Processor
/ Context of Assembler
Compiler Relocatable Machine code
/Cousins of Linker &
Compiler Loader
Library ,
Relocatable object files

Absolute Machine code


 There are three cousins of Compiler
1) Preprocessor
Language 2) Assembler
Processing 3) Linker and Loader
System
/ Translation
Processor
/ Context of
Compiler
/Cousins of
Compiler
1) Preprocessor
 Preprocessor produces input to compilers.
Language  They may perform the following functions
Processing  Macro Processing : A preprocessor may allow a user to
define macros that are short hands for longer constructs.
System File Inclusion : A preprocessor may include header files
/ Translation into program text.

Processor “Rational” Preprocessor : These processors augment older


languages with more modern flow of control and data
/ Context of structuring facilities. (Built in Macro for construct like While
or If Statement)
Compiler  Language Extension : These processor attempt to add
/Cousins of capabilities to the language by what amount to built in
macros. the language equal is a database query language
Compiler embedded in C. statement beginning with ## are taken by
preprocessor to be database access statement unrelated to C
and translated into procedure call on routines that perform
database access.
1) Preprocessor
 Macro processor deal with two kinds of statement
Language Macro Definition
Processing Macro Use
System
/ Translation
Processor
/ Context of
Compiler
/Cousins of
Compiler
 Formal Parameters
 Actual Parameters
2) Assembler
 An assembler is a translator used to translate
Language assembly language to machine language.
Processing  An assembler translates a low-level language, an
System assembly language to an even lower-level language,
which is the machine code. The machine code can be
/ Translation directly understood by the CPU.
Processor
/ Context of Source Target
Assembler
Compiler Program Program
/Cousins of Error
Compiler
2) Assembler
 Assembly code is mnemonic version of machine code,
Language in which names are used instead of binary codes for
Processing operation and names are also given to memory address.

System
/ Translation
Processor
/ Context of
Compiler
/Cousins of
Compiler
3) Linker and Loader
 A program called loader performs the two functions of
Language loading and link editing
Processing  The process of loading consist of taking relocatable
System machine code , altering the relocatable address and
placing the altered instructions and data in memory at
/ Translation the proper location.
Processor  Linker allows us to make a single program from several
/ Context of files of relocatable machine code.
 Types of Linking : Static and Dynamic
Compiler
 Types of Loader : Compile and Go loader, Absolute
/Cousins of Loader, Relocating Loader.
Compiler
Lexical Analyzer

Phases of
Compiler
1) Lexical Analysis
 It is also called Linear analysis or scanning.
 In this phase, input character from source code is read
from left to right and then break into stream of units.
Phases of These units are called tokens.
Compiler  Sequence of characters having a collective meaning is
/ Analysis – called Token.
Synthesis  Tokens can be categorized into identifiers, constants,
literals, keywords, operators, delimeters etc.
Model of
 So, Token is the smallest meaningful entities of program
Compilation are produced as output.
1) Lexical Analysis
Example : position = initial + rate * 60
id1 = id2 + id3 * 60

Phases of
Compiler
/ Analysis – Position Identifier 1

Synthesis =
Initial
Equal to operator
Identifier 2
Model of + Addition Operator

Compilation Rate
*
Identifier 3
Multiplication Operator
60 Constant
Lexical Analyzer

Syntax Analyzer

Phases of
Compiler
2) Syntax Analysis
 It is also called Hierarchical analysis or Parsing.
 It determines if the sentence formed from the words
are syntactically (grammatically) correct.
Phases of
 It creates syntax tree from generated tokens if the code
Compiler is error free.
/ Analysis –  Syntax tree consist operators as internal node and
Synthesis operands as leaf node.
Model of  This phase check each and every line and try to detect
errors if it is grammatically (syntax wise) not correct.
Compilation
2) Syntax Analysis
position = initial + rate * 60
Three Operators : = + *
Phases of position = initial + rate * 60
Compiler position = initial + rate * 60
/ Analysis – position = initial + rate * 60
Synthesis
Model of
Compilation
Lexical Analyzer

Syntax Analyzer

Phases of Semantic
Analyzer
Compiler
3) Semantic Analysis
 It determines meaning of string
 In semantic analysis various operations are performed
like
Phases of
 Performing check ; whether operator have compatible
Compiler arguments or not (type checking) , matching of
/ Analysis – parenthesis, scope of operation etc.
Synthesis  Ensuring that components of a program fits together
meaningfully.
Model of
Compilation
3) Semantic Analysis
position = initial + rate * 60

Phases of
Compiler
/ Analysis –
Synthesis
Model of
Compilation
Lexical Analyzer

Syntax Analyzer

Phases of Semantic
Analyzer
Compiler
Intermediate
Code Generator
4) Intermediate Code Generator
 Intermediate code generation should have two
properties :
it should be easy to produce,
Phases of
easy to translate.
Compiler
 Intermediate code is called “Three Address Code”.
/ Analysis –
 It is called three address code as it maximum consist
Synthesis three operands.
Model of
Compilation
4) Intermediate Code Generator
Example : position = initial + rate * 60
t1 = inttoreal(60)
Phases of t2 = rate * t1
Compiler t3 = initial + t2
/ Analysis – position = t3
Synthesis
Model of
Compilation
Lexical Analyzer

Syntax Analyzer

Phases of Semantic
Analyzer
Compiler
Intermediate
Code Generator

Code Optimizer
5) Code Optimization
 This phase improves the intermediate code, in such a
way that a machine code can be produced, which
occupies less memory space and less execution time
Phases of without changing the functionality or correctness of
program.
Compiler
 Example :
/ Analysis –
t1 = rate * 60.00
Synthesis
position = initial + t1
Model of
Compilation
Lexical Analyzer

Syntax Analyzer

Semantic

Phases of Analyzer

Compiler Intermediate
Code Generator

Code Optimizer

Code Generator
6) Code Generation
 In Code generation phase the target code gets
generated.
 In this phase intermediate (optimized) code is
Phases of translated into a sequence of machine instructions that
Compiler perform the same operation.
/ Analysis –  Example :
Synthesis MOVF id3,R1 position = initial + rate * 60
id 1 id2 id3

Model of MULF #60.00, R1 R1 = id3 (rate)


R1 = 60.00 * R1
Compilation MOVF id2, R2 R2 = id2 (initial)
R1 = R2 + R1
ADDF R2, R1 id1(position) = R1

MOVF R1, id1


Lexical Analyzer

Syntax Analyzer

Semantic

Phases of Symbol Table


Analyzer
Error

Compiler Manager
Intermediate
handler

Code Generator

Code Optimizer

Code Generator
Lexical Analyzer
Analysis Phase

Syntax Analyzer

Semantic

Phases of Symbol Table


Analyzer
Error

Compiler Manager
Intermediate
handler

Code Generator

Code Optimizer
Synthesis Phase

Code Generator
 There are two parts of Compilation Process :
1) Analysis Phase
It breaks up source program into small pieces and
create an intermediate representation of source
Phases of program.
Compiler 2) Synthesis Phase
/ Analysis – It constructs the desired target program from
Synthesis intermediate representation.
Model of
Compilation
1) Analysis Phase
Consist of Three Sub Phases :
 Lexical analysis
Phases of  Syntax analysis
Compiler  Semantic analysis
/ Analysis –
Synthesis 2) Synthesis Phase :
Model of Consist of Three Sub Phases :
Compilation  Intermediate Code generation
 Code Optimization
 Code Generation
Symbol Table :
 A symbol table is data structure used by language
translators.
 In order to keep track of identifiers used by program,
Phases of compiler maintains the record of the same with their
Compiler various attributes.
/ Analysis –  Symbol table stores information for different entities :
Identifier, Function, Procedure, Constant , Label Name,
Synthesis Compiler generated temporaries etc.
Model of
Compilation
Error Detection and Recovery :
 Each phase can encounter errors, However after
detecting an error a phase must some how deal with
that error. So that Compilation can proceed, allowing
Phases of further errors in the source program to be detected.
Compiler  Lexical Analyzer can detect errors where the character
remaining in the input do not form any token of the
/ Analysis – language.
Synthesis  Errors where the token stream violates the structure
Model of rule (syntax) of the language are determined by syntax
analysis phase.
Compilation
 During Semantic analysis, the compiler tries to detect
constructs that have the right syntactic structure but no
meaning to the operation involved.
a=b+c*60
Intermediate code generator

Lexical analyzer
t1:= inttofloat (60)
Id1=id2+id3*60 t2:= id3 * t1
t3:= id2 + t2

Phases of
id1:= t3
Syntax analyzer

Compiler =
+
Code optimizer

/ Analysis – id1
id2 *
Synthesis id3 60
t1:= id3 * 60.0
id1:= id2 + t1
Model of Semantic analyzer
Compilation =
Code generator

+
id1
MOVEF id3, R2
id2 *
MULF #60.0,R2
id3 Int to float MOVF id2, R1
ADDF R2,R1
MOVF R1,id1
60
Example :
What will be the output of each phase for the following
statement?
Statement : x=a+b*c

Practice Time
Front End Back End

Grouping of • Dependent : • Dependent : Target


Source Language Machine
Phases
• Independent :
• Independent : Source Language
Target Machine
Front End:
 Front end consist of those phase, that depends
preliminary on the source language & are
independent of the target machine.
 It includes Lexical analysis, Syntax analysis, Semantic
analysis, Intermediate code generation and Creation
Grouping of of Symbol table.
Phases  A certain amount of code optimization can be done
by the front end as well
 The front end also includes error handling that goes
along with each of these phases.
Back End:
 Back includes those portion of compiler that depend
on the target machine and do not depend on source
language.
 Back end includes code optimization and code
generation with necessary error handling and
Grouping of symbol table operation.
Phases

It has become fairly routine to take front end of a


compiler & redo its back end to produce a compiler for
the same source language on a different machine.
 One Complete scan of Source program is called Pass.
 Difficulty in single pass :
 Forward Reference : A forward reference means
variable or label is referenced before its declared.
 It leads to Multi-Pass model.
Pass – 1 : Perform analysis of source program and note
relevant information
Pass of Pass – 2 : Generate target code using information
Compiler noted in pass-1.
START 100 (AD,01) (C,200)
READ A (IS,09) (S,01)
LOOP MOVER AREG , A ..............
........ ...............
........ ...............
BC GT , LOOP ...............
STOP (IS , 00)

A DS 1 (DL,02) (C,1)
.......
Pass Phase
Various phases are logically grouped The process of compilation is carried
together to form a pass. out in various step is called Phase.
Require Less Memory Require More Memory
Execution is faster Execution is slower
Less memory operation(read/write) More memory operations to be
to be performed performed

Pass Vs Phase 2 Passes of Compiler 6 Phases of Compiler


Native Compiler Cross Compiler

Types of Source to Source


Just in Time Compiler
Compiler Compiler

Incremental Compiler Parallelizing Compiler


Native Compiler :
 The compiler used to Compile a source code for
same type of platform only.
 The output generated by this type of compiler can
only be run on the same type of computer system and
OS that the compiler itself runs on.
Types of
Cross Compiler :
Compiler
 The compiler used to compile a source code for
different kinds platform.
 A Cross compiler is a compiler capable of creating
executable code for a platform other than one on
which compiler is running.
Source to Source Compiler :
 The compiler that takes high-level language code as
input and outputs source code of another high- level
language only.
 Unlike other compilers which convert high level
language into low level machine language, it can take
Types of up a code written in Pascal and can transform it into
Compiler C-conversion of one high level language into another
high level language having same type of abstraction .
 Thus, it is also known as transpiler .
Just in Time Compiler :
 The compiler which converts a code into machine
code after the program starts execution.
 Convert .java file into .class file.

Types of
Compiler
Incremental Compiler :
 The compiler which compiles only the changed lines
from the source code and update the object code.
Parallelizing Compiler :
 A Compiler which is specially designed to run in
Types of parallel computer architecture is known as
parallelizing compiler.
Compiler
int a=0;
int b=1;
printf("Enter Input");
Scanf();

if(a==10)
printf();
Compiler Construction Tools :
1) Scanner Generators
2) Parser Generators
3) Syntax – directed translation engines
Compiler 4) Automatic Code Generators
Construction 5) Data Flow Engine
Tools
Scanner generators: This tool takes regular expressions as
input. For example LEX for Unix Operating System.
Parser generators: A parser generator takes a grammar as
input and automatically generates source code which can
parse streams of characters with the help of a grammar
Syntax-directed translation engines: These software
Compiler tools offer an intermediate code by using the parse tree. It
has a goal of associating one or more translations with each
Construction node of the parse tree.
Tools Automatic code generators: Takes intermediate code and
converts them into Machine Language
Data-flow engines: This tool is helpful for code
optimization. Here, information is supplied by user and
intermediate code is compared to analyze any relation. It is
also known as data-flow analysis. It helps you to find out
how values are transmitted from one part of the program to
another part.
 Compiler itself must be bug free.
 It must generate correct machine code.
 Generated machine code must run fast (Execution
speed).
 Compilation time must be less.
Qualities of  Compilation must be portable.
Compiler  It must be print good diagnostics and Error message.
 Generated code must work well with existing debugger.
 It must have consistent and Predictable optimization.
1. Aho, Lam, Sethi, and Ullman, Compilers: Principles,
Techniques and Tools,SecondEdition, Pearson,
2014
2. D. M. Dhamdhere: Systerm Programming, Mc Graw
Hill Publication
3. Dick Grune, Henri E. Bal, Jacob, Langendoen:
References Modern Compiler Design, Wiley India Publication
1) Develop Analysis – Synthesis model of Compiler for
a = b + c * 20. Write down explanation and output
for each phase of compiler.
2) Difference between Compiler and Interpreter.
3) Explain Cousins of compiler.
4) Give the name of Front End and Back End with
explanation, and take one example to show the
MU Questions output of the every phase.
5) List out different types of compiler and explain any
two of them in detail.
6) Explain the Language processor in detail.
7) Define the term: Compiler, Interpreter, Assembler,
Translator
Department of CE

Thanks
Unit no : 1
Introduction to
Compilers
(01CE0714)

Prof. Dhara Joshi

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy