277 views
2 2 votes

Consider the following L-attributed Syntax Directed Definition (SDD). $S$ is the start symbol, and S.val is a synthesized attribute. A.inh is an inherited attribute.

  1. $S \rightarrow A B \quad\{A . i n h=5 ; S . v a l=A . v a l+B . v a l\}$
     
  2. $A \rightarrow id \quad\{ A.val = A.inh × id.lexval \}$
     
  3. $B \rightarrow id \quad\{B. val = id.lexval +2\}$
     

For the input string id id where the lexical values of the two id tokens are $\mathbf{3}$ and $\mathbf{4}$ respectively, what is the value of $S.val$?

  1. $15$
     
  2. $21$
     
  3. $23$
     
  4. $25$

2 Answers

1 1 vote

This problem involves an L-attributed definition, where inherited attributes $($like $A.inh)$ are passed down or across the tree, and synthesized attributes (like $S.val$ and $A.val)$ are passed up.

Initialize $A.inh:$ According to the first production, $A.inh$ is set to $5$.

Evaluate $A.val:$ The second production states $A.val =A . i n h \times \mathrm{id}_1. lexval$ .

  • Given $\mathrm{id}_1.lexval =3$ :
     
  • $A.val =5 \times 3=15$.
     

Evaluate $B.val:$ The third production states $B.val =\mathrm{id}_2.lexval +2$.

  • Given $\mathrm{id}_2.lexval =4$ :
     
  • $B.val =4+2=6$.
     

Evaluate $S.val:$ Finally, $S.val = A.val + B.val$.

  • $S.val =15+6=21$.
Answer:
Position:
Show:

Related questions

2 2 votes
3 3 answers
318
318 views
GO Classes asked Jan 31
318 views
Consider the following syntax-directed translation scheme (SDTS) where $S$ is the start symbol:$$\begin{aligned}& S \rightarrow E\{\text { print }(\text { E.val })\} \\& ...
1 1 vote
2 2 answers
360
360 views
GO Classes asked Jan 31
360 views
A lexical analyzer for a specific language identifies tokens using the following regular expressions:$$\begin{aligned}& L_1: a(a \mid b)^* \\& L_2: a b(a \mid b)^* \\& L_...
1 1 vote
1 1 answer
286
286 views
GO Classes asked Jan 31
286 views
Consider the following basic block of intermediate code:1. a = 10 2. b = a + 5 3. c = b * 2 4. a = c - b 5. d = a + bAssuming that only the variable $\verb|d|$ is "live" ...
1 1 vote
1 1 answer
258
258 views
GO Classes asked Jan 31
258 views
In the context of Code Optimization, consider the following code segment inside a loop:for (i = 0; i < n; i++) { x = y + z; a[i] = 2 * i + x; }Moving the statemen...