SlideShare a Scribd company logo
1 of 21
Download to read offline
Compilers Design
Course
Ramadan Babers
Compilers Design – Lec2
Faculty of Science Helwan University
1st Term – 2017/2018
Phases of Compiler
2
Lexical Analyzer (Scanner)
reads a stream of characters and puts them
together into some meaningful (with respect to
the source language) units called tokens.
Lexical Analyzer
(Scanner)Character
Stream
Token
Stream
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
3
Lexical Analyzer (Continue)
 Token
 Token (terminal, attribute)
 Symbol Table (reserved words)
Lexical Analyzer
(Scanner)Character
Stream
Token Stream
(terminal, attribute)
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
4
Syntax Analyzer (Parsing)
• Takes a stream of tokens from the scanner,
and
• Determines if the syntax (structure) of the
program is correct according to the
(context-free) grammar of the source
language.
Syntax Analyzer
Token Syntax Tree
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
5
Syntax Analyzer (Parsing) continue
• It generates a data structure, called a
parse tree or an abstract syntax tree,
which describes the syntactic structure of
the program.
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
6
Syntax Analyzer (Parsing) continue
Ex:
 Statement:
id=id op id
 Statement:
id=id [num]
 Statement:
if (id op num) { } / if(xy==20) { }
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
7
Semantic Analyzer
 Statement:
id op id op id
x = y + z (semantically correct)
But if
Int = float + fn (semantically incorrect)
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
8
Intermediate Code Generator
CD-Course-FSH 1st Term 2017-2018
Intermediate CodeSource Code Machine Code
Intermediate Code
C
Intel
Pascal
Basic
Mac
Phases of Compiler
9
Code Optimizer
Code optimizing means
 Faster,
 Shorter,
 Target code consumes less power.
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
10
Example:
position := initial + rate * 60
would be grouped into the following tokens;
1. The identifier position
2. The assignment symbol :=
3. The identifier initial
4. The plus sign
5. The identifier rate.
6. The multiplication sign.
7. The number 60
LexicalAnalyzer
Syntax Analyzer
CD-Course-FSH 1st Term 2017-2018
Phases of Compiler
11
Example:
position := initial + rate * 60
<id, position> <=> <id, initial> <+> <id, rate> <*> <num, 60>
<id, 1> <=> <id, 2> <+> <id, 3> <*> <num, 60>
Lexical Analyzer
CD-Course-FSH 1st Term 2017-2018
1 Position ----
2 Initial ----
3 rate ----
Phases of Compiler
12
Example:
position := initial + rate * 60
Syntax Analyzer
CD-Course-FSH 1st Term 2017-2018
=
<id,1> +
<id,2>
<id,3>
*
60
Phases of Compiler
13
Example:
position := initial + rate * 60
Semantic Analyzer
CD-Course-FSH 1st Term 2017-2018
=
<id,1> +
<id,2>
<id,3>
*
int to float
Phases of Compiler
14
Example:
position := initial + rate * 60
Intermediate Code
CD-Course-FSH 1st Term 2017-2018
t1= int to float (60)
t2= id3 * t1
t3= id2 + t2
id1=t3
Phases of Compiler
15
Example:
position := initial + rate * 60
Code Optimizer
CD-Course-FSH 1st Term 2017-2018
t1= int to float (60)
t2= id3 * t1
t3= id2 + t2
id1=t3
t1= id3 * 60.0
id1= id2 + t1
Phases of Compiler
16
Example:
position := initial + rate * 60
LDF R2 , id3
MULF R2 , R2 , #60.0
LDF R1 , id2
ADDF R1 , R1 , R2
STF id1 , R1
Code Generator
CD-Course-FSH 1st Term 2017-2018
LDF: Load float
MULF: Multi float
ADDF: Add float
STF: Store float
17
Chapter 2
CD-Course-FSH 1st Term 2017-2018
A Simple Syntax-Directed
Translator
Introduction
18
CD-Course-FSH 1st Term 2017-2018
Syntax Tree
Generate
Parsing
Group of production
Terminal
Grammar
Nonterminal
Introduction
19
CD-Course-FSH 1st Term 2017-2018
Syntax
Parser Grammar
Token
Introduction
20
Example:
Code required for the process: 5+6+7
CD-Course-FSH 1st Term 2017-2018
exp exp + exp
exp 0
1
2
---
9
exp num + exp
num 0
1
2
---
9
exp exp + num
exp num
num 0
1
2
---
9
Introduction
21
Example:
Code required for the process: A=B=C=-----=Z
CD-Course-FSH 1st Term 2017-2018
exp L = exp
exp L
L A
B
C
---
Z
exp exp = L
exp L
L A
B
C
---
Z

More Related Content

What's hot

Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design BasicsAkhil Kaushik
 
C program to add two numbers
C program to add two numbers C program to add two numbers
C program to add two numbers mohdshanu
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Bhavin Darji
 
MIPS Assembly Language I
MIPS Assembly Language IMIPS Assembly Language I
MIPS Assembly Language ILiEdo
 
Programing Style
Programing StylePrograming Style
Programing StyleSumit Kumar
 
Factorial Program in C
Factorial Program in CFactorial Program in C
Factorial Program in CHitesh Kumar
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programmingMukesh Tekwani
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compilerIffat Anjum
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocationRohit Shrivastava
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.pptvenkatapranaykumarGa
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solutionAnimesh Chaturvedi
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - IntroductionMuhammad Sanaullah
 
03 mips assembly language
03 mips assembly language03 mips assembly language
03 mips assembly languageMuberra Duman
 

What's hot (20)

Compiler Design Basics
Compiler Design BasicsCompiler Design Basics
Compiler Design Basics
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
C program to add two numbers
C program to add two numbers C program to add two numbers
C program to add two numbers
 
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
Overview of Language Processor : Fundamentals of LP , Symbol Table , Data Str...
 
Input buffering
Input bufferingInput buffering
Input buffering
 
MIPS Assembly Language I
MIPS Assembly Language IMIPS Assembly Language I
MIPS Assembly Language I
 
Programing Style
Programing StylePrograming Style
Programing Style
 
Factorial Program in C
Factorial Program in CFactorial Program in C
Factorial Program in C
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
BNF & EBNF
BNF & EBNFBNF & EBNF
BNF & EBNF
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
 
16 dynamic-memory-allocation
16 dynamic-memory-allocation16 dynamic-memory-allocation
16 dynamic-memory-allocation
 
Aula14 - Funções em PHP
Aula14 - Funções em PHPAula14 - Funções em PHP
Aula14 - Funções em PHP
 
8086 assembly
8086 assembly8086 assembly
8086 assembly
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Void pointer in c
Void pointer in cVoid pointer in c
Void pointer in c
 
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
12-Syntax Directed Definition – Evaluation Order-09-06-2023.ppt
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 
Compiler Construction Course - Introduction
Compiler Construction Course - IntroductionCompiler Construction Course - Introduction
Compiler Construction Course - Introduction
 
03 mips assembly language
03 mips assembly language03 mips assembly language
03 mips assembly language
 

Similar to Compiler lec 2 (20)

Compiler lec 3
Compiler lec 3Compiler lec 3
Compiler lec 3
 
Lecture2 general structure of a compiler
Lecture2 general structure of a compilerLecture2 general structure of a compiler
Lecture2 general structure of a compiler
 
Introduction to Compilers | Phases & Structure
Introduction to Compilers | Phases & StructureIntroduction to Compilers | Phases & Structure
Introduction to Compilers | Phases & Structure
 
Introduction
IntroductionIntroduction
Introduction
 
Introduction
IntroductionIntroduction
Introduction
 
Cpcs302 1
Cpcs302  1Cpcs302  1
Cpcs302 1
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Object-Oriented Programming Using C++
Object-Oriented Programming Using C++Object-Oriented Programming Using C++
Object-Oriented Programming Using C++
 
M.tech.(cse) (regular) part i(semester i &amp; ii)
M.tech.(cse) (regular) part i(semester i &amp; ii)M.tech.(cse) (regular) part i(semester i &amp; ii)
M.tech.(cse) (regular) part i(semester i &amp; ii)
 
Chapter#01 cc
Chapter#01 ccChapter#01 cc
Chapter#01 cc
 
Improving Code Review Effectiveness Through Reviewer Recommendations
Improving Code Review Effectiveness Through Reviewer RecommendationsImproving Code Review Effectiveness Through Reviewer Recommendations
Improving Code Review Effectiveness Through Reviewer Recommendations
 
Plc part 2
Plc  part 2Plc  part 2
Plc part 2
 
Specifying compatible sharing in data structures
Specifying compatible sharing in data structuresSpecifying compatible sharing in data structures
Specifying compatible sharing in data structures
 
Chapter One
Chapter OneChapter One
Chapter One
 
Ss ui lecture 2
Ss ui lecture 2Ss ui lecture 2
Ss ui lecture 2
 
Introduction to R - from Rstudio to ggplot
Introduction to R - from Rstudio to ggplotIntroduction to R - from Rstudio to ggplot
Introduction to R - from Rstudio to ggplot
 
System Programming Overview
System Programming OverviewSystem Programming Overview
System Programming Overview
 
Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02Unit iii-111206004501-phpapp02
Unit iii-111206004501-phpapp02
 
7068458.ppt
7068458.ppt7068458.ppt
7068458.ppt
 
Ch1.ppt
Ch1.pptCh1.ppt
Ch1.ppt
 

More from Ramadan Babers, PhD

Part10 finance investment limitations of ratio analysis
Part10 finance investment limitations of ratio analysisPart10 finance investment limitations of ratio analysis
Part10 finance investment limitations of ratio analysisRamadan Babers, PhD
 
Part09 finance investment ratio analysis investment ratio
Part09 finance investment ratio analysis investment ratioPart09 finance investment ratio analysis investment ratio
Part09 finance investment ratio analysis investment ratioRamadan Babers, PhD
 
Part08 finance investment ratio analysis solvency ratio
Part08 finance investment ratio analysis solvency ratioPart08 finance investment ratio analysis solvency ratio
Part08 finance investment ratio analysis solvency ratioRamadan Babers, PhD
 
Part07 finance investment ratio analysis profitability ratio
Part07 finance investment ratio analysis profitability ratioPart07 finance investment ratio analysis profitability ratio
Part07 finance investment ratio analysis profitability ratioRamadan Babers, PhD
 
Part06 finance investment ratio analysis efficiency ratio
Part06 finance investment ratio analysis efficiency ratioPart06 finance investment ratio analysis efficiency ratio
Part06 finance investment ratio analysis efficiency ratioRamadan Babers, PhD
 
Part05 finance investment_ratio_analysis_liquidity_ratio_presentation
Part05 finance investment_ratio_analysis_liquidity_ratio_presentationPart05 finance investment_ratio_analysis_liquidity_ratio_presentation
Part05 finance investment_ratio_analysis_liquidity_ratio_presentationRamadan Babers, PhD
 
Part04 finance investment_ratio_analysis_introduction_presentation
Part04 finance investment_ratio_analysis_introduction_presentationPart04 finance investment_ratio_analysis_introduction_presentation
Part04 finance investment_ratio_analysis_introduction_presentationRamadan Babers, PhD
 
Part03 finance investment_financial_statement_analysis_presentation
Part03 finance investment_financial_statement_analysis_presentationPart03 finance investment_financial_statement_analysis_presentation
Part03 finance investment_financial_statement_analysis_presentationRamadan Babers, PhD
 
Part02 finance investment_finance_and_financial_reporting_presentation
Part02 finance investment_finance_and_financial_reporting_presentationPart02 finance investment_finance_and_financial_reporting_presentation
Part02 finance investment_finance_and_financial_reporting_presentationRamadan Babers, PhD
 
Part01 finance investment_basics_presentation
Part01 finance investment_basics_presentationPart01 finance investment_basics_presentation
Part01 finance investment_basics_presentationRamadan Babers, PhD
 
Customer Experience (CX) updated version V9.0
Customer Experience (CX) updated version V9.0Customer Experience (CX) updated version V9.0
Customer Experience (CX) updated version V9.0Ramadan Babers, PhD
 
Classification using decision tree in detail
Classification using decision tree in detailClassification using decision tree in detail
Classification using decision tree in detailRamadan Babers, PhD
 

More from Ramadan Babers, PhD (20)

Part10 finance investment limitations of ratio analysis
Part10 finance investment limitations of ratio analysisPart10 finance investment limitations of ratio analysis
Part10 finance investment limitations of ratio analysis
 
Part09 finance investment ratio analysis investment ratio
Part09 finance investment ratio analysis investment ratioPart09 finance investment ratio analysis investment ratio
Part09 finance investment ratio analysis investment ratio
 
Part08 finance investment ratio analysis solvency ratio
Part08 finance investment ratio analysis solvency ratioPart08 finance investment ratio analysis solvency ratio
Part08 finance investment ratio analysis solvency ratio
 
Part07 finance investment ratio analysis profitability ratio
Part07 finance investment ratio analysis profitability ratioPart07 finance investment ratio analysis profitability ratio
Part07 finance investment ratio analysis profitability ratio
 
Part06 finance investment ratio analysis efficiency ratio
Part06 finance investment ratio analysis efficiency ratioPart06 finance investment ratio analysis efficiency ratio
Part06 finance investment ratio analysis efficiency ratio
 
Part05 finance investment_ratio_analysis_liquidity_ratio_presentation
Part05 finance investment_ratio_analysis_liquidity_ratio_presentationPart05 finance investment_ratio_analysis_liquidity_ratio_presentation
Part05 finance investment_ratio_analysis_liquidity_ratio_presentation
 
Part04 finance investment_ratio_analysis_introduction_presentation
Part04 finance investment_ratio_analysis_introduction_presentationPart04 finance investment_ratio_analysis_introduction_presentation
Part04 finance investment_ratio_analysis_introduction_presentation
 
Part03 finance investment_financial_statement_analysis_presentation
Part03 finance investment_financial_statement_analysis_presentationPart03 finance investment_financial_statement_analysis_presentation
Part03 finance investment_financial_statement_analysis_presentation
 
Part02 finance investment_finance_and_financial_reporting_presentation
Part02 finance investment_finance_and_financial_reporting_presentationPart02 finance investment_finance_and_financial_reporting_presentation
Part02 finance investment_finance_and_financial_reporting_presentation
 
Part01 finance investment_basics_presentation
Part01 finance investment_basics_presentationPart01 finance investment_basics_presentation
Part01 finance investment_basics_presentation
 
Strategic Management part_03_03
Strategic Management part_03_03Strategic Management part_03_03
Strategic Management part_03_03
 
Strategic Management part_02_03
Strategic Management part_02_03Strategic Management part_02_03
Strategic Management part_02_03
 
Strategic Management part_01_03
Strategic Management part_01_03Strategic Management part_01_03
Strategic Management part_01_03
 
Customer Experience (CX) updated version V9.0
Customer Experience (CX) updated version V9.0Customer Experience (CX) updated version V9.0
Customer Experience (CX) updated version V9.0
 
Classification using decision tree in detail
Classification using decision tree in detailClassification using decision tree in detail
Classification using decision tree in detail
 
Customer Experience (CX)
Customer Experience (CX)Customer Experience (CX)
Customer Experience (CX)
 
Python lec 1004_ch02_excercies
Python lec 1004_ch02_excerciesPython lec 1004_ch02_excercies
Python lec 1004_ch02_excercies
 
Python lec 1003_for_biologists
Python lec 1003_for_biologistsPython lec 1003_for_biologists
Python lec 1003_for_biologists
 
Python lec 1002_for_biologists
Python lec 1002_for_biologistsPython lec 1002_for_biologists
Python lec 1002_for_biologists
 
Python lec 1001_for_biologists
Python lec 1001_for_biologistsPython lec 1001_for_biologists
Python lec 1001_for_biologists
 

Recently uploaded

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxRoyAbrique
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptxContemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
Contemporary philippine arts from the regions_PPT_Module_12 [Autosaved] (1).pptx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 

Compiler lec 2

  • 1. Compilers Design Course Ramadan Babers Compilers Design – Lec2 Faculty of Science Helwan University 1st Term – 2017/2018
  • 2. Phases of Compiler 2 Lexical Analyzer (Scanner) reads a stream of characters and puts them together into some meaningful (with respect to the source language) units called tokens. Lexical Analyzer (Scanner)Character Stream Token Stream CD-Course-FSH 1st Term 2017-2018
  • 3. Phases of Compiler 3 Lexical Analyzer (Continue)  Token  Token (terminal, attribute)  Symbol Table (reserved words) Lexical Analyzer (Scanner)Character Stream Token Stream (terminal, attribute) CD-Course-FSH 1st Term 2017-2018
  • 4. Phases of Compiler 4 Syntax Analyzer (Parsing) • Takes a stream of tokens from the scanner, and • Determines if the syntax (structure) of the program is correct according to the (context-free) grammar of the source language. Syntax Analyzer Token Syntax Tree CD-Course-FSH 1st Term 2017-2018
  • 5. Phases of Compiler 5 Syntax Analyzer (Parsing) continue • It generates a data structure, called a parse tree or an abstract syntax tree, which describes the syntactic structure of the program. CD-Course-FSH 1st Term 2017-2018
  • 6. Phases of Compiler 6 Syntax Analyzer (Parsing) continue Ex:  Statement: id=id op id  Statement: id=id [num]  Statement: if (id op num) { } / if(xy==20) { } CD-Course-FSH 1st Term 2017-2018
  • 7. Phases of Compiler 7 Semantic Analyzer  Statement: id op id op id x = y + z (semantically correct) But if Int = float + fn (semantically incorrect) CD-Course-FSH 1st Term 2017-2018
  • 8. Phases of Compiler 8 Intermediate Code Generator CD-Course-FSH 1st Term 2017-2018 Intermediate CodeSource Code Machine Code Intermediate Code C Intel Pascal Basic Mac
  • 9. Phases of Compiler 9 Code Optimizer Code optimizing means  Faster,  Shorter,  Target code consumes less power. CD-Course-FSH 1st Term 2017-2018
  • 10. Phases of Compiler 10 Example: position := initial + rate * 60 would be grouped into the following tokens; 1. The identifier position 2. The assignment symbol := 3. The identifier initial 4. The plus sign 5. The identifier rate. 6. The multiplication sign. 7. The number 60 LexicalAnalyzer Syntax Analyzer CD-Course-FSH 1st Term 2017-2018
  • 11. Phases of Compiler 11 Example: position := initial + rate * 60 <id, position> <=> <id, initial> <+> <id, rate> <*> <num, 60> <id, 1> <=> <id, 2> <+> <id, 3> <*> <num, 60> Lexical Analyzer CD-Course-FSH 1st Term 2017-2018 1 Position ---- 2 Initial ---- 3 rate ----
  • 12. Phases of Compiler 12 Example: position := initial + rate * 60 Syntax Analyzer CD-Course-FSH 1st Term 2017-2018 = <id,1> + <id,2> <id,3> * 60
  • 13. Phases of Compiler 13 Example: position := initial + rate * 60 Semantic Analyzer CD-Course-FSH 1st Term 2017-2018 = <id,1> + <id,2> <id,3> * int to float
  • 14. Phases of Compiler 14 Example: position := initial + rate * 60 Intermediate Code CD-Course-FSH 1st Term 2017-2018 t1= int to float (60) t2= id3 * t1 t3= id2 + t2 id1=t3
  • 15. Phases of Compiler 15 Example: position := initial + rate * 60 Code Optimizer CD-Course-FSH 1st Term 2017-2018 t1= int to float (60) t2= id3 * t1 t3= id2 + t2 id1=t3 t1= id3 * 60.0 id1= id2 + t1
  • 16. Phases of Compiler 16 Example: position := initial + rate * 60 LDF R2 , id3 MULF R2 , R2 , #60.0 LDF R1 , id2 ADDF R1 , R1 , R2 STF id1 , R1 Code Generator CD-Course-FSH 1st Term 2017-2018 LDF: Load float MULF: Multi float ADDF: Add float STF: Store float
  • 17. 17 Chapter 2 CD-Course-FSH 1st Term 2017-2018 A Simple Syntax-Directed Translator
  • 18. Introduction 18 CD-Course-FSH 1st Term 2017-2018 Syntax Tree Generate Parsing Group of production Terminal Grammar Nonterminal
  • 19. Introduction 19 CD-Course-FSH 1st Term 2017-2018 Syntax Parser Grammar Token
  • 20. Introduction 20 Example: Code required for the process: 5+6+7 CD-Course-FSH 1st Term 2017-2018 exp exp + exp exp 0 1 2 --- 9 exp num + exp num 0 1 2 --- 9 exp exp + num exp num num 0 1 2 --- 9
  • 21. Introduction 21 Example: Code required for the process: A=B=C=-----=Z CD-Course-FSH 1st Term 2017-2018 exp L = exp exp L L A B C --- Z exp exp = L exp L L A B C --- Z