Three-address code in Compiler Design - Time & Space Complexity
Analyzing time complexity helps us understand how the steps to create three-address code grow as the input program gets bigger.
We want to know how the work needed changes when the program has more statements or expressions.
Analyze the time complexity of the following code snippet.
for each statement S in program:
for each expression E in S:
generate three-address code for E
This code generates three-address code by visiting every statement and its expressions in the program.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Generating three-address code for each expression.
- How many times: Once for every expression inside every statement in the program.
As the number of statements and expressions grows, the total steps to generate code grow roughly in proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 statements | About 10 times the expressions per statement |
| 100 statements | About 100 times the expressions per statement |
| 1000 statements | About 1000 times the expressions per statement |
Pattern observation: The work grows linearly as the program size increases.
Time Complexity: O(n * m)
This means the time to generate three-address code grows in a straight line with the number of statements and expressions.
[X] Wrong: "Generating three-address code takes the same time no matter how big the program is."
[OK] Correct: More statements and expressions mean more code to generate, so the time grows with program size.
Understanding how code generation time grows helps you explain compiler efficiency clearly and confidently in interviews.
"What if expressions inside statements were nested deeply? How would the time complexity change?"
Practice
three-address code in compiler design?Solution
Step 1: Understand the role of intermediate code
Intermediate code like three-address code simplifies complex instructions for easier processing.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).Final Answer:
To break complex instructions into simpler steps with at most three parts -> Option BQuick Check:
Three-address code = simple 3-part instructions [OK]
- Confusing three-address code with machine code
- Thinking it compresses source code
- Believing it replaces programming languages
Solution
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.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.Final Answer:
t1 = a + b -> Option DQuick Check:
Three-address code = one operator, two operands, one result [OK]
- Choosing control flow statements as three-address code
- Confusing function calls with three-address code
- Ignoring the assignment to a temporary variable
t1 = a + b
t2 = t1 * c
d = t2 - eWhat is the final value assigned to
d in terms of a, b, c, e?Solution
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.Step 2: Substitute and simplify
The final expression for d is (a + b) * c - e, matching (a + b) * c - e.Final Answer:
(a + b) * c - e -> Option AQuick Check:
Follow code stepwise = (a + b) * c - e [OK]
- Ignoring parentheses changing operation order
- Multiplying b by c before adding a
- Mixing subtraction order
t1 = a + b
t2 = t1 *
d = t2 - eSolution
Step 1: Examine the second line syntax
The second line ends with '*' operator but lacks the second operand after '*', which is invalid.Step 2: Confirm other lines are correct
First and third lines have proper assignments and operands; only second line has syntax error.Final Answer:
Missing operand after '*' operator in second line -> Option AQuick Check:
Operator must have two operands [OK]
- Ignoring incomplete operator usage
- Thinking variable order causes error
- Assuming code is correct without checking syntax
if (a < b) c = a + b; else c = a - b; in three-address code?Solution
Step 1: Translate conditional jump
Use 'if a < b goto L1' to jump to true branch, else execute false branch.Step 2: Write false branch, jump over true branch
False branch: c = a - b; then jump to L2 to skip true branch.Step 3: Write true branch and label
Label L1: c = a + b; Label L2 marks end of conditional.Final Answer:
if a < b goto L1
c = a - b
goto L2
L1: c = a + b
L2: -> Option CQuick Check:
Use labels and jumps for if-else in three-address code [OK]
- 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
Ready to prove it?
Pass the test and this pattern moves unknown
