retagged by
26,464 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

Best answer
69 69 votes

A node in a parse tree can $\text{INHERIT}$ an attribute either from its parent or its siblings. This means for a production
$$S \to AB,$$ $A$ can inherit values from either $S$ or $B$ and similarly $B$ can inherit values from either $S$ or $A.$

In the given productions, for

$L \to L_1, id,$

$L_1$ can inherit from $L$ or $,$ or $id$ with only $L$ being a non-terminal.

So, this means $X_3$ must be $L_1$ and $X_4$ must be $L$ as $X_i$ is a placeholder for non-terminals. 

Only option A matches this. 

selected by
28 28 votes

Inherited attributes :a attribute that gets its values from the attributes attached to the parent (or siblings) of its non-terminal.

Now in question first semantics $X1 \cdot \text{Type} = X2 \cdot \text{Type}$. $X1$ inherit value from $X2$  it means $X1$ can be $L$ or $T$ same for $X2$. Option C ruled out.

$X3 \cdot \text{type}=X4 \cdot \text{type} X3$ should be  $L1$  and $X4$ should be $L$ according to definition. Option B,D ruled out

So answer is A

https://en.m.wikipedia.org/wiki/Attribute_grammar

edited by
12 12 votes

See might be it works. PLZ view page 1 then page 2 for better understanding. Also take a input example and yrself verify the situation :

edited by
3 3 votes
The production
L → L1, id {X3.type = X4.type } ,

Above production has L and L1, hence X3 and X4 can not be T.
So option 1, 3 and 4 cannot be correct.
Hence, A is the correct answer.
2 2 votes

Since the production,

L → L1, id {X3.type = X4.type } , this production has L and L1, hence X3 and X4 cannot be T.

So option B, C and D cannot be correct.

Hence, A is correct answer.

Answer:
Position:
Show:

Related questions

30 30 votes
5 answers 5 answers
25.8k
25.8k views
Arjun asked Feb 7, 2019
25,770 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...
24 24 votes
5 answers 5 answers
15.0k
15.0k views
Arjun asked Feb 7, 2019
14,982 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.3k
28.3k views
Arjun asked Feb 7, 2019
28,274 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
2.9k
2.9k views
Na462 asked Jan 13, 2019
2,903 views
Consider the following Translation rules for the Grammar G :What will be the output for input string abebebe for bottom up parser ?CBBPBPBPCBBBPPBBCBPBPBPBCBPBBPPB