SlideShare uma empresa Scribd logo
1 de 15
Type Checking
Department of Computer Science & Engineering
Hamdard University Bangladesh
 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.
• Semantic Checks
Static – done during compilation
Dynamic – done during run-time
Type Checking
2
1.Identify the types that are available in the language
2.Identify the language constructs that have types associated with
them
3. Identify the semantic rules for the language
Process of designing a type checker
3
Static type checking
• Static type checking is done at compile-time.
• Obtained via declarations and stored in a master symbol table.
• After this information is collected, the types involved in each
operation are checked.
4
Type Checking
• Example of Static Checks:
– Type Checks
– Flow of Control Checks
– Uniqueness Checks
– Name-related Checks
5
 Implemented by including type information for each data location
at runtime.
 For example, a variable of type double would contain both the
actual double value and some kind of tag indicating "double type".
Dynamic type checking
6
Type Systems
• Collection of rules for assigning type expressions.
• A sound type system eliminates run-time type checking for
type errors.
• A programming language is strongly-typed, if every program
its compiler accepts will execute without type errors.
- Ex: int x[100]; … x[i]  most of the compilers cannot guarantee that
i will be between 0 and 99
7
Uses of type checking
• Assignments: When a variable is given a value by an
assignment, it must be verified that the type of the value is the
same as the declared type of the variable.
• Overloading: The same name is used for several different
operations over several different types.
8
• Polymorphism types: Some languages allow a function to be
poly-morphic, that is, to be defined over a large class of similar
types, e.g. over all arrays no matter what the types of the
elements are.
• Data structures: A data structure may define a value with
several components, or a value that may be of different types at
different times.
9
Type Expression
• The type of a language construct is denoted by a type
expression.
• A type expression can be:
– A basic type (also called primitive types)
• a primitive data type such as integer, real, char, boolean, …
– A type name
• a name can be used to denote a type expression.
10
(a) Array. If T is a type expression, then array(I, T ) is a type
expression denoting an array with elements of type T and
index range in I—e.g., array[1..10] of int == array(1..10,int);
(a) Product. If T1 e T2 are type expressions, then their Cartesian
Product
T1 × T2 is a type expression;
Type constructors
11
(c) Record: Similar to Product but with names for different fields (used to
access the components of a record). Example of a C record type: struct
{ double r;
int i;
}
(d) Pointer: If T is a type expression, then pointer(T ) is the
type expression “pointer to an object of type T ”;
(e) Function: If D is the domain and R the range of the function then we
denote its type by the type expression: D : R.
The mod operator has type, int × int : int. The Pascal function:
function f(a, b: char): int
has type:
char × char : int
12
A Simple Type Checking System
Program → Declaration;
Statement →Declaration; Declaration
| id: Type
Statement → Statement; Statement
| id := Expression
| if Expression then Statement
| while Expression do Statement
Expression → literal | num | id
| Expression mod Expression
| E[E] | E ↑ | E (E)
13
Type Checking of Expressions
E → id { E.type:=lookup(id.entry) }
E → literal { E.type:=char }
E → int { E.type:=int }
E → real { E.type:=real }
E → E1 mod E2 { if (E1.type=int and E2.type=int) then E.type:=int
else E.type:=type-error }
E → E1 [E2] { if (E2.type=int and E1.type=array(s,t)) then E.type:=t
else E.type:=type-error }
E → E1 ↑ { if (E1.type=pointer(t)) then E.type:=t
else E.type:=type-error }
14
Type Checking of Statements
S → id := E { if (id.type=E.type then S.type=void
else S.type=type-error }
S → if E then S1 { if (E.type=boolean then S.type=S1.type
else S.type=type-error }
S → while E do S1{ if (E.type=boolean then S.type=S1.type
else S.type=type-error }
15

Mais conteúdo relacionado

Mais procurados

Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in CompilersMahbubur Rahman
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMvikas dhakane
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generationVipul Naik
 
Type checking
Type checkingType checking
Type checkingrawan_z
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGJothi Lakshmi
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazationSiva Sathya
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representationSravanthi Emani
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler designAnul Chaudhary
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction toolsAkhil Kaushik
 

Mais procurados (20)

Ll(1) Parser in Compilers
Ll(1) Parser in CompilersLl(1) Parser in Compilers
Ll(1) Parser in Compilers
 
I. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHMI. AO* SEARCH ALGORITHM
I. AO* SEARCH ALGORITHM
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Chapter 6 intermediate code generation
Chapter 6   intermediate code generationChapter 6   intermediate code generation
Chapter 6 intermediate code generation
 
Unit iv(simple code generator)
Unit iv(simple code generator)Unit iv(simple code generator)
Unit iv(simple code generator)
 
Type checking
Type checkingType checking
Type checking
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
 
COMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time EnvironmentsCOMPILER DESIGN Run-Time Environments
COMPILER DESIGN Run-Time Environments
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Specification-of-tokens
Specification-of-tokensSpecification-of-tokens
Specification-of-tokens
 
Issues in knowledge representation
Issues in knowledge representationIssues in knowledge representation
Issues in knowledge representation
 
Multi Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing MachineMulti Head, Multi Tape Turing Machine
Multi Head, Multi Tape Turing Machine
 
Input-Buffering
Input-BufferingInput-Buffering
Input-Buffering
 
Peephole optimization techniques in compiler design
Peephole optimization techniques in compiler designPeephole optimization techniques in compiler design
Peephole optimization techniques in compiler design
 
Run time storage
Run time storageRun time storage
Run time storage
 
Compiler Chapter 1
Compiler Chapter 1Compiler Chapter 1
Compiler Chapter 1
 
Compiler construction tools
Compiler construction toolsCompiler construction tools
Compiler construction tools
 
Role-of-lexical-analysis
Role-of-lexical-analysisRole-of-lexical-analysis
Role-of-lexical-analysis
 

Semelhante a Type Checking(Compiler Design) #ShareThisIfYouLike

Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingEelco Visser
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdfMdAshik35
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to AdvancedTalentica Software
 
Primitive data types
Primitive data typesPrimitive data types
Primitive data typesbad_zurbic
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and PitfallsJava Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and PitfallsRakesh Waghela
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniquesvalarpink
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-cteach4uin
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointersSamiksha Pun
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programmingHarshita Yadav
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfYRABHI
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf2b75fd3051
 

Semelhante a Type Checking(Compiler Design) #ShareThisIfYouLike (20)

Type Checking
Type CheckingType Checking
Type Checking
 
Type Systems
Type SystemsType Systems
Type Systems
 
Compiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type CheckingCompiler Construction | Lecture 7 | Type Checking
Compiler Construction | Lecture 7 | Type Checking
 
12TypeSystem.pdf
12TypeSystem.pdf12TypeSystem.pdf
12TypeSystem.pdf
 
Ch6.ppt
Ch6.pptCh6.ppt
Ch6.ppt
 
Typescript: Beginner to Advanced
Typescript: Beginner to AdvancedTypescript: Beginner to Advanced
Typescript: Beginner to Advanced
 
Primitive data types
Primitive data typesPrimitive data types
Primitive data types
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and PitfallsJava Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
 
Variables&DataTypes.pptx
Variables&DataTypes.pptxVariables&DataTypes.pptx
Variables&DataTypes.pptx
 
Problem Solving Techniques
Problem Solving TechniquesProblem Solving Techniques
Problem Solving Techniques
 
3 data-types-in-c
3 data-types-in-c3 data-types-in-c
3 data-types-in-c
 
Data Handling
Data HandlingData Handling
Data Handling
 
Data types
Data typesData types
Data types
 
Python with data Sciences
Python with data SciencesPython with data Sciences
Python with data Sciences
 
arrays and pointers
arrays and pointersarrays and pointers
arrays and pointers
 
data types in C programming
data types in C programmingdata types in C programming
data types in C programming
 
cassignmentii-170424105623.pdf
cassignmentii-170424105623.pdfcassignmentii-170424105623.pdf
cassignmentii-170424105623.pdf
 
LEARN C# PROGRAMMING WITH GMT
LEARN C# PROGRAMMING WITH GMTLEARN C# PROGRAMMING WITH GMT
LEARN C# PROGRAMMING WITH GMT
 
CSC111-Chap_02.pdf
CSC111-Chap_02.pdfCSC111-Chap_02.pdf
CSC111-Chap_02.pdf
 
Java Basics
Java BasicsJava Basics
Java Basics
 

Mais de United International University

ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)
ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)
ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)United International University
 
All types of model(Simulation & Modelling) #ShareThisIfYouLike
All types of model(Simulation & Modelling) #ShareThisIfYouLikeAll types of model(Simulation & Modelling) #ShareThisIfYouLike
All types of model(Simulation & Modelling) #ShareThisIfYouLikeUnited International University
 
System imolementation(Modern Systems Analysis and Design)
System imolementation(Modern Systems Analysis and Design)System imolementation(Modern Systems Analysis and Design)
System imolementation(Modern Systems Analysis and Design)United International University
 
Free Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFSFree Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFSUnited International University
 

Mais de United International University (20)

Digital Devices (3rd chapter-2nd part)
Digital Devices (3rd chapter-2nd part)Digital Devices (3rd chapter-2nd part)
Digital Devices (3rd chapter-2nd part)
 
Network Topology (partial)
Network Topology (partial)Network Topology (partial)
Network Topology (partial)
 
Corona prediction from symptoms v1.4
Corona prediction from symptoms v1.4Corona prediction from symptoms v1.4
Corona prediction from symptoms v1.4
 
Introduction to Cloud Computing
Introduction to Cloud ComputingIntroduction to Cloud Computing
Introduction to Cloud Computing
 
ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)
ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)
ICT-Number system.সংখ্যা পদ্ধতি(৩য় অধ্যায়-১ম অংশ)
 
Wireshark Lab HTTP, DNS and ARP v7 solution
Wireshark Lab HTTP, DNS and ARP v7 solutionWireshark Lab HTTP, DNS and ARP v7 solution
Wireshark Lab HTTP, DNS and ARP v7 solution
 
Wireshark lab ssl v7 solution
Wireshark lab ssl v7 solutionWireshark lab ssl v7 solution
Wireshark lab ssl v7 solution
 
Network Security(MD5)
Network Security(MD5)Network Security(MD5)
Network Security(MD5)
 
Secure Electronic Transaction
Secure Electronic TransactionSecure Electronic Transaction
Secure Electronic Transaction
 
Oracle installation
Oracle installationOracle installation
Oracle installation
 
IEEE 802.11 Project
IEEE 802.11 ProjectIEEE 802.11 Project
IEEE 802.11 Project
 
SONET-Communication Engineering
SONET-Communication EngineeringSONET-Communication Engineering
SONET-Communication Engineering
 
Security Issues for Cellular Telephony
Security Issues for Cellular TelephonySecurity Issues for Cellular Telephony
Security Issues for Cellular Telephony
 
All types of model(Simulation & Modelling) #ShareThisIfYouLike
All types of model(Simulation & Modelling) #ShareThisIfYouLikeAll types of model(Simulation & Modelling) #ShareThisIfYouLike
All types of model(Simulation & Modelling) #ShareThisIfYouLike
 
System imolementation(Modern Systems Analysis and Design)
System imolementation(Modern Systems Analysis and Design)System imolementation(Modern Systems Analysis and Design)
System imolementation(Modern Systems Analysis and Design)
 
Making Complex Decisions(Artificial Intelligence)
Making Complex Decisions(Artificial Intelligence)Making Complex Decisions(Artificial Intelligence)
Making Complex Decisions(Artificial Intelligence)
 
Free Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFSFree Space Management, Efficiency & Performance, Recovery and NFS
Free Space Management, Efficiency & Performance, Recovery and NFS
 
Overview of Computer Graphics
Overview of Computer GraphicsOverview of Computer Graphics
Overview of Computer Graphics
 
Keyboard & Mouse basics
Keyboard & Mouse basics Keyboard & Mouse basics
Keyboard & Mouse basics
 
Organization of a computer
Organization of a computerOrganization of a computer
Organization of a computer
 

Último

cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabusViolet Violet
 
Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...soginsider
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxrealme6igamerr
 
Landsman converter for power factor improvement
Landsman converter for power factor improvementLandsman converter for power factor improvement
Landsman converter for power factor improvementVijayMuni2
 
Nodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptxNodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptxwendy cai
 
Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...
Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...
Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...amrabdallah9
 
nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxjasonsedano2
 
me3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Ame3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Akarthi keyan
 
A Seminar on Electric Vehicle Software Simulation
A Seminar on Electric Vehicle Software SimulationA Seminar on Electric Vehicle Software Simulation
A Seminar on Electric Vehicle Software SimulationMohsinKhanA
 
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....santhyamuthu1
 
Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...
Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...
Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...Amil baba
 
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docxSUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docxNaveenVerma126
 
EPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptxEPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptxJoseeMusabyimana
 
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...Sean Meyn
 
Design of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptxDesign of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptxYogeshKumarKJMIT
 
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS Bahzad5
 

Último (20)

Présentation IIRB 2024 Chloe Dufrane.pdf
Présentation IIRB 2024 Chloe Dufrane.pdfPrésentation IIRB 2024 Chloe Dufrane.pdf
Présentation IIRB 2024 Chloe Dufrane.pdf
 
cloud computing notes for anna university syllabus
cloud computing notes for anna university syllabuscloud computing notes for anna university syllabus
cloud computing notes for anna university syllabus
 
Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...Transforming Process Safety Management: Challenges, Benefits, and Transition ...
Transforming Process Safety Management: Challenges, Benefits, and Transition ...
 
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptxUNIT4_ESD_wfffffggggggggggggith_ARM.pptx
UNIT4_ESD_wfffffggggggggggggith_ARM.pptx
 
Landsman converter for power factor improvement
Landsman converter for power factor improvementLandsman converter for power factor improvement
Landsman converter for power factor improvement
 
Présentation IIRB 2024 Marine Cordonnier.pdf
Présentation IIRB 2024 Marine Cordonnier.pdfPrésentation IIRB 2024 Marine Cordonnier.pdf
Présentation IIRB 2024 Marine Cordonnier.pdf
 
Nodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptxNodal seismic construction requirements.pptx
Nodal seismic construction requirements.pptx
 
Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...
Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...
Strategies of Urban Morphologyfor Improving Outdoor Thermal Comfort and Susta...
 
nvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptxnvidia AI-gtc 2024 partial slide deck.pptx
nvidia AI-gtc 2024 partial slide deck.pptx
 
me3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part Ame3493 manufacturing technology unit 1 Part A
me3493 manufacturing technology unit 1 Part A
 
Lecture 2 .pptx
Lecture 2                            .pptxLecture 2                            .pptx
Lecture 2 .pptx
 
A Seminar on Electric Vehicle Software Simulation
A Seminar on Electric Vehicle Software SimulationA Seminar on Electric Vehicle Software Simulation
A Seminar on Electric Vehicle Software Simulation
 
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
SATELITE COMMUNICATION UNIT 1 CEC352 REGULATION 2021 PPT BASICS OF SATELITE ....
 
Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...
Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...
Best-NO1 Best Rohani Amil In Lahore Kala Ilam In Lahore Kala Jadu Amil In Lah...
 
計劃趕得上變化
計劃趕得上變化計劃趕得上變化
計劃趕得上變化
 
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docxSUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
SUMMER TRAINING REPORT ON BUILDING CONSTRUCTION.docx
 
EPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptxEPE3163_Hydro power stations_Unit2_Lect2.pptx
EPE3163_Hydro power stations_Unit2_Lect2.pptx
 
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
Quasi-Stochastic Approximation: Algorithm Design Principles with Applications...
 
Design of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptxDesign of Clutches and Brakes in Design of Machine Elements.pptx
Design of Clutches and Brakes in Design of Machine Elements.pptx
 
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS GENERAL CONDITIONS  FOR  CONTRACTS OF CIVIL ENGINEERING WORKS
GENERAL CONDITIONS FOR CONTRACTS OF CIVIL ENGINEERING WORKS
 

Type Checking(Compiler Design) #ShareThisIfYouLike

  • 1. Type Checking Department of Computer Science & Engineering Hamdard University Bangladesh
  • 2.  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. • Semantic Checks Static – done during compilation Dynamic – done during run-time Type Checking 2
  • 3. 1.Identify the types that are available in the language 2.Identify the language constructs that have types associated with them 3. Identify the semantic rules for the language Process of designing a type checker 3
  • 4. Static type checking • Static type checking is done at compile-time. • Obtained via declarations and stored in a master symbol table. • After this information is collected, the types involved in each operation are checked. 4
  • 5. Type Checking • Example of Static Checks: – Type Checks – Flow of Control Checks – Uniqueness Checks – Name-related Checks 5
  • 6.  Implemented by including type information for each data location at runtime.  For example, a variable of type double would contain both the actual double value and some kind of tag indicating "double type". Dynamic type checking 6
  • 7. Type Systems • Collection of rules for assigning type expressions. • A sound type system eliminates run-time type checking for type errors. • A programming language is strongly-typed, if every program its compiler accepts will execute without type errors. - Ex: int x[100]; … x[i]  most of the compilers cannot guarantee that i will be between 0 and 99 7
  • 8. Uses of type checking • Assignments: When a variable is given a value by an assignment, it must be verified that the type of the value is the same as the declared type of the variable. • Overloading: The same name is used for several different operations over several different types. 8
  • 9. • Polymorphism types: Some languages allow a function to be poly-morphic, that is, to be defined over a large class of similar types, e.g. over all arrays no matter what the types of the elements are. • Data structures: A data structure may define a value with several components, or a value that may be of different types at different times. 9
  • 10. Type Expression • The type of a language construct is denoted by a type expression. • A type expression can be: – A basic type (also called primitive types) • a primitive data type such as integer, real, char, boolean, … – A type name • a name can be used to denote a type expression. 10
  • 11. (a) Array. If T is a type expression, then array(I, T ) is a type expression denoting an array with elements of type T and index range in I—e.g., array[1..10] of int == array(1..10,int); (a) Product. If T1 e T2 are type expressions, then their Cartesian Product T1 × T2 is a type expression; Type constructors 11
  • 12. (c) Record: Similar to Product but with names for different fields (used to access the components of a record). Example of a C record type: struct { double r; int i; } (d) Pointer: If T is a type expression, then pointer(T ) is the type expression “pointer to an object of type T ”; (e) Function: If D is the domain and R the range of the function then we denote its type by the type expression: D : R. The mod operator has type, int × int : int. The Pascal function: function f(a, b: char): int has type: char × char : int 12
  • 13. A Simple Type Checking System Program → Declaration; Statement →Declaration; Declaration | id: Type Statement → Statement; Statement | id := Expression | if Expression then Statement | while Expression do Statement Expression → literal | num | id | Expression mod Expression | E[E] | E ↑ | E (E) 13
  • 14. Type Checking of Expressions E → id { E.type:=lookup(id.entry) } E → literal { E.type:=char } E → int { E.type:=int } E → real { E.type:=real } E → E1 mod E2 { if (E1.type=int and E2.type=int) then E.type:=int else E.type:=type-error } E → E1 [E2] { if (E2.type=int and E1.type=array(s,t)) then E.type:=t else E.type:=type-error } E → E1 ↑ { if (E1.type=pointer(t)) then E.type:=t else E.type:=type-error } 14
  • 15. Type Checking of Statements S → id := E { if (id.type=E.type then S.type=void else S.type=type-error } S → if E then S1 { if (E.type=boolean then S.type=S1.type else S.type=type-error } S → while E do S1{ if (E.type=boolean then S.type=S1.type else S.type=type-error } 15