Question: i)Write three-address code for the following expressions: a)x = a + b * c- d / (b*c). b)x = (a*b)+(c+d) - (a+b+c+d) ii)Consider the following

i)Write three-address code for the following expressions:

a)x =a + b * c- d / (b*c).

b)x = (a*b)+(c+d) - (a+b+c+d)

ii)Consider the following expressions and construct DAGs for it:

a)(((a-a)+(a-a)+((a*a)+(a*a)))

b)(a*b)*(b*e)+(a*b)*(b*e)

sol33:

i) Three-address code for the following expressions:

a) x = a + b * c - d / (b*c)

t1 = b * c

t2 = d / t1

t3 = a + t2

t4 = t3 - t1

x = t4

b)x = (a*b) + (c+d) - (a+b+c+d)

t1 = a * b

t2 = -t1

t3 = c + d

t4 = a + b

t5 = t4 + c

t6 = t5 + d

t7 = t2 + t3

x = t7 - t6

ii) DAGs for the following expressions:

a) (((a-a)+(a-a)+((aa)+(aa))))

+ +

/ \\ / \\

- - * *

/ \\ / \\ / \\ / \\

a a a a a a

b)(ab)(be)+(ab)(be)

* *

/ \\ / \\

* * * *

/ \\ / \\ / \\ / \\

a b b a b b

| |

* *

/ \\ / \\

b e b e

Explanation:

In the first expression, we can see that some sub-expressions like (a-a) and (a*a) are being repeated multiple times. Using a DAG, we can represent these sub-expressions only once and reuse them wherever required, which reduces the number of computations and improves efficiency.

In the second expression, we can see that the expressions (a*b) and (b*e) are being multiplied twice. Using a DAG, we can represent these sub-expressions only once and reuse them wherever required, which reduces the number of computations and improves efficiency.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!