in Compiler Design retagged by
2,656 views
19 votes
19 votes

Consider the parse tree

Assume that $*$ has higher precedence than $+$, $-$ and operators associate right to left (i.e $(a + b + c= (a + (b + c)))$. Consider

  1. $2 + a - b$
  2. $2 + a - b * a + b$
  3. $(2 + ((a - b) * (a + b)))$
  4. $2 + (a - b) * (a + b)$

The parse tree corresponds to

  1. Expression (i)
  2. Expression (ii)
  3. Expression (iv) only
  4. Expression (ii), (iii), and (iv)
  5. Expression (iii) and (iv) only
in Compiler Design retagged by
2.7k views

4 Comments

@srestha Ma'am

I don't know whether they expect "most appropriate" answer or not but I think there is no need of choice 3 if choice 4 is sufficient. as "*" has highest precedence then anyhow it wont perform "+" over "*" so no need of parentheses there. Please explain.

2
2

"*" has highest precedence then anyhow it wont perform "+" over "*"

that is true, that is why option iv is true

but iii also correct

if both are correct, we need to say both correct. We cannot eliminate one without any reason

3
3
There is an extra closing bracket in option iii ?
1
1

2 Answers

15 votes
15 votes
Best answer

e is correct

Because as the precedence is right to left, expression evaluated in (ii) is $2+(a-((b*a)+b)),$ which is not a correct evaluation as the given parse tree.

edited by

4 Comments

I agree with Rajesh. b*a will be evaluated first.
0
0
As given in the question that '*' has higher priority but in the parse tree shown '*' is defined at a level above  '-'. Though the parse tree corresponds to iii) and iv), isn't the priority given wrong here?
0
0
$\mathbf *$ should come first that isn't necessary if parenthesis are being used.
0
0
6 votes
6 votes
Although given answer is correct one can approach in another way if one wants more visualization to this problem

Write the "postfix" expression of given parse tree keeping in mind precedences order & associativity as per question we will get      { 2ab-ab+*+ }

Now write the postfix expressions of options 3 & 4 you will see same and for option 2 you will get different

 

(E) correct answer

1 comment

Right approach but when we take infix of tree that match to (ii) option
0
0
Answer:

Related questions

0 votes
0 votes
1 answer
2