Chapter 5 - Type Checking
Chapter 5 - Type Checking
1 2
Cont'd ...
– It checks the type of objects and reports a type error in the case of a violation,
Type Conversions
– Conversion from one type to another type is known as implicit if it is to be done
and incorrect types are corrected.
automatically by the compiler.
– Whatever the compiler we use, while it is compiling the program, it has to follow
– Implicit type conversions are also called Coercion and coercion is limited in many
the type rules of the language.
languages.
– Every language has its own set of type rules for the language.
– Example: An integer may be converted to a real but real is not converted to an
– We know that the information about data types is maintained and computed by
integer. float a =20;
the compiler.
– The information about data types like INTEGER, FLOAT, CHARACTER, and all int b = a; // implicit conversion
– Conversion is said to be Explicit if the programmer writes something to do the
the other data types is maintained and computed by the compiler.
Conversion.
– The compiler contains modules, where the type checker is a module of a compiler int a=10
– Example
and its task is type checking. 3 c=float(a) 4
Page 1
Types of Type Checking Types of Type Checking Cont.….
• There are two kinds of type checking: – Examples of Static checks include:
1. Static Type Checking i. Type-checks
2. Dynamic Type Checking • A compiler should report an error if an operator is applied to an incompatible
1. Static Type Checking operand.
• Is type checking performed at compile time. • For example, if an array variable and function variable are added together.
• It checks the type variables at compile time, which means the type of the variable is ii. The flow of control checks
known at the compile time. • Statements that cause the flow of control to leave a construct must have
• It generally examines the program text during the translation of the program. someplace to which to transfer the flow of control.
• Using the type rules of a system, a compiler can infer from the source text that a • For example, a break statement in C causes control to leave the smallest
function(fun) will be applied to an operand (a) of the right type each time the expression enclosing while, for, or switch statement, an error occurs if such an enclosing
fun(a) is evaluated. 5 statement does not exist. 6
Cont'd ...
Types of Type Checking Cont.….
The Benefits of Static Type Checking
i. Uniqueness checks
• There are situations in which an object must be defined only once.
• Runtime Error Protection.
• There are situations in which an object must be defined only once. • It catches syntactic errors like spurious words or extra punctuation.
• For example, in Pascal an identifier must be declared uniquely, labels in a case • It catches wrong names like Math and Predefined Naming.
statement must be distinct, and else a statement in a scalar type may not be • Detects incorrect argument types.
represented. • It catches the wrong number of arguments.
iv. Name-related checks: • It catches wrong return types, like return "70", from a function that’s declared
• Sometimes the same name may appear two or more times. to return an int.
• For example in Ada, a loop may have a name that appears at the beginning and
end of the construct.
• The compiler must check that the same name is used at both places. 7 8
Page 2
Cont'd ... Cont'd ...
Disadvantages of Static Type Checking 2. Dynamic Type Checking:
• There are the following disadvantages of static type checking which are as follows: • The type checking being done at run time.
It can reduce programmer flexibility. • In Dynamic Type Checking, types are associated with values, not variables.
Many languages like APL and SNOBOL4, because of their dynamic type binding, • Implementations of dynamically type-checked languages runtime objects are
enable only dynamic type checking. generally associated with each other through a type tag,
In languages without declarations, there is no static type checking is possible. • which is a reference to a type containing its type information.
Static type checking likely to influence declarations, data control structures, and • Dynamic typing is more flexible.
supplies for independent compilation of subprograms.
• A static type system always restricts what can be conveniently expressed.
Static type checking is complex when a language enables a memory cell to save
• Dynamic typing results in more compact programs since it is more flexible
values of multiple types at various times during execution.
and does not require types to be spelled out.
9 10
Page 3
Cont'd ... Cont'd ...
• Programming with a static type system often requires more design and
Large programs tend to have portions that are rarely executed, so a program
implementation effort.
can be in use for a long time before dynamic type checking detects a type
• Languages like Pascal and C have static type checking.
error.
• Type checking is used to check the correctness of the program before its
Properties that depend on values computed at runtime are rarely checked.
execution.
• For example, imperative languages rarely check that an array index is
• The main purpose of type-checking is to check the correctness and data type
within bounds.
assignments and type-casting of the data types is syntactically correct or not
The fundamental hardware does not implement for dynamic type checking.
before its Execution.
It can manage the tags and increases the complexity.
• Whether it is syntactically correct or not before its execution.
• Static Type-Checking is also used to determine the amount of memory needed to
13 14
store the variable.
Figure: The Position of the Type checker – It produces a syntax tree, and after that, Intermediate Code Generation is done.
15
End!!! 16
Page 4