# Syntax-Directed Translation (SDT), Three-Address Code & Register Liveness
Exam: GATE Computer Science & Information Technology (GATE CS)
Subject: Compiler Design
Canonical URL: https://gateandnet.com/exams/gate-cs/compiler-design/sdt-three-address-code-and-liveness

> Synthesized vs Inherited attributes, S-attributed vs L-attributed SDTs, Basic Block leader identification, SSA static single assignment, and Chaitin register interference graph coloring.

## 1. Synthesized vs Inherited Attributes & SDT Classes

- **Synthesized Attribute:** Computed at parent node $A$ strictly from attributes of $A$'s **children** (and tokens at $A$). Evaluated naturally during **bottom-up LR parsing**.
- **Inherited Attribute:** Computed at child node $X_i$ from $X_i$'s **parent** and/or **siblings**.

| SDT Class | Allowed Attributes | Compatible Parser Evaluation |
| :--- | :--- | :--- |
| **S-Attributed SDT** | **Synthesized ONLY** (semantic actions at right end of production) | Bottom-up LR stack evaluation |
| **L-Attributed SDT** | Synthesized + Inherited ONLY from **parent** or **LEFT siblings** ($X_1, \dots, X_{i-1}$) | Top-down LL(1) / Depth-First Left-to-Right traversal |

Every S-attributed SDT is automatically L-attributed ($	ext{S-Attributed} \subsetneq 	ext{L-Attributed}$).

## 2. Basic Blocks, Leaders & SSA Form

In Three-Address Code (TAC), **Leader** instructions partition code into **Basic Blocks**:
1. The **very first instruction** of the intermediate code is a leader.
2. Any instruction that is the **target of a conditional or unconditional jump** is a leader.
3. Any instruction that **immediately follows** a conditional or unconditional jump is a leader.

- **Static Single Assignment (SSA):** Every variable is assigned **at most once** statically ($x_1, x_2, \dots$), inserting $\phi$-functions at control-flow merge points.
- **Minimum Registers via Graph Coloring:** Construct the **Register Interference Graph** where vertices = variables and an edge $(u, v)$ exists iff $u$ and $v$ are **simultaneously live**. Minimum registers without spilling = **Chromatic Number $\chi(G)$**!

## Solved Practice Questions & PYQs

### [GATE CS Compiler | MCQ]
In an **L-Attributed Syntax-Directed Definition** for production $A \to X_1 X_2 X_3$, an inherited attribute of symbol $X_2$ may depend on:
**Correct Answer:** B
**Solution:** In an **L-attributed SDT**, an inherited attribute of $X_i$ can depend ONLY on **inherited attributes of parent $A$** and **any attributes of left siblings $X_1, \dots, X_{i-1}$** — never on right siblings like $X_3$.

### [GATE CS Compiler | MCQ]
In global register allocation via Chaitin’s algorithm, finding the **minimum number of CPU registers** needed for live variables without spilling reduces directly to:
**Correct Answer:** B
**Solution:** Two variables simultaneously live at any program point share an edge in the **Register Interference Graph** and cannot occupy the same register. Assigning $k$ registers without spilling is equivalent to **$k$-vertex graph coloring**.
