Machine-translated by Google. Questions are authored in English.
Top

Discussion

Consider the following syntax-directed definition (SDD).

\(S \to DHTU\){ \(S.val = D.val + H.val + T.val + U.val;\) }
\(D \to\) "M"\(D_{1}\){ \(D.val = 5 + D_{1}.val;\) }
\(D \to \epsilon\){ \(D.val = -5;\) }
\(H \to\) "L"\(H_{1}\){ \(H.val = 5 * 10 + H_{1}.val;\) }
\(H \to \epsilon\){ \(H.val = -10;\) }
\(T \to\) "C"\(T_{1}\){ \(T.val = 5 * 100 + T_{1}.val;\) }
\(T \to \epsilon\){ \(T.val = -5;\) }
\(U \to\) "K"{ \(U.val = 5;\) }

Given "MMLK" as the input, which one of the following options is the CORRECT value computed by the SDD (in the attribute \(S.val\))?

  • A.45
  • B.50
  • C.55
  • D.65

Answer: A

The value computed is 45.

Parse 'MMLK' as \(S \to DHTU\) with D = MM, H = L, T = \(\epsilon\) and U = K, then evaluate bottom-up:

\[ \begin{aligned} D &= 5 + (5 + (-5)) = 5 \\ H &= 5 \times 10 + (-10) = 40 \\ T &= -5,\quad U = 5 \\ S &= 5 + 40 - 5 + 5 = 45 \end{aligned} \]

The \(\epsilon\) productions contribute negative values that are easy to forget: dropping the −5 for the empty T gives 50, and dropping the −5 at the end of D's chain as well gives 55.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.