Type Checking in Compiler Design
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 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:
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).