Table of contents
1.
Introduction
2.
Types and Type Checking
3.
Type Expressions
4.
Environments for type checking
5.
Frequently Asked Questions
5.1.
What is type checking?
5.2.
What is typeExp in type checking?
5.3.
What is the purpose of type checking?
5.4.
What is type conversion in compiler design?
5.5.
What are the types of type checking?
6.
Conclusion
Last Updated: Mar 27, 2024

Type checking- Environment and Expressions

Author Alok Pandey
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?
Compiler Design

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.

Also see, Type Checking a Program and Function Declarations

Types and Type Checking

  • The goal of type checking is to ensure that actions on a value are allowed.
  • A type is a collection of values that can be combined with a set of operations.
  • The type of an identifier is usually available from declarations, but the type of intermediate expressions may need to be kept care of.

Also see, Advanced Type Checking

Type Expressions

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:

  1. Type expressions include basic types such as boolean, char, integer, and real.
  2. A type name is a type expression since type expressions can be named.
  3. A type expression is a type constructor applied to type expressions.
    Constructors include:
    1. 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.
    2. Products: Suppose T1 and T2 are type expressions, then T1 X T2(Cartesian product) is also a type expression.
    3. 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.
    4. 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).
    5. 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.
  4. Variables whose values are type expressions can be found in type expressions. Vtable is the symbol table for variables.

 

illustration of Tree Representation

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 

Recommended Reading:

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Live masterclass