258 views
1 1 vote

Consider the following S-attributed SDT scheme used to calculate the value of an expression. The grammar generates simple arithmetic additions and is suitable for an LR parser.

\[
\begin{array}{ll}
\text{Production} & \text{Semantic Rules} \\
E \rightarrow E_1 + T & E.\text{val} = E_1.\text{val} + T.\text{val} \\
E \rightarrow T & E.\text{val} = T.\text{val} \\
T \rightarrow \mathbf{id} & T.\text{val} = \mathbf{id}.\text{lexval}
\end{array}
\]

If the input string is $3+5+2$, what is the sequence of values stored on the semantic stack for the attribute val immediately after each reduction of the type $E \rightarrow E_1+T$ occurs?

  1. $8,10$
     
  2. $3, 5, 2$
     
  3. $3, 8, 10$
     
  4. $5,2$

1 Answer

0 0 votes

Initial Reductions:

  • id $(3)$ is reduced to $T$, then $T$ is reduced to $E$. Stack has $E . v a l=3$.
     
  • id $(5)$ is reduced to $T$. Stack now has $E(3)$ and $T(5)$.
     

First $E \rightarrow E_1+T$ Reduction:

  • The parser reduces $E(3)+T(5)$ to a new $E$.
     
  • E.val $=3+5=\mathbf{8}$.
     

Next Steps:

  • id $(2)$ is reduced to $T$. Stack now has $E$ $(8)$ and $T$ $(2)$.
     

Second $E \rightarrow E_1+T$ Reduction:

  • The parser reduces $E(8)+T(2)$ to the final $E$.
     
  • E.val $=8+2=\mathbf{1 0}$.

The sequence of values produced by the specific reduction $E \rightarrow E_1+T$ is $\mathbf{8 , 1 0}$.

Answer:
Position:
Show:

Related questions

0 0 votes
2 2 answers
283
283 views
GO Classes asked Jan 20
283 views
Consider the following basic block consisting of three-address code instructions. Assume that only the variable $\verb|x|$ is "live" at the exit of this basic block.1. a ...
1 1 vote
1 1 answer
232
232 views
GO Classes asked Jan 20
232 views
Consider the following C-style code segment:while (a < b) { if (c < d) x = y + z; else x = y - z; }Assume this code is translated into Three-Address Code (TAC) us...
1 1 vote
1 1 answer
268
268 views
GO Classes asked Jan 20
268 views
In a compiler's optimization phase, a Directed Acyclic Graph (DAG) is constructed for the following basic block:1. t1 = a + b 2. t2 = c * d 3. t3 = t1 + t2 4. t4 = a + b ...
3 3 votes
2 2 answers
333
333 views
GO Classes asked Jan 20
333 views
Consider the following regular expression over the alphabet $\Sigma=\{a, b\}$ that represents a specific class of tokens in a new programming language:$$r=(a+b)^* a b b$$...