in Compiler Design edited by
10,524 views
26 votes
26 votes

Consider the following Syntax Directed Translation Scheme $( SDTS )$, with non-terminals $\{S,A \}$ and terminals $\{a,b \}$.

          $S \to aA  \quad \{\text{print }1\}$

          $S \to a    \quad   \{\text{print }2\}$

          $A \to Sb  \quad \{\text{print }3\}$

Using the above $SDTS$ , the output printed by a bottom-up parser, for the input $aab$ is:

  1.  $1 \ 3 \ 2 $ 
  2.  $2 \ 2 \ 3  $ 
  3.  $2 \ 3 \ 1 $ 
  4.  syntax error
in Compiler Design edited by
10.5k views

2 Answers

59 votes
59 votes
Best answer

$\bf{aab}$ could be derived as follows by the bottom up parser:

$S \rightarrow a$$\color{blue}{\mathbf A}$ prints $\color{blue}{1}$
$\quad \rightarrow a$$\color{blue}{\mathbf S}$$b$ prints $\color{blue}{3}$
$\quad \rightarrow aab$  prints $\color{blue}{2}$
Now since the bottom-up parser will work in reverse of rightmost derivation, so it will print in bottom-up fashion i.e., $231$ which is option C.

Note that this can be easily visualized by drawing the derivation tree.

edited by

4 Comments

@PriyankaS

Okay lets's try and see what happens: Look from bottom to top

Now what, we don't have any Handles to reduce

$SA$

$S\underline{Sb}$

$S\underline{a}b$

$\underline{a}ab$

 

That's why we are not reducing it initially.

0
0
@Kushagra what is SA here ?
0
0

@PriyankaS

Initially, we are reducing S to aA, not to S→a.

In your answer, while traversing the tree you are reducing the left subtree using S→a rule (that's why you are printing an extra 2 initially), but the left subtree of the root is only considered when we reduce using S→aA rule.

 

 

0
0
3 votes
3 votes

Answer is C

Answer:

Related questions