Consider the grammar rule E → E₁ − E₂ for arithmetic expressions. The code…

2014

Consider the grammar rule E → E₁ − E₂ for arithmetic expressions. The code generated is targeted to a CPU having a single user register. The subtraction operation requires the first operand to be in the register. If E₁ and E₂ do not have any common sub expression, in order to get the shortest possible code

  1. A.

    E₁ should be evaluated first

  2. B.

    E₂ should be evaluated first

  3. C.

    Evaluation of E₁ and E₂ should necessarily be interleaved

  4. D.

    Order of evaluation of E₁ and E₂ is of no consequence

Attempted by 32 students.

Show answer & explanation

Correct answer: B

With a single register, E2 must be evaluated first. Store E2 in memory, then evaluate E1 into the register. Subtract stored E2 from E1 to minimize load/store operations.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir