ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›Type Checking and Environments
    Compiler ConstructionTopic 24 of 32

    Type Checking and Environments

    5 minread
    803words
    Beginnerlevel

    🧠 Type Checking and Environments (Compiler Construction)

    Type checking and environments are core concepts of Semantic Analysis in a compiler. They ensure that a program is logically correct and consistent in data usage.


    📌 1. Type Checking

    📖 Definition

    Type checking is the process of verifying that operations in a program are performed on compatible data types according to language rules.

    👉 It ensures that expressions, assignments, and function calls use correct types.


    ⚙️ Why Type Checking is Needed?

    • Prevents illegal operations
    • Ensures program correctness
    • Helps catch errors before execution
    • Improves reliability of compiled code

    🔍 Example of Correct Type Usage

    int a = 5;
    int b = 10;
    int c = a + b;
    

    ✔ Valid because all operands are integers


    ❌ Example of Type Error

    int x = "hello";   // ❌ type mismatch
    

    👉 String cannot be assigned to integer


    🧠 2. Types of Type Checking

    🔹 1. Static Type Checking (MOST IMPORTANT)

    📖 Definition

    Type checking performed at compile time.

    Examples:

    • C
    • C++
    • Java

    Advantages:

    • Errors detected early
    • Faster execution
    • No runtime overhead

    🔹 2. Dynamic Type Checking

    📖 Definition

    Type checking performed at runtime.

    Examples:

    • Python
    • JavaScript

    Advantages:

    • Flexible
    • Easier coding

    Disadvantage:

    • Errors occur during execution

    📊 Comparison Table

    Feature Static Dynamic
    Time Compile time Runtime
    Languages C, C++ Python
    Speed Fast Slower
    Error detection Early During execution

    🧠 3. Rules of Type Checking

    🔹 1. Assignment Rule

    Left-hand side must match right-hand side type.

    int x = 10.5;  // ❌ mismatch
    

    🔹 2. Operator Rule

    Operands must be compatible.

    int a = 10 + "abc";  // ❌ invalid
    

    🔹 3. Function Call Rule

    Arguments must match parameter types.

    int add(int a, int b);
    add(10.5, 20);  // ❌ mismatch
    

    🔹 4. Return Type Rule

    Return value must match function type.

    int f() {
       return "hello";  // ❌ error
    }
    

    🧠 4. Type Expressions

    📖 Definition

    A type expression describes the structure of a type in a program.


    🔹 Basic Types

    • int
    • float
    • char
    • bool

    🔹 Constructed Types

    1. Array Type

    int a[10];
    

    Type expression:

    array(10, int)
    

    2. Pointer Type

    int *p;
    

    Type expression:

    pointer(int)
    

    3. Function Type

    int f(int, float);
    

    Type expression:

    (int, float) → int
    

    🧠 5. Type Conversion (Coercion)

    📖 Definition

    Automatic or manual conversion of one type into another.


    🔹 1. Implicit Conversion (Automatic)

    float x = 10;   // int → float
    

    🔹 2. Explicit Conversion (Type Casting)

    int x = (int) 10.5;
    

    ⚠️ Key Exam Point

    • Implicit = done by compiler
    • Explicit = done by programmer

    🧠 6. Environments (Very Important Concept)

    📖 Definition

    An environment is a mapping between names (variables/functions) and their meaning (types, memory location, values) at a particular point in a program.

    👉 In simple words:

    Environment = information about variables at a given time


    🧠 7. Types of Environments

    🔹 1. Static Environment

    • Created at compile time
    • Used in static scoping languages

    Example:

    C, C++


    🔹 2. Dynamic Environment

    • Created at runtime
    • Changes during program execution

    Example:

    Python, JavaScript


    🧠 8. Static vs Dynamic Scope Environment

    📊 Comparison Table

    Feature Static Environment Dynamic Environment
    Time Compile time Runtime
    Binding Fixed Changes dynamically
    Speed Faster Slower
    Example C, C++ Python

    🧠 9. Symbol Table as Environment

    📖 Definition

    A symbol table is a data structure used to implement the environment.

    It stores:

    • Variable names
    • Data types
    • Scope levels
    • Memory locations

    📊 Example Symbol Table

    Name Type Scope Address
    x int global 1000
    y float local 2000

    🧠 10. Relationship Between Type Checking and Environment

    ✔ Environment stores information about variables ✔ Type checking uses environment to verify correctness

    👉 Example: To check:

    x = y + z;
    

    Compiler checks:

    • Types of y and z from environment
    • Whether x is declared
    • Whether assignment is valid

    📌 11. Key Exam Points

    ✔ Type checking ensures correct use of data types ✔ Two types: Static and Dynamic ✔ Uses type rules (assignment, function, operators) ✔ Environments store variable meaning and scope ✔ Symbol table is implementation of environment ✔ Type expressions define structure of types


    📊 FINAL REVISION TABLE

    🔹 Type Checking

    Concept Description
    Static type checking Compile-time checking
    Dynamic type checking Runtime checking
    Type error Mismatch of types
    Coercion Type conversion
    Type expression Structure of type

    🔹 Environment

    Concept Meaning
    Environment Mapping of names to meanings
    Static environment Compile-time mapping
    Dynamic environment Runtime mapping
    Symbol table Implementation of environment

    🎯 Final Exam Definition

    Type checking is the process of verifying that all operations in a program use compatible data types, while an environment is a mapping that stores information about identifiers such as their types, scope, and memory locations. The environment is used by the compiler during type checking to ensure semantic correctness of the program.


    Previous topic 23
    Tasks of Semantic Analyzer and Types of Errors
    Next topic 25
    Type Conversions: Implicit vs Explicit

    Past Papers

    Open this section to load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time5 min
      Word count803
      Code examples0
      DifficultyBeginner