Consider the following grammar and the semantic actions to support the…

2019

Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let 𝑋1, 𝑋2, 𝑋3, 𝑋4, 𝑋5, and 𝑋6 be the placeholders for the non-terminals D, T, L or L1 in the following table:

Which one of the following are the appropriate choices for 𝑋1, 𝑋2, 𝑋3 and 𝑋4?

  1. A.

    𝑋1 = 𝐿 , 𝑋2 = 𝑇, 𝑋3 = 𝐿1, 𝑋4 = 𝐿

  2. B.

    𝑋1 = 𝑇 , 𝑋2 = 𝐿, 𝑋3 = 𝐿1, 𝑋4 = 𝑇

  3. C.

    𝑋1 = 𝐿 , 𝑋2 = 𝐿, 𝑋3 = 𝐿1, 𝑋4 = 𝑇

  4. D.

    𝑋1 = 𝑇 , 𝑋2 = 𝐿, 𝑋3 = 𝑇, 𝑋4 = 𝐿1

Attempted by 74 students.

Show answer & explanation

Correct answer: A

Answer: X1 = L, X2 = T, X3 = L1, X4 = L

Reasoning:

  • From the production D → T L with the semantic action X1.type = X2.type: this equation copies the type from the type symbol to the list, so the placeholder on the left of the equality refers to the list (L) and the placeholder on the right refers to the type (T). Hence X1 = L and X2 = T.

  • From the production L → L1 , id with the semantic action X3.type = X4.type: this copies the type from the sublist to the enclosing list, so the first placeholder here is the sublist (L1) and the second is the enclosing list (L). Therefore X3 = L1 and X4 = L.

  • The addType actions use additional placeholders (not asked) to supply the actual type to be attached to each identifier; those refer to the type nonterminal (T).

This mapping satisfies all semantic equations: the declaration makes the list inherit the type, the recursive list production copies type from the sublist, and addType uses the type computed from T.

A video solution is available for this question — log in and enroll to watch it.

Explore the full course: Gate Guidance By Sanchit Sir