The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+16 votes
1k views

The expression $( a * b) * c \; op \dots$

where ‘op’ is one of ‘$+$’, ‘$*$’ and ‘$\uparrow$’ (exponentiation) can be evaluated on a CPU with single register without  storing the value of ($a * b$) if

  1. $\text{‘op’}$ is ‘$+$’ or ‘$*$’

  2. $\text{‘op’}$ is ‘$\uparrow$’ or ‘$*$’

  3. $\text{‘op’}$ is ‘$\uparrow$’ or ‘$+$’

  4. not possible to evaluate without storing

asked in Compiler Design by Veteran (59.8k points)
edited by | 1k views
0
I fail to understand the answer provided below.

Using parenthesis around $(a * b)$ compels us to evaluate this expression first, which will require one register to store the result of the evaluation, irrespective of the operation $op$.

Shouldn't the answer be option $D$? What am I missing?

2 Answers

+19 votes
Best answer

A)

$\uparrow$ has higer precedence than $\{*,+,-,/ \}$

So, if op $ = \uparrow$ implies, we need to evaluate the right hand side of $\uparrow$ first and then do the lhs part, which would definitely require us to store the value of lhs

but if its a '$+$' or '$*$' , we don't need to store the values evaluated, and on the go can do the operation directly on one register.

answered by Junior (925 points)
edited by
0
why will we evaluate RHS of ↑ first, coz in C language, either side of a binary operator can be evaluated first, it is undefined!
+9 votes
Let say, the expression is one of the below:

(a*b)*c+d

(a*b)*c*d

(a*b)*c^d

In any case, brackets has the highest priority always. So I have to compute brackets first. Now, for + and *, I can do the rest of the operation and save results in the same register. But for exponentiation, I have to store the result of a*b, then do the computation of c^d, then multiply these two results.

So option A is correct.
answered by (249 points)
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true
50,129 questions
53,252 answers
184,785 comments
70,506 users