All Exams Test series for 1 year @ ₹349 only
Question

Consider the following Three Address code sample for solving the questions:
100: t1 = y+2
101: initial = x/t1
102: limit = 10
103: if i > j goto 105
104: goto 111
105: if num > b goto 107
106: goto 111
107: num = limit-1
108: b = y+2
109: i = i - num
110: goto 103
111: j = i - num
112: k= j+b

Which of the following is the correct High level code for the given TAC in the passage

The correct answer is

init = x/(y+2);
limit=10;
while(i>j && num>b)
{
num = limit-1;
b = y + 2;
i=i-num;
}
j = i - num;
k = k+b;

Understanding the Three Address Code (TAC)

The problem presents a snippet of Three Address Code (TAC) and asks us to find the equivalent high-level code. TAC is an intermediate representation used in compilers. Let's break down the given TAC step-by-step to understand its logic.

The TAC snippet is:

100: t1 = y+2
101: initial = x/t1
102: limit = 10
103: if i > j goto 105
104: goto 111
105: if num > b goto 107
106: goto 111
107: num = limit-1
108: b = y+2
109: i = i - num
110: goto 103
111: j = i - num
112: k= j+b

Analyzing the TAC Logic

We need to trace the execution flow and translate the assembly-like TAC instructions into a more readable high-level programming construct.

  • Lines 100-101: These lines compute the value of $initial$. First, $t1$ is assigned $y+2$. Then, $initial$ is assigned the result of $x / t1$. This translates to the high-level statement: $init = x / (y+2);$.
  • Line 102: Assigns the value 10 to $limit$. This translates to: $limit = 10;$.
  • Lines 103-110: Control Flow and Loop Structure
    • Line 103 checks if $i > j$.
    • If $i > j$ is true, execution proceeds to line 105.
    • If $i > j$ is false (meaning $i ≤ j$), execution goes to line 104.
    • Line 104 contains $goto 111$. So, if $i ≤ j$, the code jumps directly to line 111, skipping lines 105-110 entirely.
    • Line 105 is reached only if $i > j$ was true. It checks if $num > b$.
    • If $num > b$ is true, execution proceeds to line 107.
    • If $num > b$ is false (meaning $num ≤ b$), execution goes to line 106.
    • Line 106 contains $goto 111$. So, if $num ≤ b$, the code jumps directly to line 111, skipping lines 107-109.
    • Lines 107, 108, and 109 ($num = limit-1; b = y+2; i = i - num;$) are executed only if *both* $i > j$ and $num > b$ are true.
    • Line 110 contains $goto 103$. This creates a loop. After executing lines 107-109, the control flow returns to line 103 to re-evaluate the conditions.

Based on this analysis, the block of code (lines 107-109) is executed repeatedly as long as the combined condition $(i > j) AND (num > b)$ holds true. This is the definition of a $while$ loop.

The loop condition is therefore $while(i > j && num > b)$.

The body of the loop consists of the statements:

num = limit-1;
b = y + 2;
i = i - num;

Lines 111-112: Post-Loop Execution

After the loop terminates (because either $i ≤ j$ or $num ≤ b$), execution continues at line 111.

  • Line 111: $j = i - num;$
  • Line 112: $k = j + b;$

These are the statements that execute after the loop completes.

Evaluating the High-Level Code Options

Let's compare our derived logic with the given options:

  1. Option 1: $if(i>j || num>b){...}$
    This uses an OR condition ($||$) which is incorrect. The TAC requires an AND condition for the loop.
  2. Option 2: $while(i>j || num>b){...}$
    This uses an OR condition ($||$) in the $while$ loop, which is incorrect.
  3. Option 3: $if(i>j){ While(num> b){...} } else {...}$
    This uses an incorrect nested structure with an $if-else$. The TAC implies a combined AND condition for the loop, not separate checks with an else block performing assignments.
  4. Option 4: $init = x/(y+2); limit=10; while(i>j && num>b){ num = limit-1; b = y + 2; i=i-num; } j = i - num; k = k+b;$
    This option correctly translates the initial assignments, the loop condition (using $&&$ for AND), the loop body, and the statements following the loop.
  5. Option 5: (This option text is identical to Option 4) $init = x/(y+2); limit=10; while(i>j && num>b){ num = limit-1; b = y + 2; i=i-num; } j = i - num; k = k+b;$
    This option correctly translates the initial assignments, the loop condition (using $&&$ for AND), the loop body, and the statements following the loop.

Conclusion

The TAC logic clearly indicates a loop that continues as long as both $i > j$ and $num > b$ are true. Lines 104 and 106 provide exit paths to line 111 if either condition fails, and line 110 creates the loop by jumping back to line 103. This structure directly corresponds to a $while(i > j && num > b)$ loop in high-level code. Options 4 and 5 accurately represent this logic and are equivalent.

Was this answer helpful?

Important Questions from Code Optimization

  1. What are the various LEADERS (leading statements) for the TAC given in the passage?
  2. Which of the followings is the correct Flow graph for the TAC given in the passage?
  3. Which of the following is the Optimized version for the given TAC in the passage
  4. Consider the control flow graph shown in the figure.
     


    Which one of the following options correctly lists the set of redundant expressions (common subexpressions) in the basic blocks B4 and B5?
    Note: All the variables are integers.

  5. Consider the control flow graph given below.



    Which one of the following options is the set of live variables at the exit point of each basic block?

Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App