edited by
12,538 views
39 votes
39 votes

Consider the translation scheme shown below.

  • $S \rightarrow T\;R$
  • $R \rightarrow + T \{\text{print}(‘+’);\} R\mid \varepsilon$
  • $T  \rightarrow$ num $\{\text{print}$(num.val)$;\}$

Here num is a token that represents an integer and num.val represents the corresponding integer value. For an input string ‘$9 + 5 + 2$’, this translation scheme will print

  1. $9 + 5 + 2$
  2. $9 \ 5 + 2 +$
  3. $9 \ 5 \ 2 + +$
  4. $+ + 9 \ 5 \ 2$
edited by

4 Answers

Best answer
56 votes
56 votes

Correct Option: B
$9\ 5+2+$

edited by
25 votes
25 votes
InputTranslationOutput
9 + 5 + 2S → T R 
9 + 5 + 2T  → num {print(num.val);}9
+ 5 + 2R → + T {print(‘+’);} R (the + is simply consumed as there is no print corresponding to it)
5 + 2T  → num {print(num.val);}5+
+ 2 R → + T {print(‘+’);} R 
2T  → num {print(num.val);}2+

So, output 95+2+. Option B.

3 votes
3 votes

Reduce the Expression Rightmost in Reverse:- (Seeing the first matching reducible symbol)
9 + 5 + 2ε  (Print 9 and reduced to T)
> T + 5 + 2ε  (Print 5 and Reduced to T)
> T + T  + 2ε (+T print + and can't be reduced)
> T + T + Tε   (Print 2 and reduced to T)
> T + T +Tε (+T print + and can't be reduced)
> T + T +TR (ε Reduced to R)
> T +TR (+TR reduced to R)

>TR (+TR reduced to R)

>S (TR reduced to S)

9 5 + 2 +

Answer:

Related questions

40 votes
40 votes
3 answers
1
Kathleen asked Sep 17, 2014
19,883 views
Consider the grammar shown below. $S \rightarrow C \ C$$C \rightarrow c \ C \mid d$This grammar isLL(1)SLR(1) but not LL(1)LALR(1) but not SLR(1)LR(I) but not LALR(1)
32 votes
32 votes
4 answers
4
Kathleen asked Sep 17, 2014
8,458 views
Consider the syntax directed definition shown below.$$\begin{array}{ll}S \rightarrow \mathbf{ id :=} E&\qquad \{gen(\mathbf{ id}.place = E.place;);\}\\E \rightarrow E_1 +...