0% found this document useful (0 votes)
17 views

Chapter 5 - Type Checking

Static type checking is performed at compile time by examining the program text to check that variables are used according to their declared types. This catches many errors but reduces flexibility. Dynamic type checking is performed at runtime by associating type tags with values. It is more flexible but does not catch as many errors until runtime. Both approaches have advantages and disadvantages.

Uploaded by

yaredteshager01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Chapter 5 - Type Checking

Static type checking is performed at compile time by examining the program text to check that variables are used according to their declared types. This catches many errors but reduces flexibility. Dynamic type checking is performed at runtime by associating type tags with values. It is more flexible but does not catch as many errors until runtime. Both approaches have advantages and disadvantages.

Uploaded by

yaredteshager01
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Rules of Type Checking

– Type Checker is a module of a compiler devoted to type checking tasks.


Compiler Design – Type checking is the process of verifying and enforcing constraints of types in
values.
Chapter 5: Type Checking – A compiler must check that the source program should follow the syntactic and
semantic conventions of the source language and it should also check the type
rules of the language.
– It allows the programmer to limit what types may be used in certain circumstances
Instructor: Fikru T. (MSc.)
Email: [email protected] and assigns types to values.
– The type-checker determines whether these values are used appropriately or not.

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

Cont'd ... Cont'd ...


Advantage of Dynamic Type Checking Disadvantage of Dynamic Type Checking
• There are the following advantages of dynamic type checking which are as follows • Disadvantages of dynamic type checking which are as follows:
There is no flexibility in program design.  The dynamic type checking takes up more space as the dynamic type checking
There is no requirement for declarations. involves inserting extra code into the program to detect impending errors.
The type of data object associated with a variable name can be changed as  Programs are difficult to debug as all possible execution paths are not explored
per need during the program execution. during program testing.
Dynamic type checking can find many errors that cannot be identified by  The dynamic type checking takes more time which reduces the speed of
static type checking. executing the operation.
In most languages, static type checking is not possible for some language  It is less efficient than static type checking.
constructs in certain cases but the same purpose can be achieved by dynamic  It is more expensive.
type checking. 11  Errors can lurk in a program until they are reached during execution. 12

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.

Cont'd ... Cont'd ...


The design of the type-checker depends on: – The token streams from the lexical analyzer are passed to the PARSER.
– Syntactic Structure of language constructs. – The PARSER will generate a syntax tree.
– The Expressions of languages. – When a program (source code) is converted into a syntax tree, the type-checker
– The rules for assigning types to constructs (semantic rules). plays a Crucial Role.
– So, by seeing the syntax tree, you can tell whether each data type is handling the
correct variable or not.
– The Type-Checker will check and if any modifications are present, then it will
modify.

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

You might also like