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

Which of the following grammars is/are ambiguous?

Ambiguous Grammar Identification

A context-free grammar is considered ambiguous if there exists at least one string in the language generated by the grammar that has more than one distinct leftmost derivation, rightmost derivation, or parse tree.

Grammar Analysis

1. Grammar $S \rightarrow aSb \mid \epsilon$

This grammar generates the language $L = \{a^n b^n \mid n \ge 0\}$. For any string generated by this grammar, there is only one possible sequence of production rules to follow. For example, to generate $a^2b^2$, the derivation must be $S \Rightarrow aSb \Rightarrow aaSbb \Rightarrow aabb$. This grammar is unambiguous.

2. Grammar $E \rightarrow E + E \mid E * E \mid id$

This grammar is commonly used for arithmetic expressions. It is ambiguous because it does not inherently define the precedence or associativity of operators '+' and '*'. For instance, the string $id + id * id$ can be parsed in multiple ways:

  • $(id + id) * id$ (implying '+' has higher precedence or is evaluated left-to-right before '*')
  • $id + (id * id)$ (implying '*' has higher precedence)

The existence of multiple parse trees for the same string demonstrates ambiguity.

3. Grammar $S \rightarrow aS \mid Sa \mid \epsilon$

This grammar generates strings containing any number of 'a's. Consider the string $aa$. It can be derived in multiple ways:

  • Derivation 1: $S \Rightarrow aS \Rightarrow a(Sa) \Rightarrow a(a\epsilon) = aa$
  • Derivation 2: $S \Rightarrow Sa \Rightarrow (aS)a \Rightarrow (a\epsilon)a = aa$

Since there are multiple distinct leftmost derivations (and thus parse trees) for the string $aa$, this grammar is ambiguous.

4. Grammar $S \rightarrow aS \mid \epsilon$

This grammar generates the language $L = \{a^n \mid n \ge 0\}$. To generate any string $a^n$, the only possible derivation involves applying the rule $S \rightarrow aS$ exactly $n$ times, followed by $S \rightarrow \epsilon$. For example, $S \Rightarrow aS \Rightarrow aaS \Rightarrow aaaS \Rightarrow aaaa$. There is only one sequence of choices. This grammar is unambiguous.

Conclusion

Based on the analysis, grammars 2 ($E \rightarrow E + E \mid E * E \mid id$) and 3 ($S \rightarrow aS \mid Sa \mid \epsilon$) are ambiguous.

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. 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

  3. Consider the grammar,

    G = S → abScB | e, 

    B→ bB | b. 

    What is the language generated by the given grammar ?

     

  4. Consider the given grammar
    S → Abc | ad 

    A → eS | Cr| ξ 

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

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

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