GATE | GATE-CS-2000 | Question 49

Given the following expression grammar:

E -> E * F | F + E | F
F -> F - F | id 

which of the following is true?
(A) * has higher precedence than +
(B) – has higher precedence than *
(C) + and — have same precedence
(D) + has higher precedence than *


Answer: (B)

Explanation: Let say i/p is 3*4-5 when we draw parse tree according to grammar

      E
   /  |  \
  E   *   F
  |     / | \
  F    F  -  F
  |    |     |
id(3) id(4) id(5)

As we can see first ‘- ‘ will be evaluated then ‘ * ‘ is evaluated so ‘ – ‘ has higher precedence then *.

So correct choice is B

See question 1 of http://www.geeksforgeeks.org/compilers-set-2/

Quiz of this Question

My Personal Notes arrow_drop_up
Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.