SlideShare ist ein Scribd-Unternehmen logo
1 von 20
WELCOME TO A
JOURNEY TO
CS419

Dr. Hussien Sharaf
Computer Science Department

dr.sharaf@from-masr.com
Dr. Hussien M. Sharaf

PHASES OF COMPILER
position := initial + rate * 60
1. Lexical analyzer
id1 := id2 + id3 * 60
2. Syntax analyzer

4. Intermediate code
generator
temp1:= inttoreal(60)
temp2 := id3 * temp1
temp3 := id2 + temp2
id1 := temp3

:=

5. Code optimizer

+

id1

*

id2
id3

60

3. Semantic analyzer
:=
+

id1

*

id2
id3

inttoreal

temp1 := id3 * 60.0
id1 := id2 + temp1

6. Code generator
MOVF id3 , R2
MULF #60.0 , R2
MOVF id2 , R1
ADDF R2, R1
MOVF R1, id1

60
2
Dr. Hussien M. Sharaf

3. SEMANTIC ANALYZER
The semantics of a program are its meaning
as opposed to syntax or structure.
 The semantics consist of:
 Runtime semantics: behavior of program
at run time.
 Static semantics: checked by the
compiler.


3
Dr. Hussien M. Sharaf

STATIC SEMANTICS


Static semantics include:
 Declaration of variables and constants
before use. i.e.
int x;
x = 3;

 Calling

functions that exist (predefined in a library
or defined by the user) i.e.
n = Max(4,7);
int Max(int x, int y)
{
int z;
if(x > y)
z = x;
else
z = y;
return z;
}
4
Dr. Hussien M. Sharaf

STATIC SEMANTICS
 Passing

parameters properly.
 Type checking, i.e.
int x, y;
x = 3;
y = 2.5; performs an error, cause 2.5 is not int it is float data type.



Static semantics can not be checked by the
parser.

5
Dr. Hussien M. Sharaf

SEMANTIC ANALYZER (CONT.)


The semantic analyzer does the following:
 Checks

the static semantics of the language.
 Annotates the syntax tree with type information,
as shown in example. := x + y * 2.5;
a
:= real
id a real

+ real

id x real

Annotated syntax tree

* real

inttoreal

id y integer

literal 2.5 real
6
Dr. Hussien M. Sharaf

4. INTERMEDIATE CODE GENERATOR
Intermediate language called “Three-address
code”.
 Should have two important properties:
 Should be easy to produce.
 Should be easy to translate to the target
language.
 A TAC instruction have at most one instruction
per line and can have at most three operands.


7
Dr. Hussien M. Sharaf

INTERMEDIATE CODE GENERATOR


Example:

a := x + y * 2.5;

:= real

Three-address code
id a real

+ real

id x real

* real

inttoreal

temp1 := inttoreal(y)
temp2 := temp1 real* 2.5
temp3 := x real+ temp2
a := temp3

literal 2.5 real

id y integer



A TAC instruction have at most one instruction per
line and can have at most three operands.
8
Dr. Hussien M. Sharaf

5. CODE OPTIMIZER
 Code optimization can be applied to:
code – independent of the
target machine.
 Target code – dependent on the target
machine.
 Intermediate

9
Dr. Hussien M. Sharaf

INTERMEDIATE CODE OPTIMIZATION


Intermediate code optimization include:
A.

B.
C.

D.
E.

Constant folding.
Elimination of common sub- expressions.
Identification and elimination of unreachable
code (called dead code).
Improving loops.
Improving function calls.

10
Dr. Hussien M. Sharaf

A. CONSTANT FOLDING
Simplifying constant expressions at compile
time.
 Example:
i = 320 * 200 * 32
In this example modern compilers identify
constructs, and substitute the computed
values at compile time (in this case –
2,048,000)


11
Dr. Hussien M. Sharaf

B. ELIMINATION OF COMMON SUB- EXPRESSIONS

Replace the common expressions with a
single variable holding the computed value.
 Example:


a = b * c + g;
d = b * c * e;

tmp = b * c;
a = tmp + g;
d = tmp * e;

12
Dr. Hussien M. Sharaf

C. IDENTIFICATION AND ELIMINATION OF DEAD CODE

Dead code is the code in a program which is
executed but whose result is never used in
any other computation.
 Dead code wastes computation time.
 Example:


int f (int x, int y)
{
int z = x + y;
return x * y;
}

should be eliminated.

13
Dr. Hussien M. Sharaf

D. IMPROVING LOOPS





There are a lot of strategies for improving loops.
A simple one is: Move loop invariants out of the loop
Example:
For y = 0 to Height-1
For x = 0 to Width-1
' y*Width is invariant
i = y*Width + x
Process i
Next x
Next y



For y = 0 to Height-1
temp = y*Width
For x = 0 to Width-1
i = temp + x
Process i
Next x
Next y

This link : http://www.aivosto.com/vbtips/loopopt.html
illustrates all the strategies for interested readers. 14
Dr. Hussien M. Sharaf

E. IMPROVING FUNCTION CALLS





One strategy is by Removing recursion (function that calls itself).
Example of recursive function:
unsigned int factorial(unsigned int n)
{
if (n == 0) { return 1; }
else { return n * factorial(n - 1); }
}
After removing recursion and using loops instead it will be:
unsigned int factorial(unsigned int n)
{
int result = 1;
if (n == 0) { return 1; }
else {
for (i = n; i>=1; i--)
{
result = result * i;
}
return result;
}
}
15
Dr. Hussien M. Sharaf

TARGET CODE OPTIMIZATION


Target Code optimization is done by
improving the intermediate code, and
removing the redundant code.



Example:
Temp1 := int-to-real(60)
Temp2 := id3 * temp1
Temp3 := id2 + temp2
Id1 := temp3

temp1 := id3 * 60.0
id1 := id2 + temp1

16
Dr. Hussien M. Sharaf

TARGET CODE OPTIMIZATION


Target code optimization include:
a.

b.

Allocation and use of registers.
Selection of better (safer) instructions and
addressing modes.

17
Dr. Hussien M. Sharaf

6. CODE GENERATOR
Generates code for the target machine.
 Selects appropriate machine instructions.
 Allocates memory locations for variables.
 Allocates registers for intermediate
computations.


Three-address code
temp1 := int2real(y)
temp2 := temp1 * 2.5
temp3 := x + temp2
a : = temp3

Assembly code
LOADI
MOVF
MULF
LOADF
ADDF
STORF

R1 , y
F1 , R1
F2 , F1, 2.5
F3 , x
F4 , F3, F2
a, F4

;; R1
;; F1
;; F2
;; F3
;; F4
;; a

y
int2real(R1)
F1 * 2.5
x
F3 + F2
F4
18
Dr. Hussien M. Sharaf

CODE GENERATOR


Generation target code example:
temp := id3 * 60.0

id1 := id2 + temp

MOVF
MULF
MOVF
ADDF
MOVF

id3 , R2
#60.0, R2
id2 , R1
R2 , R1
R1, id1

;; id3 R2
;; R2 * 60.0
;; id2 R1
;; R2 + R1
;; R1
id1

R2
R1

19
Dr. Hussien M. Sharaf

THANK YOU

Weitere ähnliche Inhalte

Was ist angesagt?

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generationIffat Anjum
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONAnil Pokhrel
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler DesignShine Raj
 
C programming session 04
C programming session 04C programming session 04
C programming session 04Dushmanta Nath
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1Shashwat Shriparv
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representationsahmed51236
 
Code generator
Code generatorCode generator
Code generatorTech_MX
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generationAkshaya Arunan
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)bolovv
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)bolovv
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshopBAINIDA
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 

Was ist angesagt? (20)

Lecture 12 intermediate code generation
Lecture 12 intermediate code generationLecture 12 intermediate code generation
Lecture 12 intermediate code generation
 
COMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTIONCOMPILER DESIGN AND CONSTRUCTION
COMPILER DESIGN AND CONSTRUCTION
 
Three Address code
Three Address code Three Address code
Three Address code
 
Three address code In Compiler Design
Three address code In Compiler DesignThree address code In Compiler Design
Three address code In Compiler Design
 
C programming session 04
C programming session 04C programming session 04
C programming session 04
 
Lecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C ProgrammingLecture 6- Intorduction to C Programming
Lecture 6- Intorduction to C Programming
 
User Defined Functions in C Language
User Defined Functions   in  C LanguageUser Defined Functions   in  C Language
User Defined Functions in C Language
 
Lecture 18 - Pointers
Lecture 18 - PointersLecture 18 - Pointers
Lecture 18 - Pointers
 
C programming part4
C programming part4C programming part4
C programming part4
 
Interm codegen
Interm codegenInterm codegen
Interm codegen
 
Intermediate code generation1
Intermediate code generation1Intermediate code generation1
Intermediate code generation1
 
Intermediate code representations
Intermediate code representationsIntermediate code representations
Intermediate code representations
 
Code generator
Code generatorCode generator
Code generator
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
Intermediate code generation
Intermediate code generationIntermediate code generation
Intermediate code generation
 
Network Security CS3-4
Network Security CS3-4 Network Security CS3-4
Network Security CS3-4
 
Chapter Eight(2)
Chapter Eight(2)Chapter Eight(2)
Chapter Eight(2)
 
Chapter Eight(3)
Chapter Eight(3)Chapter Eight(3)
Chapter Eight(3)
 
Python programming workshop
Python programming workshopPython programming workshop
Python programming workshop
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 

Andere mochten auch

Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemKaran Thakkar
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Animesh Chaturvedi
 

Andere mochten auch (20)

Theory of computation Lec6
Theory of computation Lec6Theory of computation Lec6
Theory of computation Lec6
 
Cs419 lec3 lexical analysis using re
Cs419 lec3   lexical analysis using reCs419 lec3   lexical analysis using re
Cs419 lec3 lexical analysis using re
 
Cs419 lec4 lexical analysis using re
Cs419 lec4   lexical analysis using reCs419 lec4   lexical analysis using re
Cs419 lec4 lexical analysis using re
 
Lec4
Lec4Lec4
Lec4
 
Cs419 lec5 lexical analysis using dfa
Cs419 lec5   lexical analysis using dfaCs419 lec5   lexical analysis using dfa
Cs419 lec5 lexical analysis using dfa
 
Cs419 lec7 cfg
Cs419 lec7   cfgCs419 lec7   cfg
Cs419 lec7 cfg
 
Cs419 lec6 lexical analysis using nfa
Cs419 lec6   lexical analysis using nfaCs419 lec6   lexical analysis using nfa
Cs419 lec6 lexical analysis using nfa
 
Cs419 lec9 constructing parsing table ll1
Cs419 lec9   constructing parsing table ll1Cs419 lec9   constructing parsing table ll1
Cs419 lec9 constructing parsing table ll1
 
Theory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence ProblemTheory of Computer Science - Post Correspondence Problem
Theory of Computer Science - Post Correspondence Problem
 
Infos2014
Infos2014Infos2014
Infos2014
 
Cs419 lec8 top-down parsing
Cs419 lec8    top-down parsingCs419 lec8    top-down parsing
Cs419 lec8 top-down parsing
 
Minimizing DFA
Minimizing DFAMinimizing DFA
Minimizing DFA
 
Theory of computing
Theory of computingTheory of computing
Theory of computing
 
Theory of computation Lec7 pda
Theory of computation Lec7 pdaTheory of computation Lec7 pda
Theory of computation Lec7 pda
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)Deterministic Finite Automata (DFA)
Deterministic Finite Automata (DFA)
 
Theory of computation Lec1
Theory of computation Lec1Theory of computation Lec1
Theory of computation Lec1
 
NFA to DFA
NFA to DFANFA to DFA
NFA to DFA
 
Cs419 lec11 bottom-up parsing
Cs419 lec11   bottom-up parsingCs419 lec11   bottom-up parsing
Cs419 lec11 bottom-up parsing
 
Theory of computation Lec2
Theory of computation Lec2Theory of computation Lec2
Theory of computation Lec2
 

Ähnlich wie Cs419 lec12 semantic analyzer

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankarDipankar Nalui
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languagesAnkit Pandey
 
Code transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTsCode transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTsMarkRizun
 
Code Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTsCode Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTsESUG
 
Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...FahmiOlayah
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2Zaibi Gondal
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constantsCtOlaf
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10alish sha
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded cBenux Wei
 

Ähnlich wie Cs419 lec12 semantic analyzer (20)

Introduction to code optimization by dipankar
Introduction to code optimization by dipankarIntroduction to code optimization by dipankar
Introduction to code optimization by dipankar
 
Optimization in Programming languages
Optimization in Programming languagesOptimization in Programming languages
Optimization in Programming languages
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
functions
functionsfunctions
functions
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
Code transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTsCode transformation by direct transformation of ASTs
Code transformation by direct transformation of ASTs
 
Code Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTsCode Transformation by Direct Transformation of ASTs
Code Transformation by Direct Transformation of ASTs
 
Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...Public-Key Cryptography.pdfWrite the result of the following operation with t...
Public-Key Cryptography.pdfWrite the result of the following operation with t...
 
C programming Lab 2
C programming Lab 2C programming Lab 2
C programming Lab 2
 
5. using variables, data, expressions and constants
5. using variables, data, expressions and constants5. using variables, data, expressions and constants
5. using variables, data, expressions and constants
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Introductionof c
Introductionof cIntroductionof c
Introductionof c
 
20BCE1734.pdf
20BCE1734.pdf20BCE1734.pdf
20BCE1734.pdf
 
Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10Bti1022 lab sheet 9 10
Bti1022 lab sheet 9 10
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
Bc0037
Bc0037Bc0037
Bc0037
 
Code optimization
Code optimizationCode optimization
Code optimization
 
Ch3
Ch3Ch3
Ch3
 
Apclass
ApclassApclass
Apclass
 

Mehr von Arab Open University and Cairo University

Mehr von Arab Open University and Cairo University (14)

File Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswerFile Organization & processing Mid term summer 2014 - modelanswer
File Organization & processing Mid term summer 2014 - modelanswer
 
Model answer of compilers june spring 2013
Model answer of compilers june spring 2013Model answer of compilers june spring 2013
Model answer of compilers june spring 2013
 
Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013Model answer of exam TC_spring 2013
Model answer of exam TC_spring 2013
 
Theory of computation Lec3 dfa
Theory of computation Lec3 dfaTheory of computation Lec3 dfa
Theory of computation Lec3 dfa
 
Setup python with eclipse
Setup python with eclipseSetup python with eclipse
Setup python with eclipse
 
Compilers Final spring 2013 model answer
 Compilers Final spring 2013 model answer Compilers Final spring 2013 model answer
Compilers Final spring 2013 model answer
 
Compilers midterm spring 2013 model answer
Compilers midterm spring 2013   model answerCompilers midterm spring 2013   model answer
Compilers midterm spring 2013 model answer
 
Final Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answersFinal Exam OS fall 2012-2013 with answers
Final Exam OS fall 2012-2013 with answers
 
CS215 - Lec 8 searching records
CS215 - Lec 8  searching recordsCS215 - Lec 8  searching records
CS215 - Lec 8 searching records
 
CS215 - Lec 7 managing records collection
CS215 - Lec 7  managing records collectionCS215 - Lec 7  managing records collection
CS215 - Lec 7 managing records collection
 
CS215 - Lec 6 record index
CS215 - Lec 6  record indexCS215 - Lec 6  record index
CS215 - Lec 6 record index
 
CS215 - Lec 5 record organization
CS215 - Lec 5  record organizationCS215 - Lec 5  record organization
CS215 - Lec 5 record organization
 
CS215 - Lec 4 single record organization
CS215 - Lec 4  single record organizationCS215 - Lec 4  single record organization
CS215 - Lec 4 single record organization
 
CS215 - Lec 2 file organization
CS215 - Lec 2   file organizationCS215 - Lec 2   file organization
CS215 - Lec 2 file organization
 

Kürzlich hochgeladen

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - Englishneillewis46
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfPoh-Sun Goh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxAmanpreet Kaur
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsKarakKing
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 

Kürzlich hochgeladen (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Graduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - EnglishGraduate Outcomes Presentation Slides - English
Graduate Outcomes Presentation Slides - English
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Salient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functionsSalient Features of India constitution especially power and functions
Salient Features of India constitution especially power and functions
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 

Cs419 lec12 semantic analyzer

  • 1. WELCOME TO A JOURNEY TO CS419 Dr. Hussien Sharaf Computer Science Department dr.sharaf@from-masr.com
  • 2. Dr. Hussien M. Sharaf PHASES OF COMPILER position := initial + rate * 60 1. Lexical analyzer id1 := id2 + id3 * 60 2. Syntax analyzer 4. Intermediate code generator temp1:= inttoreal(60) temp2 := id3 * temp1 temp3 := id2 + temp2 id1 := temp3 := 5. Code optimizer + id1 * id2 id3 60 3. Semantic analyzer := + id1 * id2 id3 inttoreal temp1 := id3 * 60.0 id1 := id2 + temp1 6. Code generator MOVF id3 , R2 MULF #60.0 , R2 MOVF id2 , R1 ADDF R2, R1 MOVF R1, id1 60 2
  • 3. Dr. Hussien M. Sharaf 3. SEMANTIC ANALYZER The semantics of a program are its meaning as opposed to syntax or structure.  The semantics consist of:  Runtime semantics: behavior of program at run time.  Static semantics: checked by the compiler.  3
  • 4. Dr. Hussien M. Sharaf STATIC SEMANTICS  Static semantics include:  Declaration of variables and constants before use. i.e. int x; x = 3;  Calling functions that exist (predefined in a library or defined by the user) i.e. n = Max(4,7); int Max(int x, int y) { int z; if(x > y) z = x; else z = y; return z; } 4
  • 5. Dr. Hussien M. Sharaf STATIC SEMANTICS  Passing parameters properly.  Type checking, i.e. int x, y; x = 3; y = 2.5; performs an error, cause 2.5 is not int it is float data type.  Static semantics can not be checked by the parser. 5
  • 6. Dr. Hussien M. Sharaf SEMANTIC ANALYZER (CONT.)  The semantic analyzer does the following:  Checks the static semantics of the language.  Annotates the syntax tree with type information, as shown in example. := x + y * 2.5; a := real id a real + real id x real Annotated syntax tree * real inttoreal id y integer literal 2.5 real 6
  • 7. Dr. Hussien M. Sharaf 4. INTERMEDIATE CODE GENERATOR Intermediate language called “Three-address code”.  Should have two important properties:  Should be easy to produce.  Should be easy to translate to the target language.  A TAC instruction have at most one instruction per line and can have at most three operands.  7
  • 8. Dr. Hussien M. Sharaf INTERMEDIATE CODE GENERATOR  Example: a := x + y * 2.5; := real Three-address code id a real + real id x real * real inttoreal temp1 := inttoreal(y) temp2 := temp1 real* 2.5 temp3 := x real+ temp2 a := temp3 literal 2.5 real id y integer  A TAC instruction have at most one instruction per line and can have at most three operands. 8
  • 9. Dr. Hussien M. Sharaf 5. CODE OPTIMIZER  Code optimization can be applied to: code – independent of the target machine.  Target code – dependent on the target machine.  Intermediate 9
  • 10. Dr. Hussien M. Sharaf INTERMEDIATE CODE OPTIMIZATION  Intermediate code optimization include: A. B. C. D. E. Constant folding. Elimination of common sub- expressions. Identification and elimination of unreachable code (called dead code). Improving loops. Improving function calls. 10
  • 11. Dr. Hussien M. Sharaf A. CONSTANT FOLDING Simplifying constant expressions at compile time.  Example: i = 320 * 200 * 32 In this example modern compilers identify constructs, and substitute the computed values at compile time (in this case – 2,048,000)  11
  • 12. Dr. Hussien M. Sharaf B. ELIMINATION OF COMMON SUB- EXPRESSIONS Replace the common expressions with a single variable holding the computed value.  Example:  a = b * c + g; d = b * c * e; tmp = b * c; a = tmp + g; d = tmp * e; 12
  • 13. Dr. Hussien M. Sharaf C. IDENTIFICATION AND ELIMINATION OF DEAD CODE Dead code is the code in a program which is executed but whose result is never used in any other computation.  Dead code wastes computation time.  Example:  int f (int x, int y) { int z = x + y; return x * y; } should be eliminated. 13
  • 14. Dr. Hussien M. Sharaf D. IMPROVING LOOPS    There are a lot of strategies for improving loops. A simple one is: Move loop invariants out of the loop Example: For y = 0 to Height-1 For x = 0 to Width-1 ' y*Width is invariant i = y*Width + x Process i Next x Next y  For y = 0 to Height-1 temp = y*Width For x = 0 to Width-1 i = temp + x Process i Next x Next y This link : http://www.aivosto.com/vbtips/loopopt.html illustrates all the strategies for interested readers. 14
  • 15. Dr. Hussien M. Sharaf E. IMPROVING FUNCTION CALLS    One strategy is by Removing recursion (function that calls itself). Example of recursive function: unsigned int factorial(unsigned int n) { if (n == 0) { return 1; } else { return n * factorial(n - 1); } } After removing recursion and using loops instead it will be: unsigned int factorial(unsigned int n) { int result = 1; if (n == 0) { return 1; } else { for (i = n; i>=1; i--) { result = result * i; } return result; } } 15
  • 16. Dr. Hussien M. Sharaf TARGET CODE OPTIMIZATION  Target Code optimization is done by improving the intermediate code, and removing the redundant code.  Example: Temp1 := int-to-real(60) Temp2 := id3 * temp1 Temp3 := id2 + temp2 Id1 := temp3 temp1 := id3 * 60.0 id1 := id2 + temp1 16
  • 17. Dr. Hussien M. Sharaf TARGET CODE OPTIMIZATION  Target code optimization include: a. b. Allocation and use of registers. Selection of better (safer) instructions and addressing modes. 17
  • 18. Dr. Hussien M. Sharaf 6. CODE GENERATOR Generates code for the target machine.  Selects appropriate machine instructions.  Allocates memory locations for variables.  Allocates registers for intermediate computations.  Three-address code temp1 := int2real(y) temp2 := temp1 * 2.5 temp3 := x + temp2 a : = temp3 Assembly code LOADI MOVF MULF LOADF ADDF STORF R1 , y F1 , R1 F2 , F1, 2.5 F3 , x F4 , F3, F2 a, F4 ;; R1 ;; F1 ;; F2 ;; F3 ;; F4 ;; a y int2real(R1) F1 * 2.5 x F3 + F2 F4 18
  • 19. Dr. Hussien M. Sharaf CODE GENERATOR  Generation target code example: temp := id3 * 60.0 id1 := id2 + temp MOVF MULF MOVF ADDF MOVF id3 , R2 #60.0, R2 id2 , R1 R2 , R1 R1, id1 ;; id3 R2 ;; R2 * 60.0 ;; id2 R1 ;; R2 + R1 ;; R1 id1 R2 R1 19
  • 20. Dr. Hussien M. Sharaf THANK YOU