Ambiguity in Context Free Grammar - Automata

Ambiguity in Context Free Grammar?

Context Free Grammar

Context Free Grammar has one condition for production rules, which is, on the left-hand side of each rule, there must be only single variable, and on the right-hand side there may be a combination of variables and terminals including ?.

A Context Free Grammar G = (V, T, P, S) is said to be ambiguous, if there exists at least one string in L (G), which can be generated in different ways. If the same string is generated by different ways, we can say that string is derived ambiguously. This type of string will have different derivation tree or parse tree, and have several meanings.

In other words, “a grammar G is ambiguous, if some string from grammar has more than one leftmost derivation and right most derivation tree.”

These types of generated results are undesirable in any programming languages, because a given program should have a unique interpretation.

Example 1:

Consider the grammar G whose productions are:

S -> S1S

S -> 0

Prove that the above given grammar is ambiguous.

Solution:

To show that given grammar is ambiguous, we need to find a string w ? L (G).

Let w = 0101010.

1) Firstly, we check derivation of string using this

 S => S 1 S

   => 0 1 S

   => 01S1S

   => 0101S

   => 0101S1S

   => 010101S

   => 0101010

The first derivation tree for string w = 0101010.

Ambiguity in Context Free Grammar

2) Now, we check derivation of string using this

 S => S 1 S

   => S1S1 S

   => 01S1S

   => 0101S

   => 0101S1S

   => 010101S

   => 0101010

The second derivation tree for given string w = 0101010.

Ambiguity in Context Free Grammar

Both the derivation trees are different. Hence it is proved that given context free grammar G is ambiguous.

Example 2:

Consider the grammar G whose productions are:

S -> 1A0S

S -> 1A0S1S

A -> 1

S -> 0

Prove that the above given grammar is ambiguous.

Solution:

To show that given grammar is ambiguous, we need to find a string w ? L (G).

Let w = 110110010.

1) Firstly, we check derivation of string using this, S => 1A0S

   => 110S

   => 1101A0S1S

   => 110110S1S

   => 11011001S

   => 110110010

The first derivation tree for string w = 110110010.

Ambiguity in Context Free Grammar

2) Now, we check derivation of string using this

 S => 1A0S1S

    => 110S1S

    => 1101A0S1S  

   => 110110S1S

   => 11011001S

   => 110110010

The second derivation tree for given string w = 110110010.

Ambiguity in Context Free Grammar

Both derivation trees are different, hence it is proved that given context free grammar G is ambiguous.

Example 3:

Consider the grammar G whose productions are:

S -> AA

A -> AAA

A -> 1A

A -> A1

A -> 0

Prove that the above given grammar is ambiguous or not for string 010.

Solution:

To show that given grammar is ambiguous we need a string w ? L (G).

Let w = 010.

1) Firstly we check derivation of string using this

 S -> AA

   => 0A

   => 01A

   => 010

The first derivation tree for string w = 010.

Ambiguity in Context Free Grammar

2) Now, we check derivation of string using this

 S -> AA

   => A1A

   => 01A

   => 010

The second derivation tree for given string w = 010.

Ambiguity in Context Free Grammar

Both derivation trees are different, hence it is proved that given context free grammar G is ambiguous.

Example 4:

Consider the grammar G whose productions are:

S -> AB

S -> 00B

A -> 0

A -> A0

B -> 1

Prove that the above given grammar is ambiguous or not.

Solution:

To show that given grammar is ambiguous we need to find a string w ? L (G).

Let w = 001.

For string w = 001, we can derive two different left most derivation tree from given grammar.

1) Firstly, we check left most derivation tree of string using this

 S => 00B

   => 001

The first left most derivation tree for string w = 001.

Ambiguity in Context Free Grammar

2) Now, we check left most derivation tree of string using this

 S -> AB

   => A0B

   => 00B

   => 001

The second leftmost derivation tree for given string w = 001.

Ambiguity in Context Free Grammar

Both derivation trees are different, hence it is proved that given context free grammar G is ambiguous.


Related Topics

Closure Properties of Regular Languages -Automata

Closure Properties of Regular Languages We use the term “Closure” when we talk about sets of things. If we have two regular languages L1 and L2, and L is obtained by...

3 minutes read.

Automata Regular Expressions

Regular Expressions Regular expressions are also referred as rational expressions, which are used to describe the algebraic description of regular languages. It is generally a sequence of characters that is used...

3 minutes read.

Conversion from NFA to DFA

Conversion from NFA to DFA A Non-deterministic Finite Automata (NFA) is a finite state machine, in which, the move from one state to another is not fully deterministic, i.e., for a...

4 minutes read.

Pumping Lemma for Regular Languages - Automata

Pumping Lemma for Regular Languages The language accepted by the finite automata is called Regular Language. If we are given a language L and asked whether it is regular or not?...

4 minutes read.

Derivation Tree of Context Free Grammar - Automata

Derivation Tree of Context Free Grammar Derivation tree gives a way to show how a string can be derived from context free grammar. It is also called as parse tree, production...

3 minutes read.

Conversion from Moore Machine to Mealy Machine

Conversion from Moore Machine to Mealy Machine Moore Machine The output of Moore machine depends only on the present state. In this, if machine has N number of states, then it will...

6 minutes read.

Recursive Language and Recursive Enumerable Language

Recursive Language and Recursive Enumerable Language Before discussing Recursive language and Recursive Enumerable language, let us see what Turing machine responds for some input string. If we have the Turing machine T...

4 minutes read.

Finite Automata

Finite Automata Finite state Automata or Finite State Machine is the simplest model used in Automata. Finite state automata accepts regular language. In this, the term finite means it has a...

4 minutes read.

Conversion from Mealy Machine to Moore Machine

Conversion from Mealy Machine to Moore Machine In this topic, we will see different ways that are used for the conversion of Mealy Machine to Moore Machine. Moore Machine The output of Moore...

4 minutes read.

Context Free Grammar - Automata

Context Free Grammar Grammar defines a set of rules, and with the help of these rules valid sentences in a language are constructed. A grammar consists of collection of substitution rules,...

3 minutes read.

Automata Greibach Normal Form

Greibach Normal Form In Greibach Normal Form, there is restriction on the position, in which, terminals and variables can appear on right-hand side of production rules. In Greibach Normal Form, every...

4 minutes read.

Converting Finite Automata to Regular Expression using Arden’s Theorem

Converting Finite Automata to Regular Expression using Arden’s Theorem The Arden’s Theorem can be applied to find the regular expression recognized by the given transition diagram. This theorem can be applied...

4 minutes read.

Equivalence of Regular Grammar and Finite Automata

Equivalence of Regular Grammar and Finite Automata Regular Grammar / type -3 grammar A regular grammar or type-3 defines the language called regular language that is accepted by finite Automata.A Regular Grammar...

3 minutes read.

Automata Tutorial

What is Automata? Automata is a mathematical model and abstract model, which is used to detect string in various languages. In Automata, Regular language is recognized by Finite state AutomataContext Free language...

4 minutes read.

Minimization of Finite Automata with Output

Minimization of Finite Automata with Output The method to minimize the Moore and Mealy machine is very much similar to the method that we have used to minimize the DFA. Methods to...

3 minutes read.

Minimization of Finite Automata

Minimization of Finite Automata The term minimization refers to the construction of finite automata with a minimum number of states, which is equivalent to the given finite automata. The number of...

4 minutes read.

Pumping Lemma for Context Free Languages

Pumping Lemma for Context Free Languages The Pumping Lemma is made up of two words, in which, the word pumping is used to generate many input strings by pushing the symbol...

3 minutes read.

Finite Automata with Output

Finite Automata with Output The Finite State machine is similar to Finite Automata, except that it has the additional capacity of producing output. Finite State machine = Finite Automata + Output Capabilities Types...

3 minutes read.

Chomsky's Normal Form - Automata

Chomsky's Normal Form (CNF) In context free grammar, the left-hand side of production rules contains only one variable, and right side may contain any number of variables or terminals in production...

4 minutes read.

Simplification of Context Free Grammar - Automata

Simplification of Context Free Grammar Context Free Grammar has recursive structure. The languages that are accepted with Context Free Grammar are called Context Free Languages. Context Free Grammar has one condition...

3 minutes read.