0% found this document useful (0 votes)
50 views9 pages

Type Checking

Type checking is a technique used by compilers to verify that values follow the syntactic and semantic rules of a language. There are two main types of type checking: static type checking performed at compile time to check types and memory usage, and dynamic type checking performed at runtime to associate types with values. Static type checking helps prevent runtime errors and catches syntactic and semantic errors. Type expressions indicate the kind of language constructs and can include basic types, type names, and types constructed from operators applied to other type expressions. The type system defines rules for associating type expressions throughout a program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views9 pages

Type Checking

Type checking is a technique used by compilers to verify that values follow the syntactic and semantic rules of a language. There are two main types of type checking: static type checking performed at compile time to check types and memory usage, and dynamic type checking performed at runtime to associate types with values. Static type checking helps prevent runtime errors and catches syntactic and semantic errors. Type expressions indicate the kind of language constructs and can include basic types, type names, and types constructed from operators applied to other type expressions. The type system defines rules for associating type expressions throughout a program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Type Checking

The technique of verifying and enforcing type restrictions in values is 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.
 The type-checker determines whether these values are used
appropriately or not.
 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.
Types of Type Checking

There are two kinds of type checking:


• Static Type Checking.
• Dynamic Type Checking.
Static 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.
 Static Type-Checking is also used to determine the amount of memory needed
to store the variable.

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 scalar 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

• Runtime Error Protection.


• It catches syntactic errors like spurious words or
extra punctuation.
• It catches wrong names like Math and Predefined
Naming.
• Detects incorrect argument types.
• It catches the wrong number of arguments.
• It catches wrong return types, like return “70”,
from a function that’s declared to return an int.
Dynamic Type Checking

• Dynamic Type Checking is defined as the type


checking being done at run time. In Dynamic
Type Checking, types are associated with
values, not variables.
• For example, Python is a dynamically typed
language. It means that the type of a variable
is allowed to change over its lifetime. Other
dynamically typed languages are -Perl, Ruby,
PHP, Javascript etc.
Type Expression

• A "type expression" will indicate the kind of a language construct. A type


expression can be a simple type or one that is created by applying an operator
called the type constructor to other type expressions. The basic types and
constructors are determined by the language being checked.

The definitions of type expressions are as follows:

• Type expressions are basic types such as boolean, char, integer, and real. During
type checking, a basic type-type_error will signify an error; void, which denotes
"the lack of a value," allows statements to be checked.
• A type name is a type expression since type expressions can be named.
• A type expression is a type constructor applied to the type expressions.
• Variables whose values are type expressions can be found in type expressions.
Type systems
• The type system is a set of rules for associating type expressions with
varying parts of the program. A type checker implements a type system.
Distinct compilers or processors of the system Language may employ
different type systems.

You might also like