SlideShare uma empresa Scribd logo
1 de 22
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 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

Mais conteúdo relacionado

Mais procurados

Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in CompilerAkhil Kaushik
 
Compiler Design
Compiler DesignCompiler Design
Compiler DesignMir Majid
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designSadia Akter
 
Three Address code
Three Address code Three Address code
Three Address code Pooja Dixit
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignKuppusamy P
 
Context free grammars
Context free grammarsContext free grammars
Context free grammarsRonak Thakkar
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler designKuppusamy P
 
Operator precedance parsing
Operator precedance parsingOperator precedance parsing
Operator precedance parsingsanchi29
 

Mais procurados (20)

Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Bootstrapping in Compiler
Bootstrapping in CompilerBootstrapping in Compiler
Bootstrapping in Compiler
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Compiler Design
Compiler DesignCompiler Design
Compiler Design
 
Symbol Table
Symbol TableSymbol Table
Symbol Table
 
Syntax analysis
Syntax analysisSyntax analysis
Syntax analysis
 
Chapter 5 Syntax Directed Translation
Chapter 5   Syntax Directed TranslationChapter 5   Syntax Directed Translation
Chapter 5 Syntax Directed Translation
 
The role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler designThe role of the parser and Error recovery strategies ppt in compiler design
The role of the parser and Error recovery strategies ppt in compiler design
 
Three Address code
Three Address code Three Address code
Three Address code
 
Intermediate code generation in Compiler Design
Intermediate code generation in Compiler DesignIntermediate code generation in Compiler Design
Intermediate code generation in Compiler Design
 
Context free grammars
Context free grammarsContext free grammars
Context free grammars
 
Knapsack problem using fixed tuple
Knapsack problem using fixed tupleKnapsack problem using fixed tuple
Knapsack problem using fixed tuple
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
 
Operator precedence
Operator precedenceOperator precedence
Operator precedence
 
Operator precedance parsing
Operator precedance parsingOperator precedance parsing
Operator precedance parsing
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 

Semelhante a Type checking compiler construction Chapter #6

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 1Jamshid Hashimi
 
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 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 Wayakshay rajpure
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming languageAbhishek Soni
 
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
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdfMdAshik35
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingEric Chou
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxMurali M
 
Introduction to c converted-converted
Introduction to c converted-convertedIntroduction to c converted-converted
Introduction to c converted-convertedNabishaAK
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to AdvancedTalentica Software
 

Semelhante a Type checking compiler construction Chapter #6 (20)

Type Checking
Type CheckingType Checking
Type Checking
 
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
 
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)
 
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
 
Basic C Programming language
Basic C Programming languageBasic C Programming language
Basic C Programming language
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
C# Basics
C# BasicsC# Basics
C# Basics
 
Compiler lecture 07
Compiler lecture 07Compiler lecture 07
Compiler lecture 07
 
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++
 
basics dart.pdf
basics dart.pdfbasics dart.pdf
basics dart.pdf
 
lec 2.pptx
lec 2.pptxlec 2.pptx
lec 2.pptx
 
Type Systems
Type SystemsType Systems
Type Systems
 
Python Basics.pptx
Python Basics.pptxPython Basics.pptx
Python Basics.pptx
 
Python programming l2
Python programming l2Python programming l2
Python programming l2
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
 
Chapter 2: Elementary Programming
Chapter 2: Elementary ProgrammingChapter 2: Elementary Programming
Chapter 2: Elementary Programming
 
Csc240 -lecture_4
Csc240  -lecture_4Csc240  -lecture_4
Csc240 -lecture_4
 
C Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptxC Programming Lecture 3 - Elements of C.pptx
C Programming Lecture 3 - Elements of C.pptx
 
Introduction to c converted-converted
Introduction to c converted-convertedIntroduction to c converted-converted
Introduction to c converted-converted
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
 

Último

scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksMagic Marks
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VDineshKumar4165
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 

Último (20)

scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Thermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - VThermal Engineering-R & A / C - unit - V
Thermal Engineering-R & A / C - unit - V
 
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
Call Girls in South Ex (delhi) call me [🔝9953056974🔝] escort service 24X7
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 

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
  • 18.
  • 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