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

Type Checking in Compiler Design

Type checking is a crucial process in compiler design that verifies and enforces type constraints in programming languages. It can be categorized into static type checking, which occurs at compile time, and dynamic type checking, which happens at runtime, each with its own advantages and disadvantages. The type checker ensures that values are used correctly according to the language's type rules and can modify the syntax tree accordingly.

Uploaded by

nikhilmishra1710
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)
58 views

Type Checking in Compiler Design

Type checking is a crucial process in compiler design that verifies and enforces type constraints in programming languages. It can be categorized into static type checking, which occurs at compile time, and dynamic type checking, which happens at runtime, each with its own advantages and disadvantages. The type checker ensures that values are used correctly according to the language's type rules and can modify the syntax tree accordingly.

Uploaded by

nikhilmishra1710
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
You are on page 1/ 4

Type Checking in Compiler Design

The technique of verifying and enforcing type constraints in values is known as type checking.
A compiler must ensure that the source program follows the source language's syntactic and
semantic conventions, as well as the language's type rules. It lets the programmer define which
kinds are allowed to be used in specific situations and assigns types to values. The type-checker
determines whether or not these values are being used correctly.

It checks the type of objects and, if there is a violation, reports a type error, and incorrect types
are corrected. Whatever compiler we pick, it must conform to the language's standard rules
while compiling the program. Each language has its own group of characters.

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.

Tasks:
1. has to allow “Indexing is only on an array”
2. has to check the range of data types used
3. INTEGER (int) has a range of -32,768 to +32767
4. FLOAT has a range of 1.2E-38 to 3.4E+38.

The Position of the Type checker in the Compiler:

Type checking in Compiler

The token streams from the lexical analyser are passed to the PARSER. The PARSER will
generate a syntax tree. When a program (source code) is converted into a syntax tree, the type-
checker 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. It produces a syntax tree, and after that, INTERMEDIATE
CODE Generation is done.
Types of Type Checking:

There are two kinds of type checking:


1. Static Type Checking.
2. Dynamic Type Checking.

Static Type Checking:


Type checking performed at compile time is referred to as static type checking. The type
variables are checked at compile-time, indicating that the type of the variable is known at
compilation time. During the translation of the program, it normally evaluates the program text.
A compiler can infer from the source text that a function (fun) will be applied to an operand
(a) of the correct type each time the expression fun (a) is evaluated using the system's type
rules.

Advantage of Static Type Checking


There are the following advantages of static type checking which are as follows −
• The early detection of errors at compile time.
• It is used to checking all execution paths.
• There is no requirement of type tags on data objects at runtime.
• A consequential gain in the adaptability of storage use.
• A consequential gain in execution rate.

Disadvantages of Static Type Checking


There are the following disadvantages of static type checking which are as follows −
• It can reduce programmer flexibility.
• Many languages like APL and SNOBOL4, because of their dynamic type
binding, enable only dynamic type checking.
• In languages without declarations, there is no static type checking is possible.
• Static type checking likely to influence declarations, data control structures, and
supplies for independent compilation of subprograms.
• Static type checking is complex when a language enables a memory cell to
save values of multiple types at various times during execution.
Dynamic Type Checking:

Type checking which occurs at runtime is referred to as dynamic type checking. Types, not
variables, are connected with values in Dynamic Type Checking. A type tag, which is a
reference to a type holding its type information, is used to link implementations of dynamically
type-checked languages runtime objects together.
Dynamic typing is more flexible. A static type system always restricts what can be
conveniently expressed. Dynamic typing results in more compact programs since it is more
flexible and does not require types to be spelled out. Programming with a static type system
often requires more design and implementation effort.
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 is syntactically
correct or not before its Execution. Whether it is syntactically correct or not before its
execution.
Static Type-Checking is also used to determine the amount of memory needed to store the
variable.
The design of the type-checker depends on:
1. Syntactic Structure of language constructs.
2. The Expressions of languages.
3. The rules for assigning types to constructs (semantic rules).

Advantages of Dynamic Type Checking


There are the following advantages of dynamic type checking which are as follows −
• There is no flexibility in program design.
• There is no requirement for declarations.
• The type of data object associated with a variable name can be changed as per
need during the program execution.
• Dynamic type checking can find many errors that cannot be identified by static
type checking.
• In most languages, static type checking is not possible for some language
constructs in certain cases but the same purpose can be achieved by dynamic
type checking.

Disadvantage of Dynamic Type Checking


There are the following disadvantages of dynamic type checking which are as follows

• The dynamic type checking takes up more space as the dynamic type checking
involves inserting extra code into the program to detect impending errors.
• Programs are difficult to debug as all possible execution paths are not explored
during program testing.
• The dynamic type checking takes more time which reduces the speed of
executing the operation.
• It is less efficient than static type checking.
• It is more expensive.
• Errors can lurk in a program until they are reached during execution.
• Large programs tend to have portions that are rarely executed, so a program
can be in use for a long time before dynamic type checking detects a type error.
• Properties that depend on values computed at runtime are rarely checked. For
example, imperative languages rarely check that an array index is within
bounds.
• The fundamental hardware does not implement for dynamic type checking.
• It can manage the tags and increases the complexity.

You might also like