edited by
11,816 views
18 votes
18 votes

Let the attribute ‘$val$’ give the value of a binary number generated by $S$ in the following grammar:

  • $S \rightarrow L.L \mid L$
  • $L \rightarrow LB \mid B$
  • $B \rightarrow 0 \mid 1$

For example, an input $101.101$ gives $S.val = 5.625$

Construct a syntax directed translation scheme using only synthesized attributes, to determine $S.val$.

edited by

1 Answer

Best answer
36 votes
36 votes
$S\to L.L  \quad\{ S.val = L_1.val + L_2.val/2^{L_2.nb} \}$

$\quad \quad \mid \; L \quad  \{ S.val = L.val \}$

$L\to LB \quad  \{ L.val = 2 * L_1.val + B.val,$
$\qquad \qquad \quad L.nb = L_1.nb + B.nb \}$

$\quad \quad \mid \; B\quad   \{ L.val = B.val$
$\qquad\qquad \quad L.nb = B.nb \}$

$B \to 0  \quad  \{ B.val = 0$
$\qquad\qquad B.nb = 1 \}$

$\quad \quad \mid \; 1   \quad \{ B.val = 1$
$\quad \quad \quad \qquad  B.nb = 1 \}$

Here, $val =$ decimal value, $nb =$ number of bits.
edited by

Related questions

28 votes
28 votes
1 answer
1
Kathleen asked Sep 25, 2014
8,766 views
Faster access to non-local variables is achieved using an array of pointers to activation records called a stackheapdisplayactivation tree
18 votes
18 votes
2 answers
2
Kathleen asked Sep 25, 2014
5,880 views
A linker reads four modules whose lengths are $200, 800, 600$ and $500$ words, respectively. If they are loaded in that order, what are the relocation constants?$0, 200, ...
24 votes
24 votes
2 answers
3
Kathleen asked Sep 25, 2014
12,752 views
Which of the following statements is true?SLR parser is more powerful than LALRLALR parser is more powerful than Canonical LR parserCanonical LR parser is more powerful t...
26 votes
26 votes
3 answers
4
Kathleen asked Sep 25, 2014
9,377 views
In a resident – OS computer, which of the following systems must reside in the main memory under all situations?AssemblerLinkerLoaderCompiler