2080 10

CSC376 · 2080 · 5 marks

What is activation tree? Define type checking system with examples.

What is activation tree? Define type checking system with examples. [5]

Compiler Design and ConstructionChapter 3: Symbol Table and Run-time StorageRun-time storage management

Quick answer

An activation tree is a tree data structure used to represent the sequence and nesting of procedure/function calls during the execution of a program at runtime.

Activation Tree and Type Checking System


Part 1: Activation Tree

Key Properties:

  • Each node in the tree represents an activation (call) of a procedure.
  • The root represents the activation of the main program.
  • If procedure A calls procedure B, then the node for B is a child of the node for A.
  • The execution of a called procedure must complete before the calling procedure resumes (LIFO order), which is why a stack is used to manage activations at runtime.

Example:

Consider the following program:

main() {
    A();
    B();
}
A() {
    C();
}
B() { }
C() { }

The activation tree is:

        main
       /    \
      A      B
      |
      C
  • main calls A and B sequentially.
  • A calls C.
  • The left-to-right order of children represents the order of calls.

Note: The activation tree directly corresponds to the stack storage allocation model, where activation records are pushed and popped in LIFO order as activations begin and end.


Part 2: Type Checking System

Definition:

A type checking system is a component of the semantic analyzer in a compiler that verifies whether the operands and operators in expressions are type-compatible according to the rules of the source language. It ensures that operations are applied to correct types of data, preventing type errors.

As per the notes: "Type-checking is an important part of semantic analyzer."

Types of Type Checking:

TypeDescription
Static Type CheckingPerformed at compile time
Dynamic Type CheckingPerformed at runtime

Type Checking Rules:

  1. Type of an expression is determined by the types of its sub-expressions.
  2. If an operator is applied to incompatible types, a type error is reported.
  3. Type coercion (implicit conversion) may be applied where allowed.

Examples:

Example 1: Arithmetic Expression

int a = 5;
float b = 3.2;
float c = a + b;   // Valid: int is coerced to float
  • Type checker detects a is int and b is float.
  • Applies widening coercion: int → float.
  • Result type is float. No error.

Example 2: Type Mismatch Error

int x = 10;
char *p = x;   // Type Error: cannot assign int to pointer
  • Type checker detects incompatible types (int vs char *).
  • Reports a type error at compile time.

Example 3: Function Call Type Checking

int add(int a, int b) { return a + b; }
add(3, "hello");   // Type Error: second argument is string, expected int
  • Type checker verifies that actual parameter types match formal parameter types.
  • Reports a type mismatch error.

Type Checking System Summary:

Source Code
    |
    v
Lexical Analyzer --> Tokens
    |
    v
Syntax Analyzer --> Parse Tree
    |
    v
Semantic Analyzer (Type Checker)
    --> Checks type compatibility
    --> Reports type errors
    --> Annotates parse tree with type info
    |
    v
Intermediate Code Generator

The type checking system ensures type safety and produces type-annotated intermediate representations for further compilation phases.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.