Open In App

Ambiguous Grammar

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Context-Free Grammars (CFGs) is a way to describe the structure of a language, such as the rules for building sentences in a language or programming code. These rules help define how different symbols can be combined to create valid strings (sequences of symbols).

CFGs can be divided into two types based on how they create derivation trees :

  • Ambiguous grammars: These grammars can create more than one derivation tree for the same string. This means that a string can have different meanings or interpretations depending on how it's parsed.
  • Unambiguous grammars: These grammars only allow one derivation tree for each string, so there's no confusion about how the string should be interpreted.

This classification helps us to understand how CFGs are used in language and computer systems to process information.

Ambiguous grammar 

A Context-Free Grammar (CFG) is called ambiguous if there is a string that can have more than one valid derivation tree. This means the string can be generated in different ways, either through different LeftMost Derivations (LMDT) or RightMost Derivations (RMDT).

In simple terms, a CFG G = (V, T, P, S) is ambiguous if there is a string in the terminal set T that can be produced in more than one way, creating multiple parse trees. Here:

  • V is the set of variables (non-terminal symbols),
  • T is the set of terminal symbols,
  • P is the set of production rules,
  • S is the start symbol, which is a special variable from where the derivation begins.

This happens when a string has multiple possible ways to be derived using the grammar rules, leading to more than one valid tree structure.

Example 1. Let us consider this grammar: E-> E +E | E*E| id, We can create 2 parse tree from this grammar to obtain a string id + id * id.

ambiguous_grammar

Both the above parse trees are derived from the same grammar rules but both parse trees are different. Hence the grammar is ambiguous.    

Example 2. Let us now consider the following grammar:  

Set of alphabets ? = {0,…,9, +, *, (, )}

E -> I
E -> E + E
E -> E * E
E -> (E)
I -> ? | 0 | 1 | … | 9
 

From the above grammar String 3*2+5 can be derived in 2 ways:  

I) First leftmost derivation                   II) Second leftmost derivation
E=>E*E E=>E+E
=>I*E =>E*E+E
=>3*E+E =>I*E+E
=>3*I+E =>3*E+E
=>3*2+E =>3*I+E
=>3*2+I =>3*2+I
=>3*2+5 =>3*2+5

  Following are some examples of ambiguous grammar:  

  • S-> aS |Sa| ?
  • E-> E +E | E*E| id
  • A -> AA | (A) | a
  • S -> SS|AB , A -> Aa|a , B -> Bb|b

Whereas following grammars are unambiguous:  

  • S -> (L) | a, L -> LS | S
  • S -> AA , A -> aA , A -> b

Removal of Ambiguity in Grammar

To remove ambiguity from a grammar, follow these simple steps:

  1. Simplify production rules: Break down complex rules into smaller and simpler ones. This way, the grammar won't allow multiple interpretations for the same string.
  2. Set precedence and associativity: For things like math operations, make sure the order in which they are applied is clear. For example, define that multiplication happens before addition, or how to group operations (left-to-right or right-to-left).
  3. Fix left recursion: Left recursion occurs when a rule refers to itself in a way that causes infinite loops. To fix it, change the rules so that recursion happens at the end, not at the start.
  4. Factor out common parts: If two rules start the same way, combine the common part. For example, instead of having rules like A → αβ and A → αγ, make it A → αA' and A' → β | γ.

Read more about Removal of Ambiguity in Grammar.

Inherent Ambiguity

A Context-Free Language (CFL) is said to be inherently ambiguous if every possible grammar for the language is ambiguous. This means no matter how you write the grammar for the language, there will always be strings in that language that can be parsed in more than one way.

Example:

Consider the language L defined as:

L = { aⁿbⁿcᵐdᵐ : n ≥ 1, m ≥ 1 } ∪ { aⁿbᵐcᵐdⁿ : n ≥ 1, m ≥ 1 }

A grammar for this language could be:

  • S → AB | C
  • A → aAb | ab
  • B → cBd | cd
  • C → aCd | aDd
  • D → bDc | bc

Let’s see how the string aabbccdd can be parsed. This string has two possible leftmost derivations:

Using S → AB:

  • S ⇒ AB
  • A → aAbaAbB
  • A → abaabbB
  • B → cBdaabbcBd
  • B → cdaabbccdd


Using S → C:

  • S ⇒ C
  • C → aCdaCd
  • C → aDdaaDdd
  • D → bDcaabDcdd
  • D → bcaabbccdd
ambiguous_grammar_2

Both derivations result in the same string aabbccdd, but they come from different rules and paths, showing that there is more than one way to parse the string.
Since there are multiple ways to parse the string aabbccdd and this pattern holds for all grammars of L, we can conclude that L is inherently ambiguous. This means no grammar for L can avoid ambiguity for all strings in the language.

Important Points on Ambiguous Grammar

1) A grammar is ambiguous if it contains both left recursion and right recursion. This combination can lead to multiple ways of deriving the same string, creating more than one parse tree.

Example: S → SaS | ε
Here, the grammar has both left recursion (S → SaS) and right recursion (S → ε), making it ambiguous. This allows multiple derivations for the same string, such as {aa}, leading to more than one parse tree.

ambiguous_grammar_4

2) Even if a grammar does not have both left and right recursion, it can still be ambiguous. The absence of recursion does not guarantee that the grammar is unambiguous.

Example: S → aB | ab , A → AB | a , B → Abb | b
In this example, there is no left or right recursion, but the grammar is still ambiguous. For the string {ab}, we can derive it in multiple ways, resulting in more than one parse tree. This shows that even without recursion, a grammar can still be ambiguous.

ambiguous_grammar_3

From the above example, we can see that even if both left and right recursion are not present in grammar, the grammar can be ambiguous.


Next Article
Article Tags :

Similar Reads