Category: Theory of Automata & Computation

Chomsky Normal Form | Normal Forms in Automata

Normal Forms-

 

Normalization is performed in order to standardize the grammar.

 

  • By reducing the grammar, the grammar gets minimized but does not gets standardized.
  • This is because the RHS of productions have no specific format.
  • In order to standardize the grammar, normalization is performed using normal forms.

 

Types of Normal Forms-

 

The most frequently used normal forms are-

 

 

  1. Chomsky Normal Form (CNF)
  2. Greibach Normal Form (GNF)

 

In this article, we will discuss about Chomsky Normal Form.

 

Chomsky Normal Form-

 

A context free grammar is said to be in chomsky normal form (CNF) if all its productions are of the form-

BC or a

where A, B, C are non-terminals and a is a terminal.

 

From here, we infer-

  • To be in CNF, all the productions must derive either two non-terminals or a single terminal.
  • CNF restricts the number of symbols on the right side of a production to be two.
  • The two symbols must be non-terminals or a single terminal.

 

Example-

 

S → AB

A → a

B → b

 

This context free grammar is in chomsky normal form.

 

Steps-

 

The following steps are followed to standardize the grammar using CNF-

 

Rule-01:

 

Reduce the grammar completely by-

  • Eliminating ∈ productions
  • Eliminating unit productions
  • Eliminating useless productions

 

Also Watch- How To Reduce Grammar?

 

Rule-02:

 

Replace each production of the form A → B1B2B3….Bn where n > 2 with A → B1C where C → B2B3….Bn.

Repeat this step for all the productions having more than two variables on RHS.

 

Rule-03:

 

Replace each production of the form A → aB with A → XB and X → a.

Repeat this step for all the productions having the form A → aB.

 

PRACTICE PROBLEMS BASED ON CHOMSKY NORMAL FORM-

 

Problem-01:

 

Convert the given grammar to CNF-

S → aAD

A → aB / bAB

B → b

D → d

 

Solution-

 

Step-01:

 

The given grammar is already completely reduced.

 

Step-02:

 

The productions already in chomsky normal form are-

B → b        ………..(1)

D → d        ………..(2)

These productions will remain as they are.

 

The productions not in chomsky normal form are-

S → aAD                ………..(3)

A → aB / bAB         ………..(4)

We will convert these productions in chomsky normal form.

 

Step-03:

 

Replace the terminal symbols a and b by new variables Ca and Cb.

 

This is done by introducing the following two new productions in the grammar-

Ca → a       ………..(5)

Cb → b       ………..(6)

 

Now, the productions (3) and (4) modifies to-

S → CaAD                  ………..(7)

A → CaB / CbAB         ………..(8)

 

Step-04:

 

Replace AD and AB by new variables CAD and CAB respectively.

 

This is done by introducing the following two new productions in the grammar-

CAD → AD       ………..(9)

CAB → AB       ………..(10)

 

Now, the productions (7) and (8) modifies to-

S → CaCAD                  ………..(11)

A → CaB / CbCAB         ………..(12)

 

Step-05:

 

From (1), (2), (5), (6), (9), (10), (11) and (12), the resultant grammar is-

 

S → CaCAD

A → CaB / CbCAB

B → b

D → d

Ca → a

Cb → b

CAD → AD

CAB → AB

 

This grammar is in chomsky normal form.

 

Problem-02:

 

Convert the given grammar to CNF-

S → 1A / 0B

A → 1AA / 0S / 0

B → 0BB / 1S / 1

 

Solution-

 

Step-01:

 

The given grammar is already completely reduced.

 

Step-02:

 

The productions already in chomsky normal form are-

A → 0        ………..(1)

B → 1        ………..(2)

These productions will remain as they are.

 

The productions not in chomsky normal form are-

S → 1A / 0B           ………..(3)

A → 1AA / 0S         ………..(4)

B → 0BB / 1S         ………..(5)

We will convert these productions in chomsky normal form.

 

Step-03:

 

Replace the terminal symbols 0 and 1 by new variables C and D.

 

This is done by introducing the following two new productions in the grammar-

C → 0       ………..(6)

D → 1       ………..(7)

 

Now, the productions (3), (4) and (5) modifies to-

S → DA / CB           ………..(8)

A → DAA / CS         ………..(9)

B → CBB / DS         ………..(10)

 

Step-04:

 

Out of (8), (9) and (10), the productions already in Chomsky Normal Form are-

S → DA / CB           ………..(11)

A → CS                   ………..(12)

B → DS                   ………..(13)

These productions will remain as they are.

 

The productions not in chomsky normal form are-

A → DAA         ………..(14)

B → CBB         ………..(15)

We will convert these productions in Chomsky Normal Form.

 

Step-05:

 

Replace AA and BB by new variables E and F respectively.

 

This is done by introducing the following two new productions in the grammar-

E → AA       ………..(16)

F → BB       ………..(17)

 

Now, the productions (14) and (15) modifies to-

A → DE         ………..(18)

B → CF         ………..(19)

 

Step-06:

 

From (1), (2), (6), (7), (11), (12), (13), (16), (17), (18) and (19), the resultant grammar is-

 

S → DA / CB

A → CS / DE / 0

B → DS / CF / 1

C → 0

D → 1

E → AA

F → BB

 

This grammar is in chomsky normal form.

 

To gain better understanding about Chomsky Normal Form,

Watch this Video Lecture

 

Next Article- Algorithm To Decide Whether CFL Is Empty

 

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

Watch video lectures by visiting our YouTube channel LearnVidFun.

Minimization of DFA | Minimize DFA | Examples

Minimization of DFA-

 

The process of reducing a given DFA to its minimal form is called as minimization of DFA.

 

  • It contains the minimum number of states.
  • The DFA in its minimal form is called as a Minimal DFA.

 

How To Minimize DFA?

 

The two popular methods for minimizing a DFA are-

 

 

In this article, we will discuss Minimization of DFA Using Equivalence Theorem.

 

Minimization of DFA Using Equivalence Theorem-

 

Step-01:

 

  • Eliminate all the dead states and inaccessible states from the given DFA (if any).

 

Dead State

 

All those non-final states which transit to itself for all input symbols in ∑ are called as dead states.

 

Inaccessible State

 

All those states which can never be reached from the initial state are called as inaccessible states.

 

Step-02:

 

  • Draw a state transition table for the given DFA.
  • Transition table shows the transition of all states on all input symbols in Σ.

 

Step-03:

 

Now, start applying equivalence theorem.

  • Take a counter variable k and initialize it with value 0.
  • Divide Q (set of states) into two sets such that one set contains all the non-final states and other set contains all the final states.
  • This partition is called P0.

 

Step-04:

 

  • Increment k by 1.
  • Find Pk by partitioning the different sets of Pk-1 .
  • In each set of Pk-1 , consider all the possible pair of states within each set and if the two states are distinguishable, partition the set into different sets in Pk.

 

Two states q1 and q2 are distinguishable in partition Pk for any input symbol ‘a’,

if δ (q1, a) and δ (q2, a) are in different sets in partition Pk-1.

 

Step-05:

 

  • Repeat step-04 until no change in partition occurs.
  • In other words, when you find Pk = Pk-1, stop.

 

Step-06:

 

  • All those states which belong to the same set are equivalent.
  • The equivalent states are merged to form a single state in the minimal DFA.

 

Number of states in Minimal DFA

= Number of sets in Pk

 

PRACTICE PROBLEMS BASED ON MINIMIZATION OF DFA-

 

Problem-01:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

The given DFA contains no dead states and inaccessible states.

 

Step-02:

 

Draw a state transition table-

 

ab
q0q1q2
q1q1q3
q2q1q2
q3q1*q4
*q4q1q2

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 , q1 , q2 , q3 } { q4 }

P1 = { q0 , q1 , q2 } { q3 } { q4 }

P2 = { q0 , q2 } { q1 } { q3 } { q4 }

P3 = { q0 , q2 } { q1 } { q3 } { q4 }

 

Since P3 = P2, so we stop.

From P3, we infer that states q0 and q2 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Problem-02:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

  • State q3 is inaccessible from the initial state.
  • So, we eliminate it and its associated edges from the DFA.

 

The resulting DFA is-

 

 

Step-02:

 

Draw a state transition table-

 

ab
q0*q1q0
*q1*q2*q1
*q2*q1*q2

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 } { q1 , q2 }

P1 = { q0 } { q1 , q2 }

 

Since P1 = P0, so we stop.

From P1, we infer that states q1 and q2 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Problem-03:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

The given DFA contains no dead states and inaccessible states.

 

Step-02:

 

Draw a state transition table-

 

01
q0q1q3
q1q2*q4
q2q1*q4
q3q2*q4
*q4*q4*q4

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 , q1 , q2 , q3 } { q4 }

P1 = { q0 } { q1 , q2 , q3 } { q4 }

P2 = { q0 } { q1 , q2 , q3 } { q4 }

 

Since P2 = P1, so we stop.

From P2, we infer that states q1 , q2 and q3 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Problem-04:

 

Minimize the given DFA-

 

 

Solution-

 

Step-01:

 

  • State q5 is inaccessible from the initial state.
  • So, we eliminate it and its associated edges from the DFA.

 

The resulting DFA is-

 

 

Step-02:

 

Draw a state transition table-

 

01
q0q1q2
q1q2*q3
q2q2*q4
*q3*q3*q3
*q4*q4*q4

 

Step-03:

 

Now using Equivalence Theorem, we have-

P0 = { q0 , q1 , q2 } { q3 , q4 }

P1 = { q0 } { q1 , q2 } { q3 , q4 }

P2 = { q0 } { q1 , q2 } { q3 , q4 }

 

Since P2 = P1, so we stop.

From P2, we infer-

  • States q1 and q2 are equivalent and can be merged together.
  • States q3 and q4 are equivalent and can be merged together.

So, Our minimal DFA is-

 

 

Also Read- Construction of DFA

 

To gain better understanding about Minimization of DFA,

Watch this Video Lecture

 

Next Article- Converting DFA to Regular Expression

 

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

Watch video lectures by visiting our YouTube channel LearnVidFun.

DFA to Regular Expression | Examples

DFA to Regular Expression-

 

The two popular methods for converting a DFA to its regular expression are-

 

 

  1. Arden’s Method
  2. State Elimination Method

 

In this article, we will discuss State Elimination Method.

 

State Elimination Method-

 

This method involves the following steps in finding the regular expression for any given DFA-

 

Step-01:

 

Thumb Rule

The initial state of the DFA must not have any incoming edge.

 

  • If there exists any incoming edge to the initial state, then create a new initial state having no incoming edge to it.

 

Example-

 

 

Step-02:

 

Thumb Rule

There must exist only one final state in the DFA.

 

  • If there exists multiple final states in the DFA, then convert all the final states into non-final states and create a new single final state.

 

Example-

 

 

Step-03:

 

Thumb Rule

The final state of the DFA must not have any outgoing edge.

 

  • If there exists any outgoing edge from the final state, then create a new final state having no outgoing edge from it.

 

Example-

 

 

Step-04:

 

  • Eliminate all the intermediate states one by one.
  • These states may be eliminated in any order.

 

In the end,

  • Only an initial state going to the final state will be left.
  • The cost of this transition is the required regular expression.

 

NOTE

The state elimination method can be applied to any finite automata.

(NFA, ∈-NFA, DFA etc)

 

Also Read- Construction of DFA

 

PRACTICE PROBLEMS BASED ON CONVERTING DFA TO REGULAR EXPRESSION-

 

Problem-01:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • Initial state A has an incoming edge.
  • So, we create a new initial state qi.

 

The resulting DFA is-

 

 

Step-02:

 

  • Final state B has an outgoing edge.
  • So, we create a new final state qf.

 

The resulting DFA is-

 

 

Step-03:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state A.

  • There is a path going from state qi to state B via state A.
  • So, after eliminating state A, we put a direct path from state qi to state B having cost ∈.0 = 0
  • There is a loop on state B using state A.
  • So, after eliminating state A, we put a direct loop on state B having cost 1.0 = 10.

 

Eliminating state A, we get-

 

 

Step-04:

 

Now, let us eliminate state B.

  • There is a path going from state qi to state qf via state B.
  • So, after eliminating state B, we put a direct path from state qi to state qf having cost 0.(10)*.∈ = 0(10)*

 

Eliminating state B, we get-

 

 

From here,

 

Regular Expression = 0(10)*

 

NOTE-

 

In the above question,

  • If we first eliminate state B and then state A, then regular expression would be = (01)*0.
  • This is also the same and correct.

 

Problem-02:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • There exist multiple final states.
  • So, we convert them into a single final state.

 

The resulting DFA is-

 

 

Step-02:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state q4.

  • There is a path going from state q2 to state qf via state q4.
  • So, after eliminating state q4 , we put a direct path from state q2 to state qf having cost b.∈ = b.

 

 

Step-03:

 

Now, let us eliminate state q3.

  • There is a path going from state q2 to state qf via state q3.
  • So, after eliminating state q3 , we put a direct path from state q2 to state qf having cost c.∈ = c.

 

 

Step-04:

 

Now, let us eliminate state q5.

  • There is a path going from state q2 to state qf via state q5.
  • So, after eliminating state q5 , we put a direct path from state q2 to state qf having cost d.∈ = d.

 

 

Step-05:

 

Now, let us eliminate state q2.

  • There is a path going from state q1 to state qf via state q2.
  • So, after eliminating state q2 , we put a direct path from state q1 to state qf having cost a.(b+c+d).

 

 

From here,

 

Regular Expression = a(b+c+d)

 

Problem-03:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • Initial state q1 has an incoming edge.
  • So, we create a new initial state qi.

 

The resulting DFA is-

 

 

Step-02:

 

  • Final state q2 has an outgoing edge.
  • So, we create a new final state qf.

 

The resulting DFA is-

 

 

Step-03:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state q1.

  • There is a path going from state qi to state q2 via state q1 .
  • So, after eliminating state q1, we put a direct path from state qi to state q2 having cost ∈.c*.a = c*a
  • There is a loop on state q2 using state q1 .
  • So, after eliminating state q1 , we put a direct loop on state q2 having cost b.c*.a = bc*a

 

Eliminating state q1, we get-

 

 

Step-04:

 

Now, let us eliminate state q2.

  • There is a path going from state qi to state qf via state q2 .
  • So, after eliminating state q2, we put a direct path from state qi to state qf having cost c*a(d+bc*a)*∈ = c*a(d+bc*a)*

 

Eliminating state q2, we get-

 

 

From here,

 

Regular Expression = c*a(d+bc*a)*

 

Problem-04:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • State D is a dead state as it does not reach to any final state.
  • So, we eliminate state D and its associated edges.

 

The resulting DFA is-

 

 

Step-02:

 

  • Initial state A has an incoming edge (self loop).
  • So, we create a new initial state qi.

 

The resulting DFA is-

 

 

Step-03:

 

  • There exist multiple final states.
  • So, we convert them into a single final state.

 

The resulting DFA is-

 

 

Step-04:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state C.

  • There is a path going from state B to state qf via state C.
  • So, after eliminating state C, we put a direct path from state B to state qf having cost b.b*.∈ = bb*

 

Eliminating state C, we get-

 

 

Step-05:

 

Now, let us eliminate state B.

  • There is a path going from state A to state qf via state B.
  • So, after eliminating state B, we put a direct path from state A to state qf having cost a.a*.(bb*+∈) = aa*(bb*+∈)

 

Eliminating state B, we get-

 

 

Step-06:

 

Now, let us eliminate state A.

  • There is a path going from state qi to state qf via state A.
  • So, after eliminating state A, we put a direct path from state qi to state qf having cost ∈.b*.(aa*(bb*+∈)+∈) = b*(aa*(bb*+∈)+∈)

 

Eliminating state A, we get-

 

 

From here,

 

Regular Expression = b*(aa*(bb*+∈)+∈)

 

We know, bb* + ∈ = b*

So, we can also write-

 

Regular Expression = b*(aa*b*+∈)

 

Problem-05:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • Since initial state A has an incoming edge, so we create a new initial state qi.
  • Since final state A has an outgoing edge, so we create a new final state qf.

 

The resulting DFA is-

 

 

Step-02:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state B.

  • There is a path going from state C to state A via state B.
  • So, after eliminating state B, we put a direct path from state C to state A having cost b.b = bb.
  • There is a loop on state A using state B.
  • So, after eliminating state B, we put a direct loop on state A having cost a.b = ab.

 

Eliminating state B, we get-

 

 

Step-03:

 

Now, let us eliminate state C.

  • There is a loop on state A using state C.
  • So, after eliminating state C, we put a direct loop on state A having cost b.(a+bb) = b(a+bb)

 

Eliminating state C, we get-

 

 

Step-04:

 

Now, let us eliminate state A.

  • There is a path going from state qi to state qf via state A.
  • So, after eliminating state A, we put a direct path from state qi to state qf having cost ∈.(ab + b(a+bb))*∈ = (ab + b(a+bb))*

 

Eliminating state A, we get-

 

 

From here,

 

Regular Expression = (ab + b(a+bb))*

 

Problem-06:

 

Find regular expression for the following DFA-

 

 

Solution-

 

  • State B is a dead state as it does not reach to the final state.
  • So, we eliminate state B and its associated edges.

 

The resulting DFA is-

 

 

From here,

 

Regular Expression = a

 

Problem-07:

 

Find regular expression for the following DFA-

 

 

Solution-

 

Step-01:

 

  • There exist multiple final states.
  • So, we create a new single final state.

 

The resulting DFA is-

 

 

Step-02:

 

Now, we start eliminating the intermediate states.

 

First, let us eliminate state B.

  • There is a path going from state A to state qf via state B.
  • So, after eliminating state B, we put a direct path from state A to state qf having cost a.a*.∈ = aa*.

 

Eliminating state B, we get-

 

 

Step-03:

 

Now, let us eliminate state C.

  • There is a path going from state A to state qf via state C.
  • So, after eliminating state C, we put a direct path from state A to state qf having cost b.a*.∈ = ba*.

 

Eliminating state C, we get-

 

 

From here,

 

Regular Expression = aa* + ba*

 

Also Read- Minimization of DFA

 

To gain better understanding about Converting DFA to Regular Expression,

Watch this Video Lecture

 

Next Article- Parse Tree and Derivations

 

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

Watch video lectures by visiting our YouTube channel LearnVidFun.

Left Recursion | Left Recursion Elimination

Recursion-

 

Recursion can be classified into following three types-

 

 

  1. Left Recursion
  2. Right Recursion
  3. General Recursion

 

1. Left Recursion-

 

  • A production of grammar is said to have left recursion if the leftmost variable of its RHS is same as variable of its LHS.
  • A grammar containing a production having left recursion is called as Left Recursive Grammar.

 

Example-

 

S → Sa / 

(Left Recursive Grammar)

 

  • Left recursion is considered to be a problematic situation for Top down parsers.
  • Therefore, left recursion has to be eliminated from the grammar.

 

Elimination of Left Recursion

 

Left recursion is eliminated by converting the grammar into a right recursive grammar.

 

If we have the left-recursive pair of productions-

Aα / β

(Left Recursive Grammar)

where β does not begin with an A.

 

Then, we can eliminate left recursion by replacing the pair of productions with-

→ βA’

A’ → αA’ / ∈

(Right Recursive Grammar)

 

This right recursive grammar functions same as left recursive grammar.

 

2. Right Recursion-

 

  • A production of grammar is said to have right recursion if the rightmost variable of its RHS is same as variable of its LHS.
  • A grammar containing a production having right recursion is called as Right Recursive Grammar.

 

Example-

 

S → aS / 

(Right Recursive Grammar)

 

  • Right recursion does not create any problem for the Top down parsers.
  • Therefore, there is no need of eliminating right recursion from the grammar.

 

Also Read- Types of Recursive Grammar

 

3. General Recursion-

 

  • The recursion which is neither left recursion nor right recursion is called as general recursion.

 

Example-

 

S → aSb / 

 

PRACTICE PROBLEMS BASED ON LEFT RECURSION ELIMINATION-

 

Problem-01:

 

Consider the following grammar and eliminate left recursion-

A → ABd / Aa / a

B → Be / b

 

Solution-

 

The grammar after eliminating left recursion is-

A → aA’

A’ → BdA’ / aA’ / 

B → bB’

B’ → eB’ / 

 

Problem-02:

 

Consider the following grammar and eliminate left recursion-

E → E + E / E x E / a

 

Solution-

 

The grammar after eliminating left recursion is-

E → aA

A → +EA / xEA / 

 

Problem-03:

 

Consider the following grammar and eliminate left recursion-

E → E + T / T

T → T x F / F

F → id

 

Solution-

 

The grammar after eliminating left recursion is-

E → TE’

E’ → +TE’ / 

T → FT’

T’ → xFT’ / 

F → id

 

Problem-04:

 

Consider the following grammar and eliminate left recursion-

S → (L) / a

L → L , S / S

Solution-

 

The grammar after eliminating left recursion is-

S → (L) / a

L → SL’

L’ → ,SL’ / 

 

Problem-05:

 

Consider the following grammar and eliminate left recursion-

S → S0S1S / 01

 

Solution-

 

The grammar after eliminating left recursion is-

S → 01A

A → 0S1SA / 

 

Problem-06:

 

Consider the following grammar and eliminate left recursion-

S → A

A → Ad / Ae / aB / ac

B → bBc / f

 

Solution-

 

The grammar after eliminating left recursion is-

S → A

A → aBA’ / acA’

A’ → dA’ / eA’ / 

B → bBc / f

 

Problem-07:

 

Consider the following grammar and eliminate left recursion-

A → AAα / β

Solution-

 

The grammar after eliminating left recursion is-

A → βA’

A’ → AαA’ / 

 

Problem-08:

 

Consider the following grammar and eliminate left recursion-

A → Ba / Aa / c

B → Bb / Ab / d

 

Solution-

 

This is a case of indirect left recursion.

 

Step-01:

 

First let us eliminate left recursion from A → Ba / Aa / c

 

Eliminating left recursion from here, we get-

A → BaA’ / cA’

A’ → aA’ / 

 

Now, given grammar becomes-

A → BaA’ / cA’

A’ → aA’ / 

B → Bb / Ab / d

 

Step-02:

 

Substituting the productions of A in B → Ab, we get the following grammar-

A → BaA’ / cA’

A’ → aA’ / 

B → Bb / BaA’b / cA’b / d

 

Step-03:

 

Now, eliminating left recursion from the productions of B, we get the following grammar-

A → BaA’ / cA’

A’ → aA’ / 

B → cA’bB’ / dB’

B’ → bB’ / aA’bB’ / 

 

This is the final grammar after eliminating left recursion.

 

Problem-09:

 

Consider the following grammar and eliminate left recursion-

X → XSb / Sa / b

S → Sb / Xa / a

Solution-

 

This is a case of indirect left recursion.

 

Step-01:

 

First let us eliminate left recursion from X → XSb / Sa / b

 

Eliminating left recursion from here, we get-

X → SaX’ / bX’

X’ → SbX’ / 

 

Now, given grammar becomes-

X → SaX’ / bX’

X’ → SbX’ / 

S → Sb / Xa / a

 

Step-02:

 

Substituting the productions of X in S → Xa, we get the following grammar-

X → SaX’ / bX’

X’ → SbX’ / 

S → Sb / SaX’a / bX’a / a

 

Step-03:

 

Now, eliminating left recursion from the productions of S, we get the following grammar-

X → SaX’ / bX’

X’ → SbX’ / 

S → bX’aS’ / aS’

S’ → bS’ / aX’aS’ / 

 

This is the final grammar after eliminating left recursion.

 

Problem-10:

 

Consider the following grammar and eliminate left recursion-

S → Aa / b

A → Ac / Sd / 

Solution-

 

This is a case of indirect left recursion.

 

Step-01:

 

First let us eliminate left recursion from S → Aa / b

This is already free from left recursion.

 

Step-02:

 

Substituting the productions of S in A → Sd, we get the following grammar-

S → Aa / b

A → Ac / Aad / bd / 

 

Step-03:

 

Now, eliminating left recursion from the productions of A, we get the following grammar-

S → Aa / b

A → bdA’ / A’

A’ → cA’ / adA’ / 

 

This is the final grammar after eliminating left recursion.

 

Also Read- Left Factoring

 

To gain better understanding about Left Recursion Elimination,

Watch this Video Lecture

 

Next Article- Types of Grammars

 

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

Watch video lectures by visiting our YouTube channel LearnVidFun.

Parse Tree | Derivations | Automata

Parse Tree-

 

  • The process of deriving a string is called as derivation.
  • The geometrical representation of a derivation is called as a parse tree or derivation tree.

 

 

1. Leftmost Derivation-

 

  • The process of deriving a string by expanding the leftmost non-terminal at each step is called as leftmost derivation.
  • The geometrical representation of leftmost derivation is called as a leftmost derivation tree.

 

Example-

 

Consider the following grammar-

S → aB / bA

S → aS / bAA / a

B → bS / aBB / b

(Unambiguous Grammar)

 

Let us consider a string w = aaabbabbba

Now, let us derive the string w using leftmost derivation.

 

Leftmost Derivation-

 

S   → aB

→  aaBB                   (Using B → aBB)

→ aaaBBB                (Using B → aBB)

→ aaabBB                (Using B → b)

→ aaabbB                (Using B → b)

→ aaabbaBB            (Using B → aBB)

→ aaabbabB            (Using B → b)

→ aaabbabbS          (Using B → bS)

→ aaabbabbbA        (Using S → bA)

→ aaabbabbba         (Using A → a)

 

 

2. Rightmost Derivation-

 

  • The process of deriving a string by expanding the rightmost non-terminal at each step is called as rightmost derivation.
  • The geometrical representation of rightmost derivation is called as a rightmost derivation tree.

 

Example-

 

Consider the following grammar-

S → aB / bA

S → aS / bAA / a

B → bS / aBB / b

(Unambiguous Grammar)

 

Let us consider a string w = aaabbabbba

Now, let us derive the string w using rightmost derivation.

 

Rightmost Derivation-

 

S   → aB

→  aaBB                    (Using B → aBB)

→ aaBaBB                 (Using B → aBB)

→ aaBaBbS               (Using B → bS)

→ aaBaBbbA             (Using S → bA)

→ aaBaBbba              (Using A → a)

→ aaBabbba              (Using B → b)

→ aaaBBabbba          (Using B → aBB)

→ aaaBbabbba          (Using B → b)

→ aaabbabbba           (Using B → b)

 

 

NOTES

  • For unambiguous grammars, Leftmost derivation and Rightmost derivation represents the same parse tree.
  • For ambiguous grammars, Leftmost derivation and Rightmost derivation represents different parse trees.

 

Here,

  • The given grammar was unambiguous.
  • That is why, leftmost derivation and rightmost derivation represents the same parse tree.

 

Leftmost Derivation Tree = Rightmost Derivation Tree

 

Also Read- Ambiguous Grammar

 

Properties Of Parse Tree-

 

  • Root node of a parse tree is the start symbol of the grammar.
  • Each leaf node of a parse tree represents a terminal symbol.
  • Each interior node of a parse tree represents a non-terminal symbol.
  • Parse tree is independent of the order in which the productions are used during derivations.

 

Yield Of Parse Tree-

 

  • Concatenating the leaves of a parse tree from the left produces a string of terminals.
  • This string of terminals is called as yield of a parse tree.

 

PRACTICE PROBLEMS BASED ON DERIVATIONS AND PARSE TREE-

 

Problem-01:

 

Consider the grammar-

S → bB / aA

A → b / bS / aAA

B → a / aS / bBB

 

For the string w = bbaababa, find-

  1. Leftmost derivation
  2. Rightmost derivation
  3. Parse Tree

 

Solution-

 

1. Leftmost Derivation-

 

S   → bB

→ bbBB              (Using B → bBB)

→ bbaB              (Using B → a)

→ bbaaS            (Using B → aS)

→ bbaabB          (Using S → bB)

→ bbaabaS        (Using B → aS)

→ bbaababB      (Using S → bB)

→ bbaababa       (Using B → a)

 

2. Rightmost Derivation-

 

S   → bB

→ bbBB                (Using B → bBB)

→ bbBaS              (Using B → aS)

→ bbBabB            (Using S → bB)

→ bbBabaS          (Using B → aS)

→ bbBababB        (Using S → bB)

→ bbBababa        (Using B → a)

→ bbaababa         (Using B → a)

 

3. Parse Tree-

 

 

  • Whether we consider the leftmost derivation or rightmost derivation, we get the above parse tree.
  • The reason is given grammar is unambiguous.

 

Problem-02:

 

Consider the grammar-

S → A1B

A → 0A / ∈

B → 0B / 1B / ∈

 

For the string w = 00101, find-

  1. Leftmost derivation
  2. Rightmost derivation
  3. Parse Tree

 

Solution-

 

1. Leftmost Derivation-

 

S   → A1B

→ 0A1B              (Using A → 0A)

→ 00A1B            (Using A → 0A)

→ 001B              (Using A → ∈)

→ 0010B            (Using B → 0B)

→ 00101B          (Using B → 1B)

→ 00101             (Using B → ∈)

 

2. Rightmost Derivation-

 

S   → A1B

→ A10B                (Using B → 0B)

→ A101B              (Using B → 1B)

A101                (Using B → ∈)

→ 0A101              (Using A → 0A)

→ 00A101            (Using A → 0A)

→ 00101               (Using A → ∈)

 

3. Parse Tree-

 

 

  • Whether we consider the leftmost derivation or rightmost derivation, we get the above parse tree.
  • The reason is given grammar is unambiguous.

 

To gain better understanding about Derivations and Parse Tree,

Watch this Video Lecture

 

Next Article- Elimination of Left Recursion

 

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

Watch video lectures by visiting our YouTube channel LearnVidFun.