300 views
1 1 vote

A shift-reduce parser carries out the actions specified within braces immediately after reducing with the corresponding rule of the grammar:

  • $A \rightarrow a B \quad$ {print "1"}
     
  • $A \rightarrow b \quad$    {print "2"}
     
  • $B \rightarrow A c$     {print "3"}

What is the translation of $a a b c c$ using the syntax-directed translation scheme described by the above rules?

  1. $23131$
     
  2. $21323$
     
  3. $21321$
     
  4. $12312$

2 Answers

1 1 vote

1. $A \rightarrow a B$

2. $A \rightarrow b$

3. $B \rightarrow A c$

Input string: $a a b c c$

We want the rightmost derivation in reverse (reductions).

Rightmost Derivation


Start symbol: $A$


Step 1:

$$
\begin{aligned}
& A \rightarrow a B \\
& a B
\end{aligned}
$$


Step 2:

$$
\begin{aligned}
& B \rightarrow A c \\
& \text { a Ac }
\end{aligned}
$$


Step 3:

$$
\begin{aligned}
& A \rightarrow a B \\
& a a B c
\end{aligned}
$$


Step 4:

$$
\begin{aligned}
& B \rightarrow A c \\
& \text { a a Acc }
\end{aligned}
$$


Step 5:

$$
\begin{aligned}
& A \rightarrow b \\
& a a b c c
\end{aligned}
$$


So the rightmost derivation is:

$$
A \Rightarrow a B \Rightarrow a A c \Rightarrow a a B c \Rightarrow a a A c c \Rightarrow a a b c c
$$

Reduction steps in a bottom-up parser correspond to reversing the rightmost derivation:

1. Start with $a a b c c$

2. Reduce $b \rightarrow A$ using $A \rightarrow b$ (print " 2 ") → a a A c c

3. Reduce $A c \rightarrow B$ using $B \rightarrow A c$ (print " 3 ") → а а в C

4. Reduce $a B \rightarrow A$ using $A \rightarrow a B$ (print "1") → a A c

5. Reduce $A c \rightarrow B$ using $B \rightarrow A c$ (print " 3 ") → а в

6. Reduce $a B \rightarrow A$ using $A \rightarrow a B$ (print " 1 ") → A

matches with option A

Answer:
Position:
Show:

Related questions

1 1 vote
3 3 answers
322
322 views
GO Classes asked Nov 29, 2025
322 views
Consider the following grammar with semantic actions inside braces:$S \rightarrow a A \quad$ {print "1"} $S \rightarrow b \quad$ {print " 2 " } $A \rightarrow S b \quad$ ...
2 2 votes
3 3 answers
437
437 views
GO Classes asked Nov 29, 2025
437 views
Given the following expression grammar:$$\begin{gathered}E \rightarrow E+T|T * E| T \\T \rightarrow T / F \mid F \\F \rightarrow \mathrm{id}\end{gathered}$$Which of the f...
1 1 vote
2 2 answers
315
315 views
GO Classes asked Nov 29, 2025
315 views
The number of tokens in the following C statement is $\_\_\_\_$ . scanf("%d %f",&num,&value); 
2 2 votes
2 2 answers
323
323 views
GO Classes asked Nov 29, 2025
323 views
The number of tokens in the following C statement is $\_\_\_\_$for(int k=0; k<MAX; k++) { sum += data[k];}