• retagged by
27,077 views
41 41 votes

Consider the following grammar and the semantic actions to support the inherited type declaration attributes. Let $X_1, X_2, X_3, X_4, X_5$, and $X_6$ be the placeholders for the non-terminals $D, T, L$ or $L_1$ in the following table:

$$\begin{array}{|l|l|} \hline  \text{Production rule} & \text{Semantic action} \\ \hline D \rightarrow T L & X_1.\text{type} = X_2.\text{type} \\ \hline T \rightarrow \text{int} & T.\text{type} = \text{int} \\ \hline T \rightarrow \text{float} & T.\text{type} = \text{float} \\ \hline
L \rightarrow L_1, id & X_3.\text{type}= X_4.\text{type}  \\ 
&\text{addType}(id. \text{entry}, X_5.\text{type})\\ \hline
L \rightarrow id & \text{addType}(id. \text{entry}, X_6.\text{type}) \\ \hline \end{array}$$

Which one of the following are appropriate choices for $X_1, X_2, X_3$ and $X_4$?

  1. $X_1=L, \: X_2=T, \: X_3=L_1, \: X_4 = L$
  2. $X_1=T, \: X_2=L, \: X_3=L_1, \: X_4 = T$
  3. $X_1=L, \: X_2=L, \: X_3=L_1, \: X_4 = T$
  4. $X_1=T, \: X_2=L, \: X_3=T, \: X_4 = L_1$

11 Answers

0 0 votes

Let's analyze the production rules and semantic actions to determine the appropriate choices for X1, X2, X3, and X4.

Production Rule: D -> TL

Semantic Action: X1.type = X2.type

This action indicates that the type of the declaration D is inherited from the type of T. So, X1 should be D and X2 should be T.

Production Rule: T -> int or T -> float

Semantic Action: T.type = int or T.type = float

These rules directly assign a type to the terminal T.

Production Rule: L -> L1 id or L -> id

Semantic Action: X3.type = X4.type and addType(id.entry, X5.type)

In the first production, L is composed of L1 and id. The type of L is inherited from the type of L1. So, X3 should be L and X4 should be L1.

In the second production, L directly consists of id. The type of id is inherited from the type of L. So, X6 should be L.

Based on the above analysis, the correct choices for X1, X2, X3, and X4 are:

X1 = D, X2 = T, X3 = L, X4 = L1

Therefore, the correct answer is A.

0 0 votes
look 1 translation D->TL where X1.type = X2.type

as above metions it support inherited type declaration ,

so in translation x1 take value either from left sibling or parent

now x2 has only 2 option x2 = T or D

we have option x2 = T  so choose it
0 0 votes


According to how the attributes need to be inherited option A seems correct 



0 0 votes

First lets understand what the given grammar is ,
D → T L
T → int
T → float
L → L1 , id
L → id

This grammar generates an example like
int a
int a, b, c
float x, y

Here T gives the type and L is for identifiers.
From the information it is inherited attribute and from the table we can see T decides type of L .

Therefore  X1 = L & X2 = T 
Similarly we can say L1 inherits L type.

X3 = L1 & X4 = L.
Answer is A.

Answer:
Position:
Show:

Related questions

31 31 votes
5 answers 5 answers
26.3k
26.3k views
Arjun asked Feb 7, 2019
26,281 views
Consider the augmented grammar given below:$S’ \rightarrow S$$S \rightarrow \langle L \rangle \mid id$$L \rightarrow L, S \mid S$Let $I_0 = \text{CLOSURE} (\{[S’ \rightar...
25 25 votes
5 answers 5 answers
15.3k
15.3k views
Arjun asked Feb 7, 2019
15,253 views
Which one of the following kinds of derivation is used by LR parsers?LeftmostLeftmost in reverseRightmostRightmost in reverse
33 33 votes
3 answers 3 answers
28.8k
28.8k views
Arjun asked Feb 7, 2019
28,774 views
Consider the grammar given below:$S \rightarrow Aa$$A \rightarrow BD$$B \rightarrow b \mid \epsilon $$D \rightarrow d \mid \epsilon $Let $a,b,d$ and $\$ $ be indexed as f...
1 1 vote
1 1 answer
3.1k
3.1k views
Na462 asked Jan 13, 2019
3,094 views
Consider the following Translation rules for the Grammar G :What will be the output for input string abebebe for bottom up parser ?CBBPBPBPCBBBPPBBCBPBPBPBCBPBBPPB