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.
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]
2080 · 5 marks
What are the advantages of intermediate code? What types of information are provided by symbol table? [5]
2078 · 5 marks
What are the typical entries made in symbol table? Explain. [5]
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 answerHide 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
Pis the parent of node forQif control flows fromPintoQ(i.e.,PcallsQ). - 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 infact(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)$:
| Op | Arg1 | Arg2 | Result |
|---|---|---|---|
* | b | c | t1 |
* | b | d | t2 |
+ | t1 | t2 | t3 |
= | 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.
2080 · 5 marks
What is activation tree? Define type checking system with examples. [5]
Show solved answerHide 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
Acalls procedureB, then the node forBis a child of the node forA. - 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
maincallsAandBsequentially.AcallsC.- 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:
| Type | Description |
|---|---|
| Static Type Checking | Performed at compile time |
| Dynamic Type Checking | Performed at runtime |
Type Checking Rules:
- Type of an expression is determined by the types of its sub-expressions.
- If an operator is applied to incompatible types, a type error is reported.
- 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
aisintandbisfloat. - 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 (
intvschar *). - 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.
2078 · 5 marks
List out different types of run time storage management techniques. Explain any one of them. [5]
2075 · 6 marks
What is activation record? Discuss the different activities performed by caller and callee during procedure call and return.[6]
Make Unit 3 stick
Practice CSC376 with flashcards & quizzes