Given the following expression grammar: E -> E * F | F + E | F F -> F - F | id…

2000

Given the following expression grammar:

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

which of the following is true?

  1. A.

    * has higher precedence than +

  2. B.

    – has higher precedence than *

  3. C.

    + and — have same precedence

  4. D.

    + has higher precedence than *

Attempted by 19 students.

Show answer & explanation

Correct answer: B

Given Grammar:

E → E * F | F + E | F
F → F - F | id
Rule for Finding Precedence

In expression grammars:

Operators defined in lower-level non-terminals have higher precedence.
Operators defined in higher-level non-terminals have lower precedence.

Here:

E → E * F | F + E | F

Operators at E level:

*
+

And:

F → F - F | id

Operator at F level:

-

Since F is evaluated before being used in E, the operator - binds more tightly than both * and +.

Example:

a * b - c

The grammar forces:

a * (b - c)

not

(a * b) - c

Therefore, - has higher precedence than *.

Explore the full course: Gate Guidance By Sanchit Sir