SlideShare a Scribd company logo
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
1
References
"Compilers Principles, techniques, & tools", by Alfred Aho,
second edition, Pearson Addison Wesley, 2007.
Compiler and Translators
Translator is a program that takes as input a program written in one
programming language (the source language) and produces as output
a program in another language (the object or target language).
Types of translator
There are four types of translator according to the source
language as well as target language:
Compiler is a translator that translates a high-level language
program such as FORTRAN, PASCAL, C++
, to low-level language
program such as an assembly language or machine language.
Note: The translation process should also report the presence of
errors in the source program.
Translator
Source
Program
Object
Program
Compiler
High-level
language
Low-level
language
(machine 01)
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
2
Others Translators:
Interpreters are another kind of translators. An Interpreter is a
translator that effectively accepts a source program and executes it
directly, without, producing any object code first. It does this by
fetching the source program instructions one by one, analyzing them
one by one, and then "executing" them one by one.
1- Smaller ( advantage)
2- Slower (disadvantage)
An Assembler is a translator that translates the assembly language
program (mnemonic program) to machine language program.
Pre-processor: is a Translator Translate program written by H.L.L
Into an equivalent program in H.L.L.
Why do we Need Translations?
If there are no translators then we must programming in machine
language. With machine language we must be communicate directly
with a computer in terms of bits, registers, and very primitive
machine operations. Since a machine language program is nothing
more than a sequence of 0's and 1's, programming a complex
Assembler
Assembly
Program
Machine
Program
Low-level language Low-level language
Interpreter
High-level
language
Intermediate code
(
directly execution
)
Pre-processor
High-level
language
High-level
Language
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
3
algorithm in such a language is terribly tedious and fraught with
mistakes.
Why we Write Programs in High-Level Language?
We write programs in high-level language (advantages of high-level
language) because it is:
1) Readability: high-level language will allow programs to be
written in the same ways that used in description of the
algorithms.
2) Portability: High-level languages can be run without
changing on a variety of different computers.
3) Generality: Most high-level languages allow the writing of a
wide variety of programs, thus relieving the programmer of the
need to become expert in many languages.
4) Brevity: Programs expressed in high-level languages are often
considerably shorter (in terms of their number of source lines)
than their low-level equivalents.
5) It's easy in the Error checking process.
The Structure of a Compiler
The process of compilation is so complex to be achieved in
one single step, either from a logical point of view or from an
implementation point of view. For this reason it is partition into a
series of subprocesses called Phases. The typical compiler consists
of several phases each of which passes its output to the next phase
plus symbol table manager and an error handler as shown below:
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
4
Lexical Analyzer (Scanner): It is the first phase of the complier
and also called scanner, which represent the interface between
source program and the compiler. where the Lexical will read the
source program by one letter at a time and decomposed it into group
of letters called TOKEN that is logically interrelated between them.
Phases of a Compiler.
Target Program
Symbol Table
management
Lexical
Analysis
Syntax
Analysis
Semantic
Analysis
Intermediate
Code Generating
Code
Optimization
Code
Generating
Source Program
Error
Handler
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
5
This token represent string of characters that can be processed
together as a logical input.
In others words, lexical Separates characters of the source language
program into groups (sets) that logically belong together. These
groups are called Tokens.
Tokens are Keywords, Identifiers, Operator Symbols, and
Punctuation Symbols. The output of the analyzer is stream of tokens,
which is passed to the syntax analyzer.
Tokens: groups of characters of source language program logically
belong together. For example:
Syntax Analyzer (Parser): Groups tokens together into syntactic
structures called Expressions. Expressions might further be
combined to form Statements. Often the syntactic structure can be
regarded as a Tree (parse tree) whose leaves are the tokens. The
interior nodes of the tree represent strings of tokens that logically
belong together.
Every programming language has rules that prescribe the syntactic
structure of well –formed programs. Syntax analysis takes an out of
lexical analyzer and produce a large tree call parse tree
Semantics:. is a term used to describe "meaning", and so the
constraint analyzer is often called the static semantic analyzer, or
simply the semantic analyzer. Semantic analyzer takes the output of
For, If, Do {Keyword}
X, A1, Num {Identifiers}
3, 44.2,-53 {Constants}
>, +, = {Operator Symbols}
; , . ,' {Punctuation Symbols}
Tokens =
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
6
syntax analyzer and produces another tree.The output of the syntax
analyzer and semantic analyzer phases is sometimes expressed in the
form of a Abstract Syntax Tree (AST). This is a very useful
representation, as it can be used in clever ways to optimize code
generation at a later phase.
It is Recently added a stage to analyze in terms of sentence
meaning, in other words, will reject the sentence in the case of the
discovery of the meaning of error (Note: The sentence can be true in
terms of the syntax but have no meaning).
For example the English sentence : "The man eat the house " This is
in the natural language, put in programming languages can either
write inter correct the hand, but wrong in the sense rules .
Intermediate Code Generator: Create a stream of simple
instructions. Many style of intermediate code are possible. One
common style uses instructions with one operator and a small
number of operands.
This phase is used structures built in the previous stages and convert
them into intermediate codes by using a most famous meta language
such as:
- Postfix notation
- Polish notation
In some versions be enforceable as is the case in the interpreter for
the BASIC language and commands interpreter of the DOS
operating system.
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
7
Code Optimization: Is an optional phase designed to improve the
intermediate code so that the ultimate object program runs faster
and/or takes less space.
Code Generation: In a real compiler this phase takes the output
from the previous phase and produces the object code, by deciding
on the memory locations for data, generating code to access such
locations, selecting registers for intermediate calculations and
indexing, and so on.
Table Management: Portion of the compiler keeps tracks of the
name used by the program and records essential information about
each, such as its type integer, real, … etc. the data structure used to
record this information is called a Symbolic Table.
Error Handler: One of the most important functions of a compiler
is the detection and reporting of errors in the source program. The
error messages should allow the programmer to determine exactly
where the errors have occurred. Whenever a phase of the compiler
discovers an error, it must report the error to the error handler, which
issues an appropriate diagnostic message.
Passes of Compiler
The previous phases showing the main parts of the compiler
theoretically, either in the case of the application must merge
combine more than one phase in round one and this is called one
pass. Each pass will read the output of the previous pass ( except for
the first pass ) that reads the source program.
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
8
In the case of integrating more than one stage in one round will lead
to interference in the work of these stages through the control
structure between these stages.
The influential factor in the number of stages per pass depends on
the structure of the source language , the translator , who owns
multi-pass uses less memory space of a translator who is in round
one single pass. On the other hand , the multi-pass type slower than
the another because each stage reads and writes in the intermediate
file leads to the slow work of the translator . Must be a balance
between the number of stages in a single pass and between the speed
of the compiler. And on this basis can be lexical in one pass with
syntax or separated .
When a lexical phase are separate from syntax pass there are four
pros :
1 - the design of each model separately and thus achieved simplicity.
2 - be efficiency translator largest so that you can manipulate each
part in order to achieve the desired efficiency .
3 - including that the lexical is the interface for that is change when
you change the language and vocabulary development process.
4 – can make symbol table more structure in which we can use it as
input and output in the same time.
Notice: If phase lexical built with syntax In this case, the Lexical in
the form of a subroutine assistant called from the phase of syntax
that followed , and if their separation will store the results of
intermediate file, this file is called symbol table.
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
9
lexical
Syntax
Semantic
Intermediate code
generation
Code Optimization
Code Generation
Intermediate File
Intermediate File
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
11
Example of the Compilation Process
Consider the following sentence is segment from source program:
Id2
+
Id1
:
=
*
Id3 60
Syntax Analysis
Lexical Analysis
Position: = initial + rate * 60
id1 := id2 + id3 * 60
Semantic Analysis
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
11
temp1:=integer to real (60)
temp2:=id3 * temp1
temp3:=id2 + temp2
id1:= temp3
temp1:= id3 * 60.0
id1:= id2+ temp1
movf id3 ,R2
mulf #60.0 ,R2
movf id2 ,R1
addf R1,R2
movf R2,id1
intermediate Analysis
Code generation
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
12
How Lexical Work
Input Buffering:
The lexical analyzer scans the characters of the source
program one at a time to discover tokens; it is desirable for the
lexical analyzer to read its input from an input buffer.
We have two pointers one marks to the beginning of the token
begin discovered. A look-ahead pointer scans a head of the
beginning point, until the token is discovered
For example, if we have the following statement written by HLL
program
Introduction to Compiler
‫الجامع‬
‫ة‬
‫المستنصريه‬
-
13
token type
IF keyword
( punctuation
5 Constant
.EQ. Logical Operator
Sum Identifier
) punctuation
Goto keyword
1000 Constant
Ad

More Related Content

Similar to Compiler design lecture 1 introduction computer science (20)

Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
hari2010
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
abdulbaki3
 
COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis: COMPILER DESIGN- Introduction & Lexical Analysis:
COMPILER DESIGN- Introduction & Lexical Analysis:
Jyothishmathi Institute of Technology and Science Karimnagar
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
Mukesh Tekwani
 
COMPILER DESIGN.docx
COMPILER DESIGN.docxCOMPILER DESIGN.docx
COMPILER DESIGN.docx
Revathiparamanathan
 
2 Programming Language.pdf
2 Programming Language.pdf2 Programming Language.pdf
2 Programming Language.pdf
KINGZzofYouTube
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
Compiler Design Material
Compiler Design MaterialCompiler Design Material
Compiler Design Material
Dr. C.V. Suresh Babu
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
Introduction to high level Computer programming
Introduction  to high level Computer programmingIntroduction  to high level Computer programming
Introduction to high level Computer programming
bilalchuddher
 
Introduction to Compiler Design Structutre, Phases
Introduction to Compiler Design Structutre, PhasesIntroduction to Compiler Design Structutre, Phases
Introduction to Compiler Design Structutre, Phases
Vanitha Alagesan
 
Introduction to compiler design and phases of compiler
Introduction to compiler design  and phases of compilerIntroduction to compiler design  and phases of compiler
Introduction to compiler design and phases of compiler
Ranjeet Reddy
 
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdfCOMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
Abolarinwa
 
lce1 مترجمات.pptx
lce1 مترجمات.pptxlce1 مترجمات.pptx
lce1 مترجمات.pptx
SamiAAli44
 
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
ppt_cd.pptx ppt on phases of compiler of jntuk syllabusppt_cd.pptx ppt on phases of compiler of jntuk syllabus
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
padmajagrandhe1
 
SSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdfSSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdf
JacobDragonette
 
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdfCD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
Padamata Rameshbabu
 
COMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSCOMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONS
sonalikharade3
 
Introduction to compiler development
Introduction to compiler developmentIntroduction to compiler development
Introduction to compiler development
DeepOad
 
Define Computer language, Translator, Standard input out C
Define Computer language, Translator, Standard input out CDefine Computer language, Translator, Standard input out C
Define Computer language, Translator, Standard input out C
Dr Shailendra Bhalawe
 
Cs6660 compiler design
Cs6660 compiler designCs6660 compiler design
Cs6660 compiler design
hari2010
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
Mukesh Tekwani
 
2 Programming Language.pdf
2 Programming Language.pdf2 Programming Language.pdf
2 Programming Language.pdf
KINGZzofYouTube
 
Compiler Construction introduction
Compiler Construction introductionCompiler Construction introduction
Compiler Construction introduction
Rana Ehtisham Ul Haq
 
unit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdfunit1pdf__2021_12_14_12_37_34.pdf
unit1pdf__2021_12_14_12_37_34.pdf
DrIsikoIsaac
 
Introduction to high level Computer programming
Introduction  to high level Computer programmingIntroduction  to high level Computer programming
Introduction to high level Computer programming
bilalchuddher
 
Introduction to Compiler Design Structutre, Phases
Introduction to Compiler Design Structutre, PhasesIntroduction to Compiler Design Structutre, Phases
Introduction to Compiler Design Structutre, Phases
Vanitha Alagesan
 
Introduction to compiler design and phases of compiler
Introduction to compiler design  and phases of compilerIntroduction to compiler design  and phases of compiler
Introduction to compiler design and phases of compiler
Ranjeet Reddy
 
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdfCOMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
COMPUTER SCIENCE COURSE 204 COMPILER CONSTRUCTION,.pdf
Abolarinwa
 
lce1 مترجمات.pptx
lce1 مترجمات.pptxlce1 مترجمات.pptx
lce1 مترجمات.pptx
SamiAAli44
 
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
ppt_cd.pptx ppt on phases of compiler of jntuk syllabusppt_cd.pptx ppt on phases of compiler of jntuk syllabus
ppt_cd.pptx ppt on phases of compiler of jntuk syllabus
padmajagrandhe1
 
SSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdfSSD Mod 2 -18CS61_Notes.pdf
SSD Mod 2 -18CS61_Notes.pdf
JacobDragonette
 
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdfCD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
CD NOTErvvtvvevbvtgv4tgtgtgtgtvefeveS.pdf
Padamata Rameshbabu
 
COMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONSCOMPILER DESIGN OPTIONS
COMPILER DESIGN OPTIONS
sonalikharade3
 
Introduction to compiler development
Introduction to compiler developmentIntroduction to compiler development
Introduction to compiler development
DeepOad
 
Define Computer language, Translator, Standard input out C
Define Computer language, Translator, Standard input out CDefine Computer language, Translator, Standard input out C
Define Computer language, Translator, Standard input out C
Dr Shailendra Bhalawe
 

Recently uploaded (20)

What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdfRanking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Rafael Villas B
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Rococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th centuryRococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th century
Gema
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
Drive Supporter Growth from Awareness to Advocacy with TechSoup Marketing Ser...
TechSoup
 
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE  BY sweety Tamanna Mahapatra MSc PediatricAPGAR SCORE  BY sweety Tamanna Mahapatra MSc Pediatric
APGAR SCORE BY sweety Tamanna Mahapatra MSc Pediatric
SweetytamannaMohapat
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdfRanking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Ranking_Felicidade_2024_com_Educacao_Marketing Educacional_V2.pdf
Rafael Villas B
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
Grade 3 - English - Printable Worksheet (PDF Format)
Grade 3 - English - Printable Worksheet  (PDF Format)Grade 3 - English - Printable Worksheet  (PDF Format)
Grade 3 - English - Printable Worksheet (PDF Format)
Sritoma Majumder
 
dynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south Indiadynastic art of the Pallava dynasty south India
dynastic art of the Pallava dynasty south India
PrachiSontakke5
 
How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18How to Manage Purchase Alternatives in Odoo 18
How to Manage Purchase Alternatives in Odoo 18
Celine George
 
Rococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th centuryRococo versus Neoclassicism. The artistic styles of the 18th century
Rococo versus Neoclassicism. The artistic styles of the 18th century
Gema
 
Tax evasion, Tax planning & Tax avoidance.pptx
Tax evasion, Tax  planning &  Tax avoidance.pptxTax evasion, Tax  planning &  Tax avoidance.pptx
Tax evasion, Tax planning & Tax avoidance.pptx
manishbaidya2017
 
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFAExercise Physiology MCQS By DR. NASIR MUSTAFA
Exercise Physiology MCQS By DR. NASIR MUSTAFA
Dr. Nasir Mustafa
 
Computer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issuesComputer crime and Legal issues Computer crime and Legal issues
Computer crime and Legal issues Computer crime and Legal issues
Abhijit Bodhe
 
Herbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptxHerbs Used in Cosmetic Formulations .pptx
Herbs Used in Cosmetic Formulations .pptx
RAJU THENGE
 
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Redesigning Education as a Cognitive Ecosystem: Practical Insights into Emerg...
Leonel Morgado
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Ad

Compiler design lecture 1 introduction computer science

  • 1. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 1 References "Compilers Principles, techniques, & tools", by Alfred Aho, second edition, Pearson Addison Wesley, 2007. Compiler and Translators Translator is a program that takes as input a program written in one programming language (the source language) and produces as output a program in another language (the object or target language). Types of translator There are four types of translator according to the source language as well as target language: Compiler is a translator that translates a high-level language program such as FORTRAN, PASCAL, C++ , to low-level language program such as an assembly language or machine language. Note: The translation process should also report the presence of errors in the source program. Translator Source Program Object Program Compiler High-level language Low-level language (machine 01)
  • 2. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 2 Others Translators: Interpreters are another kind of translators. An Interpreter is a translator that effectively accepts a source program and executes it directly, without, producing any object code first. It does this by fetching the source program instructions one by one, analyzing them one by one, and then "executing" them one by one. 1- Smaller ( advantage) 2- Slower (disadvantage) An Assembler is a translator that translates the assembly language program (mnemonic program) to machine language program. Pre-processor: is a Translator Translate program written by H.L.L Into an equivalent program in H.L.L. Why do we Need Translations? If there are no translators then we must programming in machine language. With machine language we must be communicate directly with a computer in terms of bits, registers, and very primitive machine operations. Since a machine language program is nothing more than a sequence of 0's and 1's, programming a complex Assembler Assembly Program Machine Program Low-level language Low-level language Interpreter High-level language Intermediate code ( directly execution ) Pre-processor High-level language High-level Language
  • 3. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 3 algorithm in such a language is terribly tedious and fraught with mistakes. Why we Write Programs in High-Level Language? We write programs in high-level language (advantages of high-level language) because it is: 1) Readability: high-level language will allow programs to be written in the same ways that used in description of the algorithms. 2) Portability: High-level languages can be run without changing on a variety of different computers. 3) Generality: Most high-level languages allow the writing of a wide variety of programs, thus relieving the programmer of the need to become expert in many languages. 4) Brevity: Programs expressed in high-level languages are often considerably shorter (in terms of their number of source lines) than their low-level equivalents. 5) It's easy in the Error checking process. The Structure of a Compiler The process of compilation is so complex to be achieved in one single step, either from a logical point of view or from an implementation point of view. For this reason it is partition into a series of subprocesses called Phases. The typical compiler consists of several phases each of which passes its output to the next phase plus symbol table manager and an error handler as shown below:
  • 4. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 4 Lexical Analyzer (Scanner): It is the first phase of the complier and also called scanner, which represent the interface between source program and the compiler. where the Lexical will read the source program by one letter at a time and decomposed it into group of letters called TOKEN that is logically interrelated between them. Phases of a Compiler. Target Program Symbol Table management Lexical Analysis Syntax Analysis Semantic Analysis Intermediate Code Generating Code Optimization Code Generating Source Program Error Handler
  • 5. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 5 This token represent string of characters that can be processed together as a logical input. In others words, lexical Separates characters of the source language program into groups (sets) that logically belong together. These groups are called Tokens. Tokens are Keywords, Identifiers, Operator Symbols, and Punctuation Symbols. The output of the analyzer is stream of tokens, which is passed to the syntax analyzer. Tokens: groups of characters of source language program logically belong together. For example: Syntax Analyzer (Parser): Groups tokens together into syntactic structures called Expressions. Expressions might further be combined to form Statements. Often the syntactic structure can be regarded as a Tree (parse tree) whose leaves are the tokens. The interior nodes of the tree represent strings of tokens that logically belong together. Every programming language has rules that prescribe the syntactic structure of well –formed programs. Syntax analysis takes an out of lexical analyzer and produce a large tree call parse tree Semantics:. is a term used to describe "meaning", and so the constraint analyzer is often called the static semantic analyzer, or simply the semantic analyzer. Semantic analyzer takes the output of For, If, Do {Keyword} X, A1, Num {Identifiers} 3, 44.2,-53 {Constants} >, +, = {Operator Symbols} ; , . ,' {Punctuation Symbols} Tokens =
  • 6. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 6 syntax analyzer and produces another tree.The output of the syntax analyzer and semantic analyzer phases is sometimes expressed in the form of a Abstract Syntax Tree (AST). This is a very useful representation, as it can be used in clever ways to optimize code generation at a later phase. It is Recently added a stage to analyze in terms of sentence meaning, in other words, will reject the sentence in the case of the discovery of the meaning of error (Note: The sentence can be true in terms of the syntax but have no meaning). For example the English sentence : "The man eat the house " This is in the natural language, put in programming languages can either write inter correct the hand, but wrong in the sense rules . Intermediate Code Generator: Create a stream of simple instructions. Many style of intermediate code are possible. One common style uses instructions with one operator and a small number of operands. This phase is used structures built in the previous stages and convert them into intermediate codes by using a most famous meta language such as: - Postfix notation - Polish notation In some versions be enforceable as is the case in the interpreter for the BASIC language and commands interpreter of the DOS operating system.
  • 7. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 7 Code Optimization: Is an optional phase designed to improve the intermediate code so that the ultimate object program runs faster and/or takes less space. Code Generation: In a real compiler this phase takes the output from the previous phase and produces the object code, by deciding on the memory locations for data, generating code to access such locations, selecting registers for intermediate calculations and indexing, and so on. Table Management: Portion of the compiler keeps tracks of the name used by the program and records essential information about each, such as its type integer, real, … etc. the data structure used to record this information is called a Symbolic Table. Error Handler: One of the most important functions of a compiler is the detection and reporting of errors in the source program. The error messages should allow the programmer to determine exactly where the errors have occurred. Whenever a phase of the compiler discovers an error, it must report the error to the error handler, which issues an appropriate diagnostic message. Passes of Compiler The previous phases showing the main parts of the compiler theoretically, either in the case of the application must merge combine more than one phase in round one and this is called one pass. Each pass will read the output of the previous pass ( except for the first pass ) that reads the source program.
  • 8. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 8 In the case of integrating more than one stage in one round will lead to interference in the work of these stages through the control structure between these stages. The influential factor in the number of stages per pass depends on the structure of the source language , the translator , who owns multi-pass uses less memory space of a translator who is in round one single pass. On the other hand , the multi-pass type slower than the another because each stage reads and writes in the intermediate file leads to the slow work of the translator . Must be a balance between the number of stages in a single pass and between the speed of the compiler. And on this basis can be lexical in one pass with syntax or separated . When a lexical phase are separate from syntax pass there are four pros : 1 - the design of each model separately and thus achieved simplicity. 2 - be efficiency translator largest so that you can manipulate each part in order to achieve the desired efficiency . 3 - including that the lexical is the interface for that is change when you change the language and vocabulary development process. 4 – can make symbol table more structure in which we can use it as input and output in the same time. Notice: If phase lexical built with syntax In this case, the Lexical in the form of a subroutine assistant called from the phase of syntax that followed , and if their separation will store the results of intermediate file, this file is called symbol table.
  • 9. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 9 lexical Syntax Semantic Intermediate code generation Code Optimization Code Generation Intermediate File Intermediate File
  • 10. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 11 Example of the Compilation Process Consider the following sentence is segment from source program: Id2 + Id1 : = * Id3 60 Syntax Analysis Lexical Analysis Position: = initial + rate * 60 id1 := id2 + id3 * 60 Semantic Analysis
  • 11. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 11 temp1:=integer to real (60) temp2:=id3 * temp1 temp3:=id2 + temp2 id1:= temp3 temp1:= id3 * 60.0 id1:= id2+ temp1 movf id3 ,R2 mulf #60.0 ,R2 movf id2 ,R1 addf R1,R2 movf R2,id1 intermediate Analysis Code generation
  • 12. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 12 How Lexical Work Input Buffering: The lexical analyzer scans the characters of the source program one at a time to discover tokens; it is desirable for the lexical analyzer to read its input from an input buffer. We have two pointers one marks to the beginning of the token begin discovered. A look-ahead pointer scans a head of the beginning point, until the token is discovered For example, if we have the following statement written by HLL program
  • 13. Introduction to Compiler ‫الجامع‬ ‫ة‬ ‫المستنصريه‬ - 13 token type IF keyword ( punctuation 5 Constant .EQ. Logical Operator Sum Identifier ) punctuation Goto keyword 1000 Constant