Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
7 views4 pages

Compiler Assignment Saima Mumtaz

The document discusses type checking in programming languages, highlighting its importance in ensuring type correctness and preventing errors during compilation. It categorizes type checking into static and dynamic types, detailing their features, advantages, and disadvantages, along with examples. Additionally, it covers strong vs. weak typing and nominal vs. structural typing, providing insights into how different languages handle type compatibility.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Compiler Assignment Saima Mumtaz

The document discusses type checking in programming languages, highlighting its importance in ensuring type correctness and preventing errors during compilation. It categorizes type checking into static and dynamic types, detailing their features, advantages, and disadvantages, along with examples. Additionally, it covers strong vs. weak typing and nominal vs. structural typing, providing insights into how different languages handle type compatibility.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Assignment

Submitted to: Sir Nadeem Shoukat

Submitted By: Saima Mumtaz


(22-Arid-373)

Semester: Bscs-6(A)

Subject: Compiler Construction

Barani Institute of Sciences, Sahiwal


1. Introduction
Type checking is a critical phase in the compilation process. It ensures that the operations in a
program are applied to compatible data types. Type checking detects type errors and guarantees
the program adheres to the rules of the language's type system.
Key Objectives of Type Checking:
 To ensure type correctness in a program.
 To improve program reliability by preventing type-related errors.
 To aid in early error detection during compilation.

2. What is Type Checking?


Type checking refers to the process of verifying and enforcing the constraints of types in
a programming language. It involves examining the types of variables, functions, and
expressions to ensure they are used correctly.

Example:

int x = 10;
float y = 3.5;
int z = x + y; // Error: Type mismatch as x (int) and y (float) are incompatible

In this example, the compiler will identify a type mismatch error unless implicit or
explicit type conversion (casting) is applied.

3. Types of Type Checking


Type checking can be classified into two broad categories based on when the checks are
performed:

3.1. Static Type Checking

Static type checking is performed at compile-time. The compiler checks the types of variables,
expressions, and function calls before the program is executed.

 Key Features:
o Errors are detected early during compilation.
o Ensures better reliability and faster execution of code.
o Requires explicit type declarations (in most statically typed languages).
 Examples of Statically Typed Languages:
o C, C++, Java, Rust, and Go.
 Advantages:
o Early detection of errors.
o Better optimization of code by the compiler.
 Disadvantages:
o Reduces flexibility since types must be declared explicitly.
o Makes rapid prototyping more challenging.

Example in Java:

int number = "Hello"; // Error: Incompatible types, string cannot be assigned

to and integer variables

3.2. Dynamic Type Checking

Dynamic type checking is performed at runtime. The types of variables and expressions are
checked during the program's execution.

 Key Features:
o Errors are detected only when the program runs.
o Provides more flexibility in coding since type declarations are often unnecessary.
 Examples of Dynamically Typed Languages:
o Python, JavaScript, Ruby, and PHP.
 Advantages:
o Greater flexibility for developers.
o Enables faster prototyping and coding.
 Disadvantages:
o Errors are caught late, potentially leading to runtime crashes.
o Slower execution due to runtime type checks.

Example in Python:

x = 10 # Initially x is an integer

x = "Hello" # Now x becomes a string

print(x + 5) # Error: Unsupported operand types for +: 'str' and 'int'

4. Other Types of Type Checking


4.1. Strong Typing

A strongly typed language enforces strict rules about type compatibility. Even implicit type
conversions are limited.
 Example: Python, Java

4.2. Weak Typing

A weakly typed language allows implicit conversions between incompatible types, which may
lead to unexpected results.

 Example: JavaScript, C

4.3. Nominal Typing vs. Structural Typing

 Nominal Typing: Type compatibility is determined by explicit declarations (e.g., C++,


Java).
 Structural Typing: Type compatibility is based on the structure or behavior of types
(e.g., TypeScript, Go).

You might also like