Q. 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:
| Production rule | Semantic action |
| D → T L | 𝑋1.type = 𝑋2.type |
| T → int | T.type = int |
| T → float | T.type = float |
| L → L1 , id | 𝑋3.type = 𝑋4.type addType(id.entry, 𝑋5.type) |
| L → id | addType(id.entry, 𝑋6.type) |
Which one of the following are the appropriate choices for 𝑋1, 𝑋2, 𝑋3 and 𝑋4?
| (A) 𝑋1 = 𝐿 , 𝑋2 = 𝑇, 𝑋3 = 𝐿1, 𝑋4 = 𝐿 | (B) 𝑋1 = 𝑇 , 𝑋2 = 𝐿, 𝑋3 = 𝐿1, 𝑋4 = 𝑇 |
| (C) 𝑋1 = 𝐿 , 𝑋2 = 𝐿, 𝑋3 = 𝐿1, 𝑋4 = 𝑇 | (D) 𝑋1 = 𝑇 , 𝑋2 = 𝐿, 𝑋3 = 𝑇, 𝑋4 = 𝐿1 |
Solution:
The correct answer is X1 = L, X2 = T, X3 = L1, X4 = L
According to Inherited attribute definition, an attribute is inherited if the attribute value of a parse-tree node is determined from attribute values of its parent and siblings.
So, semantic rules should be as following below:
D → TL {L.idtype = T.stype}
T → int {T.stype = int}
T → float {T.stype = float}
L → L1, id {L1.itype = L.itype}
addtype(id.entry, L.itype)
L → id addtype(id.entry, L.itype)




