0% found this document useful (0 votes)
3 views4 pages

Chapter 5

Uploaded by

kaleabgemechu657
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views4 pages

Chapter 5

Uploaded by

kaleabgemechu657
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Chapter-5//Type Checking

Chapter-5
Type Checking
Introduction
Type checking is the process of verifying that a program is well-typed, meaning that
each variable is used in a way that is consistent with its type. A type checker can
catch type errors before the program is run, helping to prevent runtime type errors.
In a compiled language, type checking is typically performed by the compiler, which
checks the type of each variable and expression in the program and generates an error
if it detects a type mismatch. For example, if a variable of type int is used in an
operation with a variable of type string, the compiler will generate an error because
these types are incompatible.
Type checking can also be performed at runtime, but this is generally less efficient
because it requires the program to execute additional checks at runtime. In a compiled
language, type checking is typically performed before the program is run, which allows
for faster execution.
The information about data types like INTEGER, FLOAT, CHARACTER, and all the
other data types is maintained and computed by the compiler. The compiler contains
modules, where the type checker is a module of a compiler and its task is type
checking.
The Position of the Type checker in the Compiler:

Type Systems:
A type system is a set of rules that defines how types can be used in a programming
language. It specifies the possible types that variables can have, the operations that
can be performed on those types, and the type of the result of those operations.

NOTE: Type conversion can either be implicit (automatically done during code
execution) or explicit (done by you the developer).
Implicit Type Conversion is also known (and more commonly referred to) as Coercion
while Explicit Type Conversion is also known as Type Casting.

Type Conversions
Type conversions, also known as type casting, are the process of converting a value
from one type to another. This is often necessary when a value of one type is expected,

DR. GANGONE SWAPNA 1


Chapter-5//Type Checking

but a value of a different type is provided. For example, consider the following code in
a strongly typed language:
int x = 10;
float y = 3.14;

x = y;
The last line of this code will generate a type error because x is an int and y is a float,
and it is not allowed to assign a float to an int without an explicit type conversion. To
fix this error, we can add a type conversion to cast the float value to an int:

x = (int) y;
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.
Type conversions can also be implicit, meaning that the compiler will automatically
perform the conversion when it is safe to do so. For example, in the following code, the
value of x is automatically converted from an int to a float when it is assigned to y:

int x = 10;
float y;

y = x;
Not all type conversions are possible or safe. For example, it is not possible to convert
a value of type string to a value of type int without first parsing the string to extract a
numeric value. Attempting to perform an invalid or unsafe type conversion can result
in a runtime error.

Types of Type Checking:

There are two kinds of type checking:


1. Static Type Checking.

2. Dynamic Type Checking.

[Link] Type Checking:


Static type checking is defined as type checking performed at compile time. It checks
the type variables at compile-time, which means the type of the variable is known at
the compile time. It generally examines the program text during the translation of the
program. Using the type rules of a system, a compiler can infer from the source text

DR. GANGONE SWAPNA 2


Chapter-5//Type Checking

that a function (fun) will be applied to an operand (a) of the right type each time the
expression fun(a) is evaluated.
Examples of Static checks include:
• Type-checks: A compiler should report an error if an operator is applied to an
incompatible operand. For example, if an array variable and function variable
are added together.
• The flow of control checks: Statements that cause the flow of control to leave
a construct must have someplace to which to transfer the flow of control. For
example, a break statement in C causes control to leave the smallest enclosing
while, for, or switch statement, an error occurs if such an enclosing statement
does not exist.
• Uniqueness checks: There are situations in which an object must be defined
only once. For example, in Pascal an identifier must be declared uniquely,
labels in a case statement must be distinct, and else a statement in a scaler
type may not be represented.
• Name-related checks: Sometimes the same name may appear two or more
times. 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.
The Benefits of Static Type Checking:
1. Runtime Error Protection.

2. It catches syntactic errors like spurious words or extra punctuation.


3. It catches wrong names like Math and Predefined Naming.
4. Detects incorrect argument types.
5. It catches the wrong number of arguments.
6. It catches wrong return types, like return “70”, from a function that’s declared
to return an int.
Languages like Pascal and C have static type checking. Type checking is used to check
the correctness of the program before its execution. The main purpose of type-
checking is to check the correctness and data type assignments and type-casting of
the data types, whether it is syntactically correct or not before their execution.
Static Type-Checking is also used to determine the amount of memory needed to store
the variable.

DR. GANGONE SWAPNA 3


Chapter-5//Type Checking

[Link] Type Checking:


It is possible to implement dynamic type checking in a compiled language, but this is
less common because static type checking is generally more efficient and can catch
type errors at compile-time, rather than at runtime.
One way to implement dynamic type checking in a compiled language is to use a
runtime type information (RTTI) system, which stores type information for values at
runtime and allows the program to check the type of a value at runtime. This is
similar to the way that dynamically typed languages store and check type information,
but it requires additional overhead to store and retrieve the type information at
runtime.
Another way to implement dynamic type checking in a compiled language is to use a
just-in-time (JIT) compiler, which can generate machine code at runtime that includes
type checks. This can allow a compiled language to perform type checking
dynamically, but it comes with the overhead of generating and executing machine code
at runtime.
Overall, it is generally more efficient to use static type checking in a compiled
language, because it allows the compiler to catch type errors before the program is run
and eliminates the need for additional type checking at runtime. However, there may
be cases where dynamic type checking is necessary or desirable, and it is possible to
implement this feature in a compiled language using RTTI or a JIT compiler.

DR. GANGONE SWAPNA 4

You might also like