Consider the following grammar: E → E # X | X X → T − X | X * T | T T → T + F…

Consider the following grammar:

E → E # X | X
X → T − X | X * T | T
T → T + F | F
F → (E) | id

Here, id stands for an identifier. The grammar is used to generate valid arithmetic expressions in a hypothetical language. Consider the following statements:

(i) # associates from left and has the least precedence.
(ii) − associates from left and has higher precedence than #.
(iii) + associates from left and has the highest precedence.
(iv) * associates from left and has the same precedence as −.

Which option is correct?

Answer: A. Only (i), (iii), and (iv) are trueCorrect answer: Only (i), (iii), and (iv) are true. Precedence is determined by the grammar level. The operator # appears at the E level, so it has the least…

  1. A.

    Only (i), (iii), and (iv) are true

  2. B.

    Only (ii) and (iii) are true

  3. C.

    Only (ii), (iii), and (iv) are true

  4. D.

    All of the above

Attempted by 14 students.

Show answer & explanation

Correct answer: A

Correct answer: Only (i), (iii), and (iv) are true.

Precedence is determined by the grammar level. The operator # appears at the E level, so it has the least precedence. The operators − and * appear at the X level, so they have the same precedence and higher precedence than #. The operator + appears at the T level, so it has the highest precedence.

Associativity is determined by the direction of recursion.
E → E # X is left-recursive, so # is left-associative.
X → T − X is right-recursive, so − is right-associative, not left-associative.
X → X * T is left-recursive, so * is left-associative.
T → T + F is left-recursive, so + is left-associative.

Thus statements (i), (iii), and (iv) are true, while statement (ii) is false because − associates from right.

Explore the full course: Gate Guidance By Sanchit Sir

Loading lesson…