150 views
1 votes
1 votes
Which of the following grammar is/are left-recursive? (Mark all the appropriate choices)
  1. $A \rightarrow B \mid a \mid CBD$
    $B \rightarrow C\mid b$
    $C \rightarrow A \mid c$
    $D \rightarrow d$
  2. $A \rightarrow Ba \mid C$
    $B \rightarrow AA$
    $C \rightarrow B \mid b$
  3. $Q \rightarrow QED \mid q$
    $E \rightarrow e$
    $D \rightarrow NFA \mid d$
    $N \rightarrow DFA \mid n$
    $F \rightarrow f$
    $A \rightarrow a$
  4. $S \rightarrow (L) \mid x$
    $L \rightarrow SL'$
    $L' \rightarrow \epsilon \mid ,SL'$

1 Answer

3 votes
3 votes

We have to check each and every option.

Option $A:$

  • $A \rightarrow B \mid a \mid-CBD$
  • $B \rightarrow C\mid b$
  • $C \rightarrow A \mid c$
  • $D \rightarrow d$

This grammar is not immediately left-recursive, because there is no single production $X \rightarrow X \alpha.$ However, it is left recursive because there are valid derivations of the form $A\overset{\ast}{\rightarrow} A\alpha\:(\text{and}\: B\overset{\ast}{\rightarrow} B\beta\:\text{and}\:C\overset{\ast}{\rightarrow} C\gamma).$

For example$:A \rightarrow B \rightarrow C \rightarrow A,$  so $A\overset{\ast}{\rightarrow} A.$

Option $B:$

  • $A \rightarrow Ba \mid C$
  • $B \rightarrow AA$
  • $C \rightarrow B \mid b$

For example$:A \rightarrow C \rightarrow B \rightarrow AA,$  so $A\overset{\ast}{\rightarrow} A.$

Option $C:$

  • $Q \rightarrow QED \mid q$
  • $E \rightarrow e$
  • $D \rightarrow NFA \mid d$
  • $N \rightarrow DFA \mid n$
  • $F \rightarrow f$
  • $A \rightarrow a$

For example$:D \rightarrow NFA \rightarrow DFAFA.$  so $D\overset{\ast}{\rightarrow} D.$

Option $D:$

  • $S \rightarrow (L) \mid x$
  • $L \rightarrow SL'$
  • $L' \rightarrow \epsilon \mid ,SL'$

In this grammar, there is no direct or indirect left recursion.

Actually, the above grammar is free from left recursion, when we remove left recursion from the following grammar.

  • $S \rightarrow (L) \mid x$
  • $L \rightarrow L,S \mid S$

References:

So, the correct answer is $A; B; C.$

edited by
Answer:

Related questions

6 votes
6 votes
1 answer
2
6 votes
6 votes
1 answer
4