Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
The technique of checking and enforcing type restrictions in values is called type checking.
Most texts are filtered out by the compiler's Lexical Analysis and parsing phases. Still, these techniques don't address many programming languages with well-formed needs since they aren't always context-free and can't be checked by the membership of context-free grammar.
The type-checking phase of compiler design happens before a program's execution or translation (static typing), and the information is gathered for use by following stages, such as the translator, which will naturally integrate type computation with the actual translation.
Types have a structure that shall represent using type expression. A "type expression" is used to express the type of a language construct. It is either a basic type or is formed by applying a type constructor operator to other type expressions. The basic types and constructors are determined by the language being tested. The function CheckExp is responsible for type checking. Type expressions are described as follows:
Type expressions include basic types such as boolean, char, integer, and real.
A type name is a type expression since type expressions can be named.
A type expression is a type constructor applied to type expressions. Constructors include:
Records: The names are what distinguish a record from a product. A tuple made up of field names, and field types will be passed to the record type constructor.
Products: Suppose T1 and T2 are type expressions, then T1 X T2(Cartesian product) is also a type expression.
Functions: In computer languages, a function transfers a domain type D to a range type R. The type expression D →R denotes the type of such a function. ftable is the symbol table for functions. The error function is responsible for reporting errors. If we let this function return, the type-checker will continue reporting errors.
Pointers: When T is a type expression, pointer(T) is a type expression that denotes "pointer to an object of type T." Eg-var p↑ row declares the type pointer for variable p. (row).
Arrays: If T is a type expression, array (I, T) denotes the type of an array with items of type T and index set I.
Variables whose values are type expressions can be found in type expressions. Vtable is the symbol table for variables.
Environments for type checking
To bind functions and variables to their types, a symbol table is required.
One symbol table is for variables, while the other is for functions.
We need symbol tables to type-check the program since they connect variables and functions to their types.
We shall utilize two symbol tables, one for functions and the other for variables because functions and variables have different namespaces. An int or a bool type is assigned to a variable. The type of a function is made up of the types of its arguments and the type of its outcome. Function types are written as a parenthesized list of the argument types, an arrow, and the result type, such as (int, bool) → int for a function that takes two parameters of type int and bool and returns an integer.
Frequently Asked Questions
What is type checking?
The task of ensuring that an operator's operands are of compatible types is referred to as type checking.
What is typeExp in type checking?
It is a function that is responsible for type checking.
What is the purpose of type checking?
Type checking is a program analysis that confirms that the types used in the program are correct. The goal of type checking is to ensure that data is type safe. Type checking might take either at compile time or during runtime.
What is type conversion in compiler design?
A data type is changed into another data type by a compiler via type conversion.A programmer converts a data type into another data type using the casting operator in typecasting.
What are the types of type checking?
There are two primary methods of type checking: static type checking and dynamic type checking.
Conclusion
In this article, we have extensively discussed Type checking- Environment and Expressions and their implementation in compiler design.
We hope that this blog has helped you enhance your knowledge regarding Type checking- Environment and Expressions