SlideShare a Scribd company logo
TYPE CHECKING
Muhammad Daniyal
Salman
Saif Ali
Lexical Analyzer
• Tokens
Syntax Analyzer
• Context free grammar
• Parse tree(Parser)
Semantic Analyzer
• Before we don’t have rules and attributes in CFG
• Then we add some semantic rule with productions
Rule
Syntax Directed Definition
• Each grammar symbol associate with some attributes that
gives information
• CFG + Semantic Rules = Syntax directed definition
• Do not specify the order in which semantic actions should
be executed.
Translation Scheme
• explicitly specify the ordering of the semantic actions
TYPES OF ATTRIBUTES
• Synthesized
• Inherited
Synthesized
These attributes get value from child node attributes
Inherited
These attributes get value from parent node or from siblings
What Semantic Analyzer do ?
Scope resolution
• Variables should be declare at least once before usage
Array Bound Checking
• When boundaries of an Array exceed
Type Checking
• Check type
• Eg: int x = 15.5;
Type Checking
• Type checking is the process of verifying that each operation
executed in a program respects the type system of the
language.
• This generally means that all operands in any expression are of
appropriate types and number.
• Much of what we do in the semantic analysis phase is type
checking.
How to design a Type Checker?
When designing a type checker for a compiler, here’s the process:
• Identify the types that are available in the language
• Identify the language constructs that have types associated with
them
• Identify the semantic rules for the language
• A language is considered strongly- typed if each and every type
error is detected during compilation.
Type Checking Preventions
• Application of a function to wrong number of arguments
• Application of integer functions to floats
• Use of undeclared variables in expressions
• Functions that do not return values,
• Division by zero
• Array indices out of bounds
Two Types of Type Checking
Static Type Checking
Dynamic Type Checking
Static Type Checking : Check on Compile time
1- Type Check
• 2+2.5 = Error
2- Flow of Control
• Flow of control stop at somewhere
3- Uniqueness Check
• Int a = 2; a should be unique
4- Name Related Check
• Calling add()
Definition add()
Example:
For example, if a and b are of type int and we assign very large
values to them, a * b may not be in the acceptable range of ints,
or an attempt to compute the ratio between two integers may
raise a division by zero. These kinds of type errors usually cannot
be detected at compiler time.
Dynamic Type Checking : Check on Runtime
• Common dynamically typed languages are : JavaScript, Php
and Python etc.
• Most of the languages used both.
• Static or Dynamic doesn’t mean Weak or strong
Type System
• Type system is a collection of rules applied on Type expression
• Designing of type checker vary from language to language
E.G : 2+2 = 4
• Each expression has a type associated
• Basic types: Boolean, Int , Char
• Constructed types : Pointer, Array , Structures
Type Expression
• A basic type is a type expression
• A type name is a type expression
• A type expression can be formed by applying the array type
constructor to a number and a type expression.
• A record is a data structure with named field
• A type expression can be formed by using the type
constructor for function types
• If s and t are type expressions, then their Cartesian product s*t is
a type expression
• Type expressions may contain variables whose values are type
expressions
Error Recovery
• Type checker found an error
• They report the location and nature of an error
• Ideal type checker recover the error and move on to detect more
Type Checking of Statements
S -> id := E {S.type := if id.type = E.type then void else error}
S -> if E then S1 {S.type := if E.type = boolean then S1.type else error}
S -> while E do S1 {S.type := if E.type = boolean then S1.type}
S -> S1; S2 {S.type := if S1.type = void ∧
Equivalence of Type Expression
Structural Equivalence
Name for Type Expression
Structural Equivalence:
• Two expression have same basic type
• Formed by applying same constructor type
Name for Type Expression:
• In some languages type expression can given some name
Type checking compiler construction Chapter #6
Type Conversion
• Expression X + I (The X is real and I is int type)
• First compiler check both of the operand are of same type or
not
• Usually convert int type into real type and perform real
operand operation
How to Convert
• X + I (int convert into real)
• X I intoreal real +
• Intoreal operation convert I intoreal
• Or real+ operation perform real addition in between both real
operands
Coercions
• Convert one type to another automatically called implicit or
coercions like ASCII
• It is limited in many language in case of no information loss
• Integer convert into real but no vice versa because of some
memory loss
• When programmer write something to convert called Explicit
Over Loading Functions
• The same name is used for several different operations over
several different types.
• Type checker is used to detect the error while creating
overloading functions
Polymorphic Functions
• A piece of code that can be executed with arguments of
different types
Thank you
Ad

More Related Content

What's hot (20)

Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
Radhakrishnan Chinnusamy
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
NderituGichuki1
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 
Code generation
Code generationCode generation
Code generation
Aparna Nayak
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Compiler design error handling
Compiler design error handlingCompiler design error handling
Compiler design error handling
RohitK71
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum
 
Loop optimization
Loop optimizationLoop optimization
Loop optimization
Vivek Gandhi
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
sanchi29
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
Narayana Galla
 
Code Generation
Code GenerationCode Generation
Code Generation
PrabuPappuR
 
Types of Parser
Types of ParserTypes of Parser
Types of Parser
SomnathMore3
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Compiler design
Compiler designCompiler design
Compiler design
Thakur Ganeshsingh Thakur
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 
Lexical Analysis - Compiler Design
Lexical Analysis - Compiler DesignLexical Analysis - Compiler Design
Lexical Analysis - Compiler Design
Akhil Kaushik
 
Lecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.pptLecture 1 - Lexical Analysis.ppt
Lecture 1 - Lexical Analysis.ppt
NderituGichuki1
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Compiler design error handling
Compiler design error handlingCompiler design error handling
Compiler design error handling
RohitK71
 
Lecture 01 introduction to compiler
Lecture 01 introduction to compilerLecture 01 introduction to compiler
Lecture 01 introduction to compiler
Iffat Anjum
 
Intermediate code generator
Intermediate code generatorIntermediate code generator
Intermediate code generator
sanchi29
 
Error Detection & Recovery
Error Detection & RecoveryError Detection & Recovery
Error Detection & Recovery
Akhil Kaushik
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
Swati Chauhan
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
Sudip Singh
 

Similar to Type checking compiler construction Chapter #6 (20)

Type Checking
Type CheckingType Checking
Type Checking
A. S. M. Shafi
 
Chapter -5.pptx chapter five compiler design
Chapter -5.pptx  chapter five compiler designChapter -5.pptx  chapter five compiler design
Chapter -5.pptx chapter five compiler design
gadisaAdamu
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
Jamshid Hashimi
 
Chapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhklj
Chapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhkljChapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhklj
Chapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhklj
Shemse Shukre
 
TypeScript.ppt LPU Notes Lecture PPT for 2024
TypeScript.ppt  LPU Notes Lecture PPT for 2024TypeScript.ppt  LPU Notes Lecture PPT for 2024
TypeScript.ppt LPU Notes Lecture PPT for 2024
manveersingh2k05
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
Dilawar Khan
 
BASICS OF PYTHON usefull for the student who would like to learn on their own
BASICS OF PYTHON usefull for the student who would like to learn on their ownBASICS OF PYTHON usefull for the student who would like to learn on their own
BASICS OF PYTHON usefull for the student who would like to learn on their own
Nandini485510
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
Abhishek Soni
 
Basics Of C Programming For Beginners In Easiest Way
Basics Of C Programming For Beginners In Easiest WayBasics Of C Programming For Beginners In Easiest Way
Basics Of C Programming For Beginners In Easiest Way
akshay rajpure
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
Ayshwarya Baburam
 
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFPROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
dinesh620610
 
C# Basics
C# BasicsC# Basics
C# Basics
vijayakumari kaliannan
 
Compiler lecture 07
Compiler lecture 07Compiler lecture 07
Compiler lecture 07
University of Chitral
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
ssuser0ca68e
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
AhsanAli64749
 
Type Systems
Type SystemsType Systems
Type Systems
Jordan Parmer
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
16115yogendraSingh
 
Python programming l2
Python programming l2Python programming l2
Python programming l2
Aishwarya Deshmukh
 
Chapter -5.pptx chapter five compiler design
Chapter -5.pptx  chapter five compiler designChapter -5.pptx  chapter five compiler design
Chapter -5.pptx chapter five compiler design
gadisaAdamu
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
Jamshid Hashimi
 
Chapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhklj
Chapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhkljChapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhklj
Chapter 5 - Type Cheaking.pptxkgjkh,lhj,lghjhklj
Shemse Shukre
 
TypeScript.ppt LPU Notes Lecture PPT for 2024
TypeScript.ppt  LPU Notes Lecture PPT for 2024TypeScript.ppt  LPU Notes Lecture PPT for 2024
TypeScript.ppt LPU Notes Lecture PPT for 2024
manveersingh2k05
 
CS4443 - Modern Programming Language - I Lecture (2)
CS4443 - Modern Programming Language - I  Lecture (2)CS4443 - Modern Programming Language - I  Lecture (2)
CS4443 - Modern Programming Language - I Lecture (2)
Dilawar Khan
 
BASICS OF PYTHON usefull for the student who would like to learn on their own
BASICS OF PYTHON usefull for the student who would like to learn on their ownBASICS OF PYTHON usefull for the student who would like to learn on their own
BASICS OF PYTHON usefull for the student who would like to learn on their own
Nandini485510
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
Abhishek Soni
 
Basics Of C Programming For Beginners In Easiest Way
Basics Of C Programming For Beginners In Easiest WayBasics Of C Programming For Beginners In Easiest Way
Basics Of C Programming For Beginners In Easiest Way
akshay rajpure
 
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFPROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
PROGRAMMING IN C UNIT II.pdfFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
dinesh620610
 
2nd PUC Computer science chapter 5 review of c++
2nd PUC Computer science chapter 5   review of c++2nd PUC Computer science chapter 5   review of c++
2nd PUC Computer science chapter 5 review of c++
Aahwini Esware gowda
 
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
BCP_u2.pptxBCP_u2.pptxBCP_u2.pptxBCP_u2.pptx
RutviBaraiya
 
Ad

Recently uploaded (20)

QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Development of MLR, ANN and ANFIS Models for Estimation of PCUs at Different ...
Journal of Soft Computing in Civil Engineering
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Structural Response of Reinforced Self-Compacting Concrete Deep Beam Using Fi...
Journal of Soft Computing in Civil Engineering
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)QA/QC Manager (Quality management Expert)
QA/QC Manager (Quality management Expert)
rccbatchplant
 
Introduction to FLUID MECHANICS & KINEMATICS
Introduction to FLUID MECHANICS &  KINEMATICSIntroduction to FLUID MECHANICS &  KINEMATICS
Introduction to FLUID MECHANICS & KINEMATICS
narayanaswamygdas
 
Compiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptxCompiler Design_Lexical Analysis phase.pptx
Compiler Design_Lexical Analysis phase.pptx
RushaliDeshmukh2
 
theory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptxtheory-slides-for react for beginners.pptx
theory-slides-for react for beginners.pptx
sanchezvanessa7896
 
Avnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights FlyerAvnet Silica's PCIM 2025 Highlights Flyer
Avnet Silica's PCIM 2025 Highlights Flyer
WillDavies22
 
Reagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptxReagent dosing (Bredel) presentation.pptx
Reagent dosing (Bredel) presentation.pptx
AlejandroOdio
 
Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.Fort night presentation new0903 pdf.pdf.
Fort night presentation new0903 pdf.pdf.
anuragmk56
 
Artificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptxArtificial Intelligence (AI) basics.pptx
Artificial Intelligence (AI) basics.pptx
aditichinar
 
DSP and MV the Color image processing.ppt
DSP and MV the  Color image processing.pptDSP and MV the  Color image processing.ppt
DSP and MV the Color image processing.ppt
HafizAhamed8
 
fluke dealers in bangalore..............
fluke dealers in bangalore..............fluke dealers in bangalore..............
fluke dealers in bangalore..............
Haresh Vaswani
 
Level 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical SafetyLevel 1-Safety.pptx Presentation of Electrical Safety
Level 1-Safety.pptx Presentation of Electrical Safety
JoseAlbertoCariasDel
 
Compiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptxCompiler Design Unit1 PPT Phases of Compiler.pptx
Compiler Design Unit1 PPT Phases of Compiler.pptx
RushaliDeshmukh2
 
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdffive-year-soluhhhhhhhhhhhhhhhhhtions.pdf
five-year-soluhhhhhhhhhhhhhhhhhtions.pdf
AdityaSharma944496
 
Raish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdfRaish Khanji GTU 8th sem Internship Report.pdf
Raish Khanji GTU 8th sem Internship Report.pdf
RaishKhanji
 
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptxLidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
Lidar for Autonomous Driving, LiDAR Mapping for Driverless Cars.pptx
RishavKumar530754
 
new ppt artificial intelligence historyyy
new ppt artificial intelligence historyyynew ppt artificial intelligence historyyy
new ppt artificial intelligence historyyy
PianoPianist
 
Machine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptxMachine learning project on employee attrition detection using (2).pptx
Machine learning project on employee attrition detection using (2).pptx
rajeswari89780
 
some basics electrical and electronics knowledge
some basics electrical and electronics knowledgesome basics electrical and electronics knowledge
some basics electrical and electronics knowledge
nguyentrungdo88
 
Ad

Type checking compiler construction Chapter #6

  • 2. Lexical Analyzer • Tokens Syntax Analyzer • Context free grammar • Parse tree(Parser)
  • 3. Semantic Analyzer • Before we don’t have rules and attributes in CFG • Then we add some semantic rule with productions Rule Syntax Directed Definition • Each grammar symbol associate with some attributes that gives information • CFG + Semantic Rules = Syntax directed definition
  • 4. • Do not specify the order in which semantic actions should be executed. Translation Scheme • explicitly specify the ordering of the semantic actions
  • 5. TYPES OF ATTRIBUTES • Synthesized • Inherited Synthesized These attributes get value from child node attributes Inherited These attributes get value from parent node or from siblings
  • 6. What Semantic Analyzer do ? Scope resolution • Variables should be declare at least once before usage Array Bound Checking • When boundaries of an Array exceed Type Checking • Check type • Eg: int x = 15.5;
  • 7. Type Checking • Type checking is the process of verifying that each operation executed in a program respects the type system of the language. • This generally means that all operands in any expression are of appropriate types and number. • Much of what we do in the semantic analysis phase is type checking.
  • 8. How to design a Type Checker? When designing a type checker for a compiler, here’s the process: • Identify the types that are available in the language • Identify the language constructs that have types associated with them • Identify the semantic rules for the language • A language is considered strongly- typed if each and every type error is detected during compilation.
  • 9. Type Checking Preventions • Application of a function to wrong number of arguments • Application of integer functions to floats • Use of undeclared variables in expressions • Functions that do not return values, • Division by zero • Array indices out of bounds
  • 10. Two Types of Type Checking Static Type Checking Dynamic Type Checking Static Type Checking : Check on Compile time 1- Type Check • 2+2.5 = Error 2- Flow of Control • Flow of control stop at somewhere
  • 11. 3- Uniqueness Check • Int a = 2; a should be unique 4- Name Related Check • Calling add() Definition add() Example: For example, if a and b are of type int and we assign very large values to them, a * b may not be in the acceptable range of ints, or an attempt to compute the ratio between two integers may raise a division by zero. These kinds of type errors usually cannot be detected at compiler time.
  • 12. Dynamic Type Checking : Check on Runtime • Common dynamically typed languages are : JavaScript, Php and Python etc. • Most of the languages used both. • Static or Dynamic doesn’t mean Weak or strong
  • 13. Type System • Type system is a collection of rules applied on Type expression • Designing of type checker vary from language to language E.G : 2+2 = 4 • Each expression has a type associated • Basic types: Boolean, Int , Char
  • 14. • Constructed types : Pointer, Array , Structures Type Expression • A basic type is a type expression • A type name is a type expression • A type expression can be formed by applying the array type constructor to a number and a type expression. • A record is a data structure with named field • A type expression can be formed by using the type constructor for function types
  • 15. • If s and t are type expressions, then their Cartesian product s*t is a type expression • Type expressions may contain variables whose values are type expressions Error Recovery • Type checker found an error • They report the location and nature of an error • Ideal type checker recover the error and move on to detect more
  • 16. Type Checking of Statements S -> id := E {S.type := if id.type = E.type then void else error} S -> if E then S1 {S.type := if E.type = boolean then S1.type else error} S -> while E do S1 {S.type := if E.type = boolean then S1.type} S -> S1; S2 {S.type := if S1.type = void ∧
  • 17. Equivalence of Type Expression Structural Equivalence Name for Type Expression Structural Equivalence: • Two expression have same basic type • Formed by applying same constructor type Name for Type Expression: • In some languages type expression can given some name
  • 19. Type Conversion • Expression X + I (The X is real and I is int type) • First compiler check both of the operand are of same type or not • Usually convert int type into real type and perform real operand operation How to Convert • X + I (int convert into real) • X I intoreal real +
  • 20. • Intoreal operation convert I intoreal • Or real+ operation perform real addition in between both real operands Coercions • Convert one type to another automatically called implicit or coercions like ASCII • It is limited in many language in case of no information loss • Integer convert into real but no vice versa because of some memory loss • When programmer write something to convert called Explicit
  • 21. Over Loading Functions • The same name is used for several different operations over several different types. • Type checker is used to detect the error while creating overloading functions Polymorphic Functions • A piece of code that can be executed with arguments of different types