Category: Subjects

Recursive Grammar | Left Recursive Grammar

Grammar in Automata-

 

Before you go through this article, make sure that you have gone through the previous article on Types of Grammar in Automata.

 

On the basis of number of strings, grammars are classified as-

 

 

  1. Recursive Grammar
  2. Non-Recursive Grammar

 

1. Recursive Grammar-

 

  • A grammar is said to be recursive if it contains at least one production that has the same variable at both its LHS and RHS.

OR

  • A grammar is said to be recursive if and only if it generates infinite number of strings.

 

A recursive grammar may be either-

  1. Left recursive grammar
  2. Right recursive grammar

 

A) Left Recursive Grammar-

 

  • A recursive grammar is said to be left recursive if the leftmost variable of RHS is same as variable of LHS.

OR

  • A recursive grammar is said to be left recursive if it has Left Recursion.

 

Example-

 

S → Sa / b

(Left Recursive Grammar)

 

B) Right Recursive Grammar-

 

  • A recursive grammar is said to be right recursive if the rightmost variable of RHS is same as variable of LHS.

OR

  • A recursive grammar is said to be right recursive if it has right recursion.

 

Example-

 

S → aS / b

(Right Recursive Grammar)

 

2. Non-Recursive Grammar-

 

  • A grammar is said to be non-recursive if it contains no production that has the same variable at both its LHS and RHS.

OR

  • A grammar is said to be non-recursive if and only if it generates finite number of strings.

 

NOTE

A non-recursive grammar has neither left recursion nor right recursion.

 

Example-

 

S → aA / bB

A → a / b

B → c / d

(Non-Recursive Grammar)

 

The language generated from this grammar is L = { aa , ab , bc , bd }

Since the grammar generates finite number of strings, therefore it is a non-recursive grammar.

 

Also Read- Ambiguous Grammar

 

Important Notes-

 

Note-01:

 

The grammar which is either left recursive or right recursive is always unambiguous.

Examples-

  • S → aS / b   (Unambiguous Grammar)
  • S → Sa / b   (Unambiguous Grammar)

 

Note-02:

 

The grammar which is both left recursive and right recursive is always ambiguous.

Example-

E → E + E / E – E / E x E / id

(Ambiguous Grammar)

 

Note-03:

 

  • Left recursive grammar is not suitable for Top down parsers.
  • This is because it makes the parser enter into an infinite loop.
  • To avoid this situation, it is converted into its equivalent right recursive grammar.
  • This is done by eliminating left recursion from the left recursive grammar.

 

Note-04:

 

  • The conversion of left recursive grammar into right recursive grammar and vice-versa is decidable.

 

To gain better understanding about Recursive Grammar,

Watch this Video Lecture

 

Next Article- Ambiguous Vs Unambiguous Grammar

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Language Ambiguity | Ambiguous Language

Language Of Grammar-

 

Before you go through this article, make sure that you have gone through the previous article on Language of Grammar.

 

We have discussed-

  • Language of grammar is the set of all strings that can be generated from that grammar.
  • Finite language consists of finite number of strings.
  • Infinite language consists of infinite number of strings.

 

In this article, we will discuss about Language Ambiguity.

 

Language Ambiguity-

 

If every grammar that generates language L is ambiguous,

then language L is called as Inherently ambiguous language.

OR

A language is inherently ambiguous if all the grammars which generate that language are ambiguous.

 

Also Read- Ambiguous Grammar

 

NOTE-

 

  • The term “inherently ambiguous language” is used instead of “ambiguous language”.
  • This is because all the grammars which generate that language are ambiguous.

 

Important Points-

 

  • If a grammar is ambiguous, it does not imply that its language will be ambiguous too.
  • If a grammar is ambiguous, its language may be unambiguous.
  • If a grammar is ambiguous, its language will be unambiguous when there exists at least one unambiguous grammar which generates that language.

 

Example Of Not An Inherently Ambiguous Language-

 

Consider the following language-

L = an , n>=0

 

  • This language consists of strings having any number of a’s.
  • Few grammars which generate this language are-

 

Grammar-01:

 

S → aS / ∈

(Unambiguous Grammar)

 

Grammar-02:

 

S → aS / a / ∈

(Ambiguous Grammar)

 

Grammar-03:

 

S → aS / Sa / ∈

(Ambiguous Grammar)

 

Grammar-04:

 

S → aS / a / Sa / ∈

(Ambiguous Grammar)

 

  • All the above grammars generate the same language L = an , n>=0.
  • Grammar-01 is unambiguous.
  • Since there exists at least one unambiguous grammar which generates language L.
  • Therefore, L is not an inherently ambiguous language.

 

Example Of An Inherently Ambiguous Language-

 

Consider the following language-

L = { anbncm } ∪ { anbmcm }

 

There exists only one grammar which generates this language L.

The grammar is-

 

S → S1 / S2

S1 → S1C / A

A → aAb / ∈

S2 → aS2 / B

B → bBc / ∈

 

This grammar is ambiguous in nature.

This is because following two parse trees exist for string w = abc-

 

Also Read- Checking Grammar Ambiguity

 

 

  • There exists no language that generates this language L and is unambiguous.
  • Therefore, L is an inherently ambiguous language.

 

Next Article- Context Free Grammar

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Grammar Ambiguity | Check Ambiguous Grammar

Grammar Ambiguity-

 

Before you go through this article, make sure that you have gone through the previous article on Ambiguous Grammar.

 

  • There exists no algorithm to check whether any given grammar is ambiguous or not.
  • This general decision problem is undecidable-

“Whether a grammar is ambiguous or not?”

  • This is because it can be shown that this problem is equivalent to Post Correspondence Problem.

 

General Approach To Check Grammar Ambiguity-

 

To check whether a given grammar is ambiguous or not, we follow the following steps-

 

Step-01:

 

We try finding a string from the Language of Grammar such that for the string there exists more than one-

  • parse tree
  • or derivation tree
  • or syntax tree
  • or leftmost derivation
  • or rightmost derivation

 

Step-02:

 

If there exists at least one such string, then the grammar is ambiguous otherwise unambiguous.

 

PROBLEMS BASED ON CHECKING WHETHER GRAMMAR IS AMBIGUOUS-

 

Problem-01:

 

Check whether the given grammar is ambiguous or not-

S → SS

S → a

S → b

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = abba

Now, let us draw parse trees for this string w.

 

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

Problem-02:

 

Check whether the given grammar is ambiguous or not-

S → A / B

A → aAb / ab

B → abB / ∈

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = ab

Now, let us draw parse trees for this string w.

 

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

Problem-03:

 

Check whether the given grammar is ambiguous or not-

S → AB / C

A → aAb / ab

B → cBd / cd

C → aCd / aDd

D → bDc / bc

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = aabbccdd

Now, let us draw parse trees for this string w.

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

Problem-04:

 

Check whether the given grammar is ambiguous or not-

S → AB / aaB

A → a / Aa

B → b

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = aab

Now, let us draw parse trees for this string w.

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

Problem-05:

 

Check whether the given grammar is ambiguous or not-

S → a / abSb / aAb

A → bS / aAAb

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = abababb

Now, let us draw parse trees for this string w.

 

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

Problem-06:

 

Check whether the given grammar is ambiguous or not-

E → E + T / T

T → T x F / F

F → id

 

Solution-

 

  • There exists no string belonging to the language of grammar which has more than one parse tree.
  • Since a unique parse tree exists for all the strings, therefore the given grammar is unambiguous.

 

Problem-07:

 

Check whether the given grammar is ambiguous or not-

S → aSbS / bSaS / ∈

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = abab

Now, let us draw parse trees for this string w.

 

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

Problem-08:

 

Check whether the given grammar is ambiguous or not-

R → R + R / R . R / R* / a / b

 

Solution-

 

Let us consider a string w generated by the given grammar-

w = ab + a

Now, let us draw parse trees for this string w.

 

 

Since two different parse trees exist for string w, therefore the given grammar is ambiguous.

 

To gain better understanding about Grammar Ambiguity,

Watch this Video Lecture

 

Next Article- Converting Ambiguous into Unambiguous Grammar

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Difference between Ambiguous and Unambiguous Grammar

Difference Between Ambiguous and Unambiguous Grammar-

 

Before you go through this article, make sure that you have gone through the previous article on Ambiguous Grammar.

 

Some of the important differences between ambiguous grammar and unambiguous grammar are-

 

Ambiguous Grammar Unambiguous Grammar
A grammar is said to be ambiguous if for at least one string generated by it, it produces more than one-

  • parse tree
  • or derivation tree
  • or syntax tree
  • or leftmost derivation
  • or rightmost derivation
A grammar is said to be unambiguous if for all the strings generated by it, it produces exactly one-

  • parse tree
  • or derivation tree
  • or syntax tree
  • or leftmost derivation
  • or rightmost derivation
For ambiguous grammar, leftmost derivation and rightmost derivation represents different parse trees. For unambiguous grammar, leftmost derivation and rightmost derivation represents the same parse tree.
Ambiguous grammar contains less number of non-terminals. Unambiguous grammar contains more number of non-terminals.
For ambiguous grammar, length of parse tree is less. For unambiguous grammar, length of parse tree is large.
Ambiguous grammar is faster than unambiguous grammar in the derivation of a tree.

(Reason is above 2 points)

Unambiguous grammar is slower than ambiguous grammar in the derivation of a tree.
Example-

 

E → E + E / E x E / id

(Ambiguous Grammar)

 

Example-

E → E + T / T

T → T x F / F

F → id

(Unambiguous Grammar)

 

Next Article- Algorithm To Check Grammar Ambiguity

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.

Ambiguous Grammar | Grammar in Automata

Grammar in Automata-

 

Before you go through this article, make sure that you have gone through the previous article on Types of Grammar in Automata.

 

On the basis of number of derivation trees, grammars are classified as-

 

 

  1. Ambiguous Grammar
  2. Unambiguous Grammar

 

1. Ambiguous Grammar-

 

A grammar is said to ambiguous if for any string generated by it, it produces more than one-

  • Parse tree
  • Or derivation tree
  • Or syntax tree
  • Or leftmost derivation
  • Or rightmost derivation

 

Example-

 

Consider the following grammar-

E → E + E / E x E / id

Ambiguous Grammar

 

This grammar is an example of ambiguous grammar.

Any of the following reasons can be stated to prove the grammar ambiguous-

 

Reason-01:

 

Let us consider a string w generated by the grammar-

w = id + id x id

Now, let us draw the parse trees for this string w.

 

 

Since two parse trees exist for string w, therefore the grammar is ambiguous.

 

Reason-02:

 

Let us consider a string w generated by the grammar-

w = id + id x id

Now, let us draw the syntax trees for this string w.

 

 

Since two syntax trees exist for string w, therefore the grammar is ambiguous.

 

Reason-03:

 

Let us consider a string w generated by the grammar-

w = id + id x id

Now, let us write the leftmost derivations for this string w.

 

 

Since two leftmost derivations exist for string w, therefore the grammar is ambiguous.

 

Reason-04:

 

Let us consider a string w generated by the grammar-

w = id + id x id

Now, let us write the rightmost derivations for this string w.

 

 

Since two rightmost derivations exist for string w, therefore the grammar is ambiguous.

 

Also Read- Checking Grammar Ambiguity

 

2. Unambiguous Grammar-

 

A grammar is said to unambiguous if for every string generated by it, it produces exactly one-

  • Parse tree
  • Or derivation tree
  • Or syntax tree
  • Or leftmost derivation
  • Or rightmost derivation

 

Example-

 

Consider the following grammar-

E → E + T / T

T → T x F / F

F → id

Unambiguous Grammar

 

This grammar is an example of unambiguous grammar.

 

Also Read- Removing Ambiguity From Ambiguous Grammar

 

To gain better understanding about Ambiguous Grammar,

Watch this Video Lecture

 

Next Article- Recursive Grammar

 

Get more notes and other study material of Theory of Automata and Computation.

Watch video lectures by visiting our YouTube channel LearnVidFun.