Bird
Raised Fist0
Compiler Designknowledge~10 mins

Three-address code in Compiler Design - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Three-address code
Start with high-level expression
↓
Break expression into simple operations
↓
Assign each operation to a temporary variable
↓
Generate three-address instructions
↓
Use temporaries for next operations
↓
Repeat until full expression is converted
↓
End
The process breaks a complex expression into simple steps, each with at most three parts: two operands and one result, assigned to temporary variables.
Execution Sample
Compiler Design
a = b + c * d
Convert the expression into three-address code by breaking it into simpler steps with temporary variables.
Analysis Table
StepOperationOperandsTemporary VariableResulting Instruction
1Multiplyc, dt1t1 = c * d
2Addb, t1t2t2 = b + t1
3Assignt2aa = t2
4End---
💡 All parts of the expression are converted into simple three-address instructions.
State Tracker
VariableStartAfter Step 1After Step 2After Step 3
t1-c * dc * dc * d
t2--b + t1b + t1
a---b + (c * d)
Key Insights - 3 Insights
Why do we need temporary variables like t1 and t2?
Temporary variables hold intermediate results so each instruction has at most two operands and one result, as shown in steps 1 and 2 of the execution_table.
Why can't we write the whole expression in one instruction?
Three-address code limits each instruction to one operation for simplicity and easier optimization, so complex expressions are split into multiple steps as seen in the execution_table.
What does the 'Assign' operation do in step 3?
It stores the final computed value from temporary variable t2 into the original variable a, completing the translation.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the operation performed at step 2?
AAssign t2 to a
BMultiply c and d
CAdd b and t1
DEnd of instructions
💡 Hint
Check the 'Operation' and 'Operands' columns in row 2 of the execution_table.
At which step is the temporary variable t1 created and what does it store?
AStep 1, stores c * d
BStep 1, stores b + c
CStep 2, stores b + t1
DStep 3, stores final result
💡 Hint
Look at the 'Temporary Variable' and 'Operation' columns in step 1 of the execution_table.
If the expression was a = (b + c) * d, how would the first temporary variable change?
At1 = b * d
Bt1 = b + c
Ct1 = c * d
Dt1 = b + d
💡 Hint
Remember the order of operations and how the expression is broken down into steps.
Concept Snapshot
Three-address code breaks complex expressions into simple steps.
Each instruction has at most two operands and one result.
Temporary variables hold intermediate results.
This makes code easier to optimize and translate.
Example: a = b + c * d becomes t1 = c * d; t2 = b + t1; a = t2.
Full Transcript
Three-address code is a way to simplify complex expressions in programming languages during compilation. It breaks down expressions into simple instructions with at most two operands and one result. For example, the expression a = b + c * d is split into steps: first multiply c and d storing in t1, then add b and t1 storing in t2, and finally assign t2 to a. Temporary variables like t1 and t2 hold intermediate results. This step-by-step breakdown helps compilers optimize and generate machine code more easily.

Practice

(1/5)
1. What is the main purpose of three-address code in compiler design?
easy
A. To execute machine code directly on hardware
B. To break complex instructions into simpler steps with at most three parts
C. To store source code in a compressed format
D. To replace high-level language syntax with natural language

Solution

  1. Step 1: Understand the role of intermediate code

    Intermediate code like three-address code simplifies complex instructions for easier processing.
  2. Step 2: Identify the key feature of three-address code

    It breaks instructions into steps with at most three parts (two operands and one result).
  3. Final Answer:

    To break complex instructions into simpler steps with at most three parts -> Option B
  4. Quick Check:

    Three-address code = simple 3-part instructions [OK]
Hint: Three-address code simplifies instructions into three parts [OK]
Common Mistakes:
  • Confusing three-address code with machine code
  • Thinking it compresses source code
  • Believing it replaces programming languages
2. Which of the following is a correct example of a three-address code instruction?
easy
A. if a > b goto L1 else goto L2
B. while (a < b) { a = a + 1; }
C. print(a, b, c)
D. t1 = a + b

Solution

  1. Step 1: Identify the format of three-address code

    It typically has one operator and up to two operands, assigning result to a temporary variable.
  2. Step 2: Check each option

    t1 = a + b matches the format: one operator (+), two operands (a, b), and a result (t1). Others are control flow or high-level statements.
  3. Final Answer:

    t1 = a + b -> Option D
  4. Quick Check:

    Three-address code = one operator, two operands, one result [OK]
Hint: Look for one operator and two operands with a result variable [OK]
Common Mistakes:
  • Choosing control flow statements as three-address code
  • Confusing function calls with three-address code
  • Ignoring the assignment to a temporary variable
3. Given the three-address code:
t1 = a + b
t2 = t1 * c
d = t2 - e

What is the final value assigned to d in terms of a, b, c, e?
medium
A. (a + b) * c - e
B. a + b * c - e
C. a + (b * c) - e
D. (a + b) - (c * e)

Solution

  1. Step 1: Evaluate intermediate expressions step-by-step

    t1 = a + b; then t2 = t1 * c = (a + b) * c; finally d = t2 - e = (a + b) * c - e.
  2. Step 2: Substitute and simplify

    The final expression for d is (a + b) * c - e, matching (a + b) * c - e.
  3. Final Answer:

    (a + b) * c - e -> Option A
  4. Quick Check:

    Follow code stepwise = (a + b) * c - e [OK]
Hint: Follow temporary variables stepwise to build final expression [OK]
Common Mistakes:
  • Ignoring parentheses changing operation order
  • Multiplying b by c before adding a
  • Mixing subtraction order
4. Identify the error in this three-address code snippet:
t1 = a + b
t2 = t1 *
d = t2 - e
medium
A. Missing operand after '*' operator in second line
B. Incorrect use of temporary variables
C. Assignment to 'd' should be before 't2'
D. No error, code is correct

Solution

  1. Step 1: Examine the second line syntax

    The second line ends with '*' operator but lacks the second operand after '*', which is invalid.
  2. Step 2: Confirm other lines are correct

    First and third lines have proper assignments and operands; only second line has syntax error.
  3. Final Answer:

    Missing operand after '*' operator in second line -> Option A
  4. Quick Check:

    Operator must have two operands [OK]
Hint: Check each operator has two operands in three-address code [OK]
Common Mistakes:
  • Ignoring incomplete operator usage
  • Thinking variable order causes error
  • Assuming code is correct without checking syntax
5. How would you represent the high-level statement if (a < b) c = a + b; else c = a - b; in three-address code?
hard
A. if a < b then c = a + b else c = a - b
B. c = a + b if a < b else c = a - b
C. if a < b goto L1
c = a - b
goto L2
L1: c = a + b
L2:
D. t1 = a < b
c = t1 ? a + b : a - b

Solution

  1. Step 1: Translate conditional jump

    Use 'if a < b goto L1' to jump to true branch, else execute false branch.
  2. Step 2: Write false branch, jump over true branch

    False branch: c = a - b; then jump to L2 to skip true branch.
  3. Step 3: Write true branch and label

    Label L1: c = a + b; Label L2 marks end of conditional.
  4. Final Answer:

    if a < b goto L1
    c = a - b
    goto L2
    L1: c = a + b
    L2:
    -> Option C
  5. Quick Check:

    Use labels and jumps for if-else in three-address code [OK]
Hint: Use labels and jumps to represent if-else in three-address code [OK]
Common Mistakes:
  • Writing high-level if-else syntax directly
  • Using ternary operator not valid in three-address code
  • Missing jump to skip true branch after false branch
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard