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
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)