A tough Compiler question on operator precedence,what should be evaluated first?


3.Consider the grammar rule E → E1 - E2 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 E1 and E2 do not have any common sub expression, in order to get the shortest possible code
a) E1 should be evaluated first.
b) E2 should be evaluated first.
c)Evaluation of E1 and E2 should necessarily be interleaved.
d) Order of evaluation of E1 and E2 is of no consequence.


Although I dont understand E1 and E2 do not have any common sub expression in the question,but I have some understanding that between options a and b b is more appropriate.

As in option a E1 is in register then it has to be moved back to memory so that we can evalaute E2 in register then it has to be transfered to memory again.so that E1 can come in register then we can perform subtract.

If E2 evaluates first,then E2 moved back to memory,E1 comes in register and then there is no need to move E1 again to memory,we can simply peform E1 - E2,so less mov instructions,less code is required in comparision to above.Also E1 and E2 are expressions so E1 can be (2+3)*7 ,E2 can be (7/2 + 5).

But why option c and d are wrong I have no clue,also how will the answer change if E1 and E2 have a common sub expression,can any body give some example?