Open In App

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 https://www.geeksforgeeks.org/compilers-set-2/amp/
Quiz of this Question

Article Tags :