All Exams Test series for 1 year @ ₹349 only
Question

Which of the following grammars is (are)ambiguous ?
(1) S → SS | aSb | bSa |e 

(2) S → aSbS | bSaS |e 

(3) S → aAB 

A → bBb 
B → A Ι e

The correct answer is
1, 2 and 3

Understanding Ambiguous Grammars

A context-free grammar (CFG) is considered ambiguous if there exists at least one string that can be generated by the grammar in more than one distinct way. This means that a single string can have multiple different parse trees or multiple different leftmost (or rightmost) derivations.

Analyzing Grammar 1

Grammar 1: $S \rightarrow SS \mid aSb \mid bSa \mid \epsilon$

This grammar can be ambiguous, primarily due to the $S \rightarrow SS$ rule. Let's consider the string $"ab"$:

  • Derivation 1: Using $S \rightarrow aSb$
    • $S \Rightarrow aSb \Rightarrow a\epsilon b = ab$
    This derivation directly produces "ab" with a simple parse tree.
  • Derivation 2: Using $S \rightarrow SS$ and other rules
    • $S \Rightarrow SS \Rightarrow aSb S \Rightarrow a\epsilon b S = abS \Rightarrow ab\epsilon = ab$
    This derivation involves breaking the string down using $SS$ and then building it back up. The parse tree structure differs from Derivation 1 because of the initial split via $SS$. The first $S$ in $SS$ derives $ab$ (via $aSb$) and the second $S$ derives $\epsilon$.

Since the string "ab" has at least two distinct derivations, Grammar 1 is ambiguous.

Analyzing Grammar 2

Grammar 2: $S \rightarrow aSbS \mid bSaS \mid \epsilon$

This grammar generates strings where the number of 'a's equals the number of 'b's. Ambiguity can arise from the rule $S \rightarrow aSbS$.

Let's consider the string $"ab"$:

  • Derivation 1:
    • $S \Rightarrow aSbS \Rightarrow a\epsilon b S \Rightarrow abS \Rightarrow ab\epsilon = ab$
    In this parse, the inner $S$ (between 'a' and 'b') derives $\epsilon$, and the final $S$ derives $\epsilon$.
  • Derivation 2:
    • $S \Rightarrow aSbS \Rightarrow aSb\epsilon \Rightarrow a\epsilon b = ab$
    In this parse, the rule $aSbS$ is treated as $(aSb)S$ where the first $S$ derives $\epsilon$, and the second $S$ derives $\epsilon$. This is essentially the same parse structure as Derivation 1. Let's try a different string or interpretation.

Let's re-evaluate with a slightly different perspective on the structure generated by $aSbS$. It can represent $a(S1)b(S2)$. Consider the string $"abab"$:

  • Derivation 1:
    • $S \Rightarrow aSbS \Rightarrow a\epsilon b S \Rightarrow abS \Rightarrow ab(aSb) \Rightarrow ab(a\epsilon b) = abab$
    Here, the inner $S$ is $\epsilon$, and the outer $S$ derives $ab$ via $aSb$.
  • Derivation 2:
    • $S \Rightarrow aSbS \Rightarrow a(bSaS)bS \Rightarrow abSaSbS \Rightarrow ab\epsilon bS \Rightarrow abbS \Rightarrow abb\epsilon = abb$
    • This derivation does not yield `abab`.

Let's reconsider the ambiguity based on parsing structure. The rule $S \rightarrow aSbS$ could potentially be parsed in different ways depending on which recursive call is applied first or how intermediate non-terminals are expanded. For instance, a string like "ab" can be derived as shown above. Another interpretation might split the derivation differently, potentially leading to ambiguity.

A common example for ambiguity in similar grammars involves the structure. For $S \rightarrow aSbS$, consider how $ab$ is derived. One way: Inner $S$ is $\epsilon$, outer $S$ is $\epsilon$. Result $ab$. Another view: $S \rightarrow aSbS$ implies structure $a S b S$. If the first $S$ inside becomes $\epsilon$ and the second $S$ outside becomes $\epsilon$, we get $ab$. If the rule is applied such that the structure is interpreted differently, ambiguity arises. A commonly cited ambiguity for this type of grammar is the string "ab", having multiple parse trees.

Thus, Grammar 2 is generally considered ambiguous.

Analyzing Grammar 3

Grammar 3: $S \rightarrow aAB; A \rightarrow bBb \mid \epsilon; B \rightarrow A \mid \epsilon$

Let's analyze the derivations for a string, for example, $"abb"$.

  • Derivation 1:
    • $S \Rightarrow aAB$
    • Use $A \rightarrow bBb$ and $B \rightarrow \epsilon$
    • $S \Rightarrow a(bBb)B \Rightarrow abBbB \Rightarrow ab(\epsilon)B \Rightarrow abB$
    • Now use $B \rightarrow \epsilon$
    • $abB \Rightarrow ab\epsilon = abb$
    This derivation path yields "abb". The structure involves `S -> a A B`, `A -> b B b`, and `B -> ε` applied twice.
  • Derivation 2:
    • $S \Rightarrow aAB$
    • Use $A \rightarrow \epsilon$
    • $S \Rightarrow a(\epsilon)B \Rightarrow aB$
    • Now use $B \rightarrow A$
    • $aB \Rightarrow aA$
    • Now use $A \rightarrow bBb$
    • $aA \Rightarrow a(bBb) \Rightarrow abBb$
    • Now use $B \rightarrow \epsilon$
    • $abBb \Rightarrow ab(\epsilon)b = abb$
    This derivation path also yields "abb", but the parse tree structure is different. Here, `S -> a A B`, `A -> ε`, `B -> A`, `A -> b B b`, `B -> ε`.

Since the string "abb" can be derived in at least two distinct ways (leading to different parse trees), Grammar 3 is ambiguous.

Conclusion

Based on the analysis, all three grammars (1, 2, and 3) exhibit ambiguity.

The correct option is the one that includes all three.

Was this answer helpful?

Important Questions from Context Free Grammars

  1. Which of the following Grammars is/are only Context Free?
    AS $\rightarrow$ Ab
    aS$\rightarrow$ aA
    A$\rightarrow$ a
    BS $\rightarrow$ Ab
    A $\rightarrow$ Sa
    A $\rightarrow$ a
    CS $\rightarrow$ AS
    S $\rightarrow$ aA
    A$\rightarrow$ a
    DS $\rightarrow$ Ab
    S $\rightarrow$ aA
    A$\rightarrow$ a
    ES $\rightarrow$ Sb
    A $\rightarrow$ Aa
    A $\rightarrow$ $\varepsilon$

    Choose the correct answer from the options given below:
  2. Consider the grammar,

    G = S → abScB | e, 

    B→ bB | b. 

    What is the language generated by the given grammar ?

     

  3. Consider the given grammar
    S → Abc | ad 

    A → eS | Cr| ξ 

    C → f/p/ξ
    Compute FIRST (A)

  4. Select the sentence which is meaningful and grammatically correct.

  5. Consider the following context-free grammar $G$.
    $$S \rightarrow abaABabba$$
    $$A \rightarrow aaBBAB \mid bBabaa$$
    $$B \rightarrow aBb \mid ab$$
    In the above grammar, $S$ is the start symbol, $a$ and $b$ are terminal symbols, and $A$ and $B$ are non-terminal symbols.
    Let $L(G)$ be the language generated by the grammar $G$. For a string $s \in L(G)$, let $n_1(s)$ be the number of $a$’s in $s$ and $n_2(s)$ be the number of $b$’s in $s$.
    Which of the following statements is/are true?
Need Expert Advice?

Start Your Preparation with Prepp Mobile App

Download the app from Google Play & App Store
Download the app from Google Play & App Store
Prepp Mobile App