Professional Documents
Culture Documents
Compiler CH 5
Compiler CH 5
Compiler CH 5
BY DAWIT H.
By: D. Nanne H. 2023/24
Type Checking
• Type checking is the process of verifying and enforcing constraints of types in values.
• A compiler must check if the source program follows the syntactic and semantic
conventions of the source language.
• Type checking allows the programmer to limit what types may be used in certain
circumstances and assigns types to values.
• The type-checker determines whether these values are used appropriately or not.
• It checks the type of objects and reports a type error in the case of a violation, and
incorrect types are corrected.
By: D. Nanne H.
Cont…
By: D. Nanne H.
Cont…
• The compiler contains modules, where the type checker is a module of a compiler and its task is
type checking.
Tasks:
• has to allow “Indexing is only on an array”
• has to check the range of data types used
• INTEGER (int) has a range of -32,768 to +32767
• FLOAT has a range of 1.2E-38 to 3.4E+38.
By: D. Nanne H.
Types of Type Checking
• There are two kinds of Semantic Checks or type checking:
• i. Static Type Checking.
• ii. Dynamic Type Checking.
• i. Static Type Checking:
• Here, checking is done by a compiler at compile time.
• It checks the type variables, i.e., type of the variable is known at the compile time.
• It examines the program text during the translation of the program.
• also used to determine the amount of memory needed to store the variable
By: D. Nanne H.
Cont...
Static checks include:
• Type-checks: A compiler should report an error if an operator is applied to an incompatible operand.
• Example: an error occurs if array variable is added with ‘function variable int a, c[10], d; d=c+d;//error
• flow of control checks: Statements that cause the flow of control to leave a construct must have
someplace to transfer the flow of control.
• Example: an error occurs when an enclosing statement such as ‘break’ does not exist in switch statements
• Uniqueness checks: There are situations in which an object must be defined only once.
• Example: int i, j, i;//error add( int a, int a){}//error
By: D. Nanne H.
Cont…
By: D. Nanne H.
Type systems
• Type system is a collection of rules implemented by the type checker for assigning
type expressions to the parts of a program.
• Different compilers may use different type systems for the same language
• A type checker implements a type system.
The design of the type-checker depends on:
• Syntactic Structure of language constructs.
• The Expressions of languages.
• The rules for assigning types to constructs (semantic rules).
By: D. Nanne H.
Cont…
• The Position of the Type checker in the Compiler:-
By: D. Nanne H.
Cont…
By: D. Nanne H.
Type Expressions
By: D. Nanne H.
Cont…
By: D. Nanne H.
Cont…
By: D. Nanne H.
Cont…
By: D. Nanne H.
Specifications of a type checker
• Many type-checking rules have the form; "If two type expressions are
equal then return a certain type else return type error."
• Ambiguities arise when names are given to two type expressions, and the
names are used in subsequent expressions.
• Type equivalence that implemented by a compiler can be explained using
the concepts of structural and name equivalence.
By: D. Nanne H.
Cont…
By: D. Nanne H.
Cont…
b. Name equivalence
• In some programming languages, we give a name to a type expression, and we use that
name as a type expression afterwards.
• Two type expressions are name equivalent if and only if they are identical.
• In structural equivalence, names are replaced by type expressions that they define,
• So two type expressions are structurally equivalent if they represent two structurally
equivalent type expressions when all names have been substituted.
• Example1: ptr and pointer (integer) are not name equivalent but they are structurally
equivalent.
By: D. Nanne H.
Type Conversions
By: D. Nanne H.
Cont…
• Of course, the machine cannot execute this operation as it involves different types of
values
• However, most languages accept such expressions to be used;
• The compiler will be in charge of converting one of the operand into the type of the
other.
• The type checker can be used to insert these conversion operations into the
intermediate representation of the source program
• For example, an operator inttoreal may be inserted whenever an operand need to
implicitly converted
By: D. Nanne H.
Cont…
Coercion
• Conversion from one type to another type is known as implicit if it is to be done
automatically by the compiler.
• Implicit-type conversions are also called Coercion and coercion is limited in many
languages.
• Example: An integer may be converted to a real but real is not converted to an integer.
• Conversion is said to be Explicit if the programmer writes something to do the
Conversion.
By: D. Nanne H.
THANKS!
By: D. Nanne H.