3 Symbol Table And Run Time Storage

Compiler Design and Construction · Unit 3 · 4 hrs

Symbol Table and Run-time Storage

Exam-focused notes for Symbol Table and Run-time Storage (Compiler Design and Construction, CSC376): what the TU syllabus asks and how it has actually been tested, with 7 solved past questions from this unit.

Symbol Table and Run-time Storage in short

This chapter looks at the two supporting structures every phase of the compiler leans on: the symbol table that remembers every identifier's properties, and the run-time storage schemes that decide where a running program's variables actually live in memory.

Symbol Table and Run-time Storage: notes

Symbol Table Design: Function of Symbol Table

A symbol table is a data structure created and maintained by the compiler to keep track of every identifier appearing in the source program, along with everything the compiler has learned about it. It begins to fill up as early as lexical and syntax analysis and is consulted, and further updated, by every phase that follows.

  • Lexical analysis creates a new entry in the table the first time it recognizes a fresh identifier lexeme, initially holding little more than the identifier's name.
  • Syntax analysis adds structural information to that entry: the identifier's declared type, its scope, its dimensions if it is an array, and where in the source it is referenced.
  • Semantic analysis consults the table's type and scope information to check that every use of the identifier is consistent with how it was declared.
  • Intermediate code generation uses the table to create and track compiler-generated temporary names alongside the programmer's own identifiers.
  • Code optimization uses the attributes recorded in the table, such as whether an identifier's value is provably constant, to justify machine-independent transformations.
  • Target code generation uses the address information stored in the table to translate a reference to an identifier into a reference to an actual memory location or register.

Beyond simply storing facts, a symbol table exists to support a fixed set of functions: to hold the names of every entity in the program in one structured place; to let the compiler verify whether a given name has already been declared, catching both undeclared-identifier and duplicate-declaration errors; to determine the scope in which a name is visible (scope resolution); to retrieve any attribute associated with a given name on demand; to add new attributes to an existing entry as later phases learn more about it; and to remove a name, or an entire group of names, once the scope that declared them has closed.

Information provided by Symbol Table

Each entry in the symbol table is a small record of attributes describing one identifier, and the exact set of attributes a compiler stores depends on the language, but the following are used almost universally.

  • Name. The identifier's own text. Since names can be arbitrarily long, an entry commonly stores either the name directly or a pointer into a separate string table, so that the fixed-size part of the entry does not have to grow to accommodate long names.
  • Type. What kind of entity the identifier denotes (variable, function name, label, type name, and so on), and, if it denotes a variable, whether that variable's type is a basic type or a derived (constructed) type such as an array, pointer, or record.
  • Location. The offset within the program's storage at which the current binding of the identifier is valid; this is exactly the address information used later by code generation.
  • Scope. The region of the program text over which the current declaration of the identifier applies.
  • Other attributes, as the language and the identifier's kind require: an array's declared dimensions and bounds, a record's field layout, a function's parameter types and return type, or a function's default or actual argument values.

A symbol table is manipulated through a small, fixed set of operations regardless of how it is implemented underneath: allocate, which creates a new, empty symbol table (typically one per scope); free, which discards a symbol table and reclaims its storage once its scope has closed; insert, which adds a new name to the table and returns a pointer to its entry; lookup, which searches for a name and returns a pointer to its entry if found; set_attribute, which records an attribute value against a given entry; and get_attribute, which retrieves a previously recorded attribute from a given entry.

What this unit covers

  • Symbol Table Design: Function of Symbol Table
  • Information provided by Symbol Table
  • Attributes and Data Structures for symbol table
  • Run-time storage management

Information provided by Symbol Table

2081 · 5 marks

What types of information are stored in a symbol table? Discuss the activation record. [5]

A symbol table is a data structure used by compilers to hold information about source-program constructs. The information is collected incrementally by the analysis phases and used by the synthesis phases to generate target code. The following types of information are stored in a symbol table: - ...

Open this answer on its own page →

2080 · 5 marks

What are the advantages of intermediate code? What types of information are provided by symbol table? [5]

--- Intermediate code is a machine-independent representation generated between the front-end and back-end of a compiler. Its main advantages are: 1. Machine Independence Intermediate code is not tied to any specific target machine. The same intermediate representation can be used to generate cod...

Open this answer on its own page →

2078 · 5 marks

What are the typical entries made in symbol table? Explain. [5]

A symbol table is a data structure used by compilers to hold information about source-program constructs. Information is collected incrementally by the analysis phase and used by the synthesis phase to generate target code. --- Each entry in a symbol table corresponds to an identifier and contain...

Open this answer on its own page →

Run-time storage management

2081 · 5 marks

How do you represent recursion in an activation tree? Generate the three-address code for the following instruction: [5]

Show solved answer

Given

Part 1: Conceptual question about representing recursion in an activation tree. No numeric data.

Part 2: "Generate the three-address code for the following instruction."

Critical issue: The actual instruction/expression to be translated into three-address code is NOT provided in the question text. The statement ends with "the following instruction:" but no instruction, expression, or code fragment follows.

Therefore, the specific expression required for Part 2 is missing or unreadable, and three-address code cannot be generated for an expression that was not given. An expression such as a = b * c + b * d can only be treated as an illustration, not as the question's data.

I will fully answer Part 1 (conceptual, no data needed) and, for Part 2, explain the method and demonstrate it on a clearly-labelled illustrative example (marked as assumed, since the actual instruction is missing).


Solution

Part 1: Representing Recursion in an Activation Tree

An activation tree shows the flow of control among procedure activations during a program's execution.

Properties:

  • Each node = one activation (one call) of a procedure.
  • The root = the activation of main.
  • Node for P is the parent of node for Q if control flows from P into Q (i.e., P calls Q).
  • Left-to-right ordering reflects the temporal order in which activations begin.

Recursion representation: When a procedure calls itself, the activation tree contains several distinct nodes bearing the same procedure name, arranged as a chain (or subtree) descending from the calling node. Each recursive call is a separate activation with its own activation record (own parameters, locals, return address) pushed onto the control stack.

Example: fact(n) computing $n!$

$$ \text{fact}(4) \rightarrow \text{fact}(3) \rightarrow \text{fact}(2) \rightarrow \text{fact}(1) $$

        fact(4)
           |
        fact(3)
           |
        fact(2)
           |
        fact(1)
  • Each node is a distinct activation even though the procedure name repeats.
  • At any moment, the runtime stack holds exactly the activation records lying on the path from the root to the currently active node.
  • On return, fact(1)'s record is popped, control resumes in fact(2), and so on.

Thus recursion = repeated nodes of the same procedure along a root-to-leaf path, each with its own activation record on the stack.


Part 2: Three-Address Code Generation

Missing data: The question says "Generate the three-address code for the following instruction:" but no instruction/expression was supplied in the given text. I cannot produce the intended answer without it. Below I state the method and demonstrate it on an assumed illustrative expression (clearly marked as not from the question).

Method (syntax-directed translation):

  • $E \rightarrow id$: E.place = id
  • $E \rightarrow E_1 * E_2$: emit t = E1.place * E2.place
  • $E \rightarrow E_1 + E_2$: emit t = E1.place + E2.place
  • $S \rightarrow id = E$: emit id = E.place

Assumed example (illustrative only): $a = b * c + b * d$

$$ \begin{aligned} t_1 &= b * c \ t_2 &= b * d \ t_3 &= t_1 + t_2 \ a &= t_3 \end{aligned} $$

Quadruple form $(op, arg_1, arg_2, result)$:

OpArg1Arg2Result
*bct1
*bdt2
+t1t2t3
=t3-a

Caveat: If the actual exam instruction differs (e.g., x = (a + b) * (c + d) or a control statement), the code must be regenerated from that expression. The above b*c+b*d result is not verifiable because the source instruction was absent from the given question.

Open this answer on its own page →

2080 · 5 marks

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

Show solved answer

Activation Tree and Type Checking System


Part 1: Activation Tree

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.

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.

Open this answer on its own page →

2078 · 5 marks

List out different types of run time storage management techniques. Explain any one of them. [5]

There are three main types of run-time storage management techniques: 1. Static Storage Management 2. Stack-based (Dynamic) Storage Management 3. Heap-based Storage Management --- In static storage management, memory is allocated at compile time before the program begins execution. The size and p...

Open this answer on its own page →

2075 · 6 marks

What is activation record? Discuss the different activities performed by caller and callee during procedure call and return.[6]

An activation record is a block of memory used for managing information needed by a single execution of a procedure. It is also called a stack frame. Each time a procedure is called, a new activation record is pushed onto the runtime stack, and when the procedure returns, the record is popped off...

Open this answer on its own page →