The Gateway to Computer Science Excellence

+14 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

- $2 + a - b$
- $2 + a - b * a + b$
- $2 + ((a - b) * (a + b)))$
- $2 + (a - b) * (a + b)$

The parse tree corresponds to

- Expression (i)
- Expression (ii)
- Expression (iv) only
- Expression (ii), (iii), and (iv)
- Expression (iii) and (iv) only

0

It took a bit time for me to understand why (E) is the correct option.

Though * has higher precedence than + and -, but brackets always have the highest precedence.

In the given parse tree, (a-b) and (a+b) are evaluated first, then multiplied and added with 2 at last, and the same happening in option iii and iv.

Though * has higher precedence than + and -, but brackets always have the highest precedence.

In the given parse tree, (a-b) and (a+b) are evaluated first, then multiplied and added with 2 at last, and the same happening in option iii and iv.

+1

@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.

+12 votes

Best answer

+5 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

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

- All categories
- General Aptitude 1.8k
- Engineering Mathematics 7.4k
- Digital Logic 2.9k
- Programming and DS 4.9k
- Algorithms 4.4k
- Theory of Computation 6.2k
- Compiler Design 2.1k
- Databases 4.1k
- CO and Architecture 3.4k
- Computer Networks 4.1k
- Non GATE 1.4k
- Others 1.7k
- Admissions 595
- Exam Queries 576
- Tier 1 Placement Questions 23
- Job Queries 72
- Projects 17

50,666 questions

56,157 answers

193,767 comments

93,743 users