All TOC E-Lecture Notes

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 57

Construction Of DFA-

Type-01 Problems-
In Type-01 problems, we will discuss the construction of DFA for languages consisting of strings ending with a
particular substring.

Steps To Construct DFA-


Following steps are followed to construct a DFA for Type-01 problems-

Step-01:
Determine the minimum number of states required in the DFA.

Draw those states.


Use the following rule to determine the minimum number of states-

RULE

Calculate the length of substring.

All strings ending with ‘n’ length substring will always require minimum (n+1) states in the DFA.

Step-02:
 Decide the strings for which DFA will be constructed.
Step-03:
 Construct a DFA for the strings decided in Step-02.
Remember the following rule while constructing the DFA-

RULE
While constructing a DFA,

 Always prefer to use the existing path.


 Create a new path only when there exists no path to go with.
Step-04:
 Send all the left possible combinations to the starting state.
 Do not send the left possible combinations over the dead state.

Example-01:
Draw a DFA for the language accepting strings ending with ’01’ over input alphabets ∑ = {0, 1}

Solution-
Regular expression for the given language = (0 + 1)*01

Step-01:
All strings of the language ends with substring “01”.

 So, length of substring = 2.


Thus, Minimum number of states required in the DFA = 2 + 1 = 3.

It suggests that minimized DFA will have 3 states.

Step-02:
We will construct DFA for the following strings-
 01
 001
 0101
Step-03:
The required Final DFA is-

Example-02:
Draw a DFA for the language accepting strings ending with ‘abb’ over input alphabets ∑ = {a, b}

Solution-
Regular expression for the given language = (a + b)*abb

Step-01:
 All strings of the language ends with substring “abb”.
 So, length of substring = 3.
Thus, Minimum number of states required in the DFA = 3 + 1 = 4.

It suggests that minimized DFA will have 4 states.

Step-02:
We will construct DFA for the following strings-

 abb
 aabb
 ababb
 abbabb
Step-03:
 The required Final DFA is-
Type-02 Problems-

In Type-02 problems, we will discuss the construction of DFA for languages consisting of strings starting with a
particular substring.

Steps To Construct DFA-

Following steps are followed to construct a DFA for Type-02 problems-

Step-01:

Determine the minimum number of states required in the DFA.

 Draw those states.


Use the following rule to determine the minimum number of states-

RULE
Calculate the length of substring.

All strings starting with ‘n’ length substring will always require minimum (n+2) states in the DFA.

Step-02:

 Decide the strings for which DFA will be constructed.


 The method for deciding the strings has been discussed in this Video.
Step-03:
 Construct a DFA for the strings decided in Step-02.
Remember the following rule while constructing the DFA-

RULE
While constructing a DFA,

 Always prefer to use the existing path.


Create a new path only when there exists no path to go with.

Step-04:
 Send all the left possible combinations to the dead state.
 Do not send the left possible combinations over the starting state.

Example-01:
Draw a DFA for the language accepting strings starting with ‘ab’ over input alphabets ∑ = {a, b}

Solution-

Regular expression for the given language = ab(a + b)*

Step-01:

All strings of the language starts with substring “ab”.

 So, length of substring = 2.


Thus, Minimum number of states required in the DFA = 2 + 2 = 4.

It suggests that minimized DFA will have 4 states.

Step-02:

We will construct DFA for the following strings-

 ab
 aba
 abab
Step-03:

The required DFA is-

Example-02:
Draw a DFA for the language accepting strings starting with ‘a’ over input alphabets ∑ = {a, b}

Solution-

Regular expression for the given language = a(a + b)*

Step-01:

All strings of the language starts with substring “a”.

 So, length of substring = 1.


Thus, Minimum number of states required in the DFA = 1 + 2 = 3.

It suggests that minimized DFA will have 3 states.

Step-02:

We will construct DFA for the following strings-

 a
 aa
Step-03:
The required DFA is-

Example-03(Type02)

Draw a DFA for the language accepting strings starting with ‘101’ over input alphabets ∑ = {0, 1}

Solution-

Regular expression for the given language = 101(0 + 1)*

Step-01:

All strings of the language starts with substring “101”.

 So, length of substring = 3.


Thus, Minimum number of states required in the DFA = 3 + 2 = 5.

It suggests that minimized DFA will have 5 states.

Step-02:

We will construct DFA for the following strings-

 101
 1011
 10110
 101101
Step-03:

The required DFA is-


Symbol:

Symbols are an entity or individual objects.A symbol (often also called a character) is the smallest building block,
which can be any alphabet, letter, or picture.
a,b,c,………0,1,2…………….

Alphabets (Σ):

Alphabets are a set of symbols, which are always finite.

String:

A string is a finite sequence of symbols from some alphabet. A string is generally denoted as w and the length of
a string is denoted as |w|.
Empty string is the string with zero occurrence of symbols, represented as ε.
Number of Strings (of length 2)
that can be generated over the alphabet {a, b}:
- -
a a
a b
b a
b b

Length of String |w| = 2


Number of Strings = 4

Conclusion:
For alphabet {a, b} with length n, number of
strings can be generated = 2n.
Note: If the number of symbols in the alphabet Σ is represented by |Σ|, then a number of strings of length n,
possible over Σ is |Σ|n.

Regular operations.

There are three operations which are performed on languages namely: union, concatenation and kleen closure.

Let A and B be two languages over a similar alphabet:


1. The union of A and B is defined as:
A ∪ B = {w : w ∈ A or w ∈ B}
2. The concatenation of the two is defined as:
AB = {ww′ : w ∈ A and w′ ∈ B} where AB is the set of all strings obtained by taking an arbitrary string w in A and an
arbitrary string w′ in B then putting them together such that the former is to the left of the latter.
3. The kleen closure of A is defined as:
A* = {u1u2 ... uk : k ≥ 0 and ui ∈ A for all i = 1, 2, ..., k}
Where A* is obtained by taking an infinite number of strings in A and putting them together.
Note that k cannot be zero, in this case it will correspond to an empty string ϵ and therefore ϵ ∈ A*.
An Example:
Let A = {0, 01} and B = {1, 10}.
union: A ∪ B= {0, 01, 1, 10}
concatenation: AB = {01, 010, 011, 0110}
kleen closure: A* = {ϵ, 0, 01, 00, 001, 010, 0101, 000, 0001, 00101, ...}
An example:
If Σ = {0, 1}, Σ* is the set of all binary strings including an empty string.
Alternative kleen closure definition for a language A,
A0 = {ϵ}.
For k ≥ 1, Ak = AAk-1 where Ak is a concatenation of the languages A and Ak-1.
We define A* as follows:
A* = ⋃k=0∞Ak

A string is a finite set sequence of symbols chosen from some alphabets.

For example,

 00011001 is a string from binary alphabet Σ={0,1}


 aabbcabcd is a string from alphabet Σ={a,b,c,d}

The different operations performed on strings are explained below −

 Concatenation.
 Substring.
 Kleen star operation.
 Reversal.

Concatenation

Concatenation is nothing but combining the two strings one after another.

Example

Let’s consider two strings −


X= Tutorials
Y= Point
The concatenation (X, Y) of two strings is −
X.Y = TutorialsPoint
Note − Concatenation of empty string with other string gives string itself.
For example, X. ε = ε.X = X
Substring
If ‘w’ is a string then ‘v’ is substring of ‘w’ if there exists string x and y such that w=xvy
‘x’ is called ‘prefix’ and y is called the suffix of w.

Example

Let’s consider w=’Theory’ is defined with prefix, x=’The’ and suffix , y=’ry’.
The substring is v=’o’, because, w=xvy and Theory= Thevry
Therefore, v=o
Kleen star operation
Let ‘w’ be a string. w’ is a set of strings obtained by applying any number of concatenations of w with itself,
including empty string.
Example
a*= { ε,a,aa,aaa,………}
Reversal operation
If ‘w’ is a string, then wR is the reversal of the string in backwards.
Rules
The rules for the reversal operation are as follows −
 x=(xR)R
 (xz)R= zR.xR

Example

A string x is defined as x= tutorial then (xR)R is tutorial. This is because,

X= tutorial

(x)R= lairotut

(xR)R= tutorial
Moore and Mealy machines

Moore and Mealy machines are called Finite Automata with output. This is different from
a DFA or NFA (Finite Automata without output) where there is no output (input is either accepted or rejected).

Finite State Machines with Output

Moore Machine Mealy Machine

Moore Machine

Moore machine is a finite state machine in which the next state is decided by the current state and current input
symbol. The output symbol at a given time depends only on the present state of the machine.

Moore machine can be described by 6 tuples (Q, q0, ∑, O, δ, λ) where,

1. Q: finite set of states


2. q0: initial state of machine
3. ∑: finite set of input symbols
4. O: output alphabet
5. δ: transition function where Q × ∑ → Q
6. λ: output function where Q → O

Example 1:

The state diagram for Moore Machine is

Transition table for Moore Machine is:


In the above Moore machine, the output is represented with each input state separated by /. The output length for a
Moore machine is greater than input by 1.

Input: 010

Transition: δ (q0,0) => δ(q1,1) => δ(q1,0) => q2

Output: 1110(1 for q0, 1 for q1, again 1 for q1, 0 for q2)

Example 2:

Design a Moore machine to generate 1's complement of a given binary number.

Solution: To generate 1's complement of a given binary number the simple logic is that if the input is 0 then the
output will be 1 and if the input is 1 then the output will be 0. That means there are three states. One state is start
state. The second state is for taking 0's as input and produces output as 1. The third state is for taking 1's as input and
producing output as 0.

Hence the Moore machine will be,

For instance, take one binary number 1011 then

Input 1 0 1 1
State q0 q2 q1 q2 q2

Output 0 0 1 0 0

Thus we get 00100 as 1's complement of 1011, we can neglect the initial 0 and the output which we get is 0100
which is 1's complement of 1011. The transaction table is as follows:

Thus Moore machine M = (Q, q0, ∑, O, δ, λ); where Q = {q0, q1, q2}, ∑ = {0, 1}, O = {0, 1}. the transition table
shows the δ and λ functions.

Example 3:

Design a Moore machine for a binary input sequence such that if it has a substring 101, the machine output A, if the
input has substring 110, it outputs B otherwise it outputs C.

Solution: For designing such a machine, we will check two conditions, and those are 101 and 110. If we get 101, the
output will be A, and if we recognize 110, the output will be B. For other strings, the output will be C.

The partial diagram will be:

Now we will insert the possibilities of 0's and 1's for each state. Thus the Moore machine becomes:
Example 4:

Construct a Moore machine that determines whether an input string contains an even or odd number of 1's. The
machine should give 1 as output if an even number of 1's are in the string and 0 otherwise.

Solution:

The Moore machine will be:

This is the required Moore machine. In this machine, state q1 accepts an odd number of 1's and state q0 accepts even
number of 1's. There is no restriction on a number of zeros. Hence for 0 input, self-loop can be applied on both the
states.

Mealy Machine

In a Mealy machine the output symbol depends upon the present input symbol and present state of the machine.

In the Mealy machine, the output is represented with each input symbol and each state is separated by /.

The Mealy machine can be described by six tuples (Q, q0, Σ, O, δ, λ')

Where,

 Q: Finite set of states.


 q0: Initial state of machine.
 Σ: Finite set of input alphabet.
 O: Output alphabet.
 δ: Transition function where Q × Σ → Q.
 λ': Output function where Q × Σ → O.

In the Mealy machine, the output is represented with each input symbol and each state is separated by /.

The length of output for a mealy machine is equal to the length of input.

Example 1:

Design a Mealy machine for a binary input sequence such that if it has a substring 101, the machine output A, if the
input has substring 110, it outputs B otherwise it outputs C.

Solution: For designing such a machine, we will check two conditions, and those are 101 and 110. If we get 101, the
output will be A. If we recognize 110, the output will be B. For other strings the output will be C.

The partial diagram will be:

Now we will insert the possibilities of 0's and 1's for each state. Thus the Mealy machine becomes:

Differences b/w Moore and Mealy machines

Moore Machine Mealy Machine


The output depends only on the present state. The output depends both on the present state and the present input.
Moore Machine Mealy Machine
Generally, a Moore machine has more states. It generally has fewer states than Moore's machine.

Less hardware requirement for circuit The hardware requirement is comparatively more for circuit
implementation. implementation.

Moore machines react slower to input. They react faster to input.

Synchronous output and state generation. The output generation is asynchronous.

Output is placed on state. Output is placed on transition.

If input changes, output does not change If input changes, output also changes.

These are generally easy to design. Comparatively difficult to design.

Similarities b/w Moore and Mealy machines


1. Both Moore and Mealy machines do not have any final state.

2. They both produce output in the form of string.

https://www.codingninjas.com/studio/library/moore-and-mealy-machine

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

DFA minimization stands for converting a given DFA to its equivalent DFA with minimum number of
states. DFA minimization is also called as Optimization of DFA and uses partitioning algorithm.

Minimization of DFA means reducing the number of states from given FA. Thus, we get the FSM(finite state
machine) with redundant states after minimizing the FSM.

How To Minimize DFA?

The two popular methods for minimizing a DFA are-

Minimization of DFA(Methods)

Equivalence Theorem Myhill Narode Theorem(Table Filling Method)


Minimization of DFA Using Equivalence Theorem-
There is also another method called Partitioning Method or Equivalence Method
for the minimization of DFA

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

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

a b

→q0 q1 q2

q1 q1 q3

q2 q1 q2

q3 q1 *q4

*q4 q1 q2

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-

https://www.gatevidyalay.com/minimization-of-dfa-minimize-dfa-example/

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

a b

→q0 *q1 q0

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


OR

We have to follow the various steps to minimize the DFA. These are as follows:

Step 1: Remove all the states that are unreachable from the initial state via any set of the transition of DFA.

Step 2: Draw the transition table for all pair of states.

Step 3: Now split the transition table into two tables T1 and T2. T1 contains all final states, and T2 contains non-
final states.

Step 4: Find similar rows from T1 such that:

1. δ (q, a) = p
2. δ (r, a) = p

That means, find the two states which have the same value of a and b and remove one of them.

Step 5: Repeat step 3 until we find no similar rows available in the transition table T1.

Step 6: Repeat step 3 and step 4 for table T2 also.

Step 7: Now combine the reduced T1 and T2 tables. The combined transition table is the transition table of
minimized DFA.

Example:
Solution:

Step 1: In the given DFA, q2 and q4 are the unreachable states so remove them.

State 0 1

→q0 q1 q3

q1 q0 q3

*q3 q5 q5

*q5 q5 q5

Step 2: Draw the transition table for the rest of the states.

Step 3: Now divide rows of transition table into two sets as:

1. One set contains those rows, which start from non-final states:

State 0 1

q0 q1 q3

q1 q0 q3

2. Another set contains those rows, which starts from final states.

State 0 1

q3 q5 q5

q5 q5 q5

Step 4: Set 1 has no similar rows so set 1 will be the same.


Step 5: In set 2, row 1 and row 2 are similar since q3 and q5 transit to the same state on 0 and 1. So skip q5 and then
replace q5 by q3 in the rest.

State 0 1

q3 q3 q3

Step 6: Now combine set 1 and set 2 as:

State 0 1

→q0 q1 q3

q1 q0 q3

*q3 q3 q3

Now it is the transition table of minimized DFA.


Myhill nerode theorem
The Myhill Nerode theorem is a fundamental result coming down to the theory of
languages. This theory was proven by John Myhill and Anil Nerode in 1958. his theorem
is used for proving whether a language is regular or not.

It can also be used to minimize states in Deterministic Finite Automata (DFA).

Myhill Nerode Theorem gives sufficient and necessary conditions for a language to be
regular.

Minimization of DFA is Required to obtain the minimal and equivalent version of


any DFA which consists of minimum number of states possible. Myhill-Nerode
theorem can be used to convert a DFA to its equivalent DFA with minimum no
of states. This method of minimization is also called Table filling method.
Steps for the Minimization of DFA :
1. Create the pairs of all the states involved in the given DFA.
2. Mark all the pairs (Q a,Qb) such a that Qa is Final state and Qb is Non-Final
State.
3. If there is any unmarked pair (Qa,Qb) such a that δ(Qa,x) and δ(Q b,x) is
marked, then mark (Qa,Qb). Here x is a input symbol. Repeat this step until
no more marking can be made.
4. Combine all the unmarked pairs and make them a single state in the
minimized DFA.
Example
Consider the following DFA,

Following is the transition table for the above DFA


Minimizing the above DFA using Myhill-Nerode Theorem :
Step-1: Create the pairs of all the states involved in DFA.

Step-2: Mark all the pairs (Qa,Qb) such a that Qa is Final state and Qb is Non-
Final State.
Step-3: If there is any unmarked pair (Qa,Qb) such a that δ(Qa,x) and δ(Qb,x)
is marked, then mark (Qa,Qb). Here x is a input symbol. Repeat this step until
no more marking can be made.
 Check for the unmarked pair Q2,Q1
 Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair
Q4,Q3 is marked and no it is not marked.
 Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair
Q4,Q3 is marked and no it is not marked.
 Hence we cannot mark the pair Q2,Q1.
 Check for the unmarked pair Q3,Q0
 Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and no it is not marked.
 Check when x=1 : δ(Q3,1) = Q5 and δ(Q0,1) = Q2, check if the pair
Q5,Q2 is marked and no it is not marked.
 Hence we cannot mark the pair Q3,Q0.
 Check for the unmarked pair Q4,Q0
 Check when x=0 : δ(Q4,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and no it is not marked.
 Check when x=1 : δ(Q4,1) = Q5 and δ(Q0,1) = Q2, check if the pair
Q5,Q2 is marked and no it is not marked.
 Hence we cannot mark the pair Q4,Q0.
 Check for the unmarked pair Q4,Q3
 Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of
state Q5,Q5 don’t exists.
 Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of
state Q5,Q5 don’t exists.
 Hence we cannot mark the pair Q4,Q3.
 Check for the unmarked pair Q5,Q1
 Check when x=0 : δ(Q5,0) = Q5 and δ(Q1,0) = Q3, check if the pair
Q5,Q3 is marked and yes it is marked.
 Hence we can mark the pair Q5,Q1.

 Check for the unmarked pair Q5,Q2


 Check when x=0 : δ(Q5,0) = Q5 and δ(Q2,0) = Q4, check if the pair
Q5,Q4 is marked and yes it is marked.
 Hence we can mark the pair Q5,Q2.

 We have checked for all the unmarked pairs but don’t need to stop here we
need to continue this process until no more markings can be made.
 Check for the unmarked pair Q2,Q1
 Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair
Q4,Q3 is marked and no it is not marked.
 Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair
Q4,Q3 is marked and no it is not marked.
 Hence we cannot mark the pair Q2,Q1.
 Check for the unmarked pair Q3,Q0
 Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and yes it is marked.
 Hence we can mark the pair Q3,Q0.

 Check for the unmarked pair Q4,Q0


 Check when x=0 : δ(Q4,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and yes it is marked.
 Hence we cannot mark the pair Q4,Q0.

 Check for the unmarked pair Q4,Q3


 Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of
state Q5,Q5 don’t exists.
 Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of
state Q5,Q5 don’t exists.
 Hence we cannot mark the pair Q4,Q3.
 Now even though we repeat the procedure we cannot mark the pairs
Q2,Q1(since Q4,Q3 is not marked) and Q4,Q3(since Q5,Q5 such pair of
states does not exists.). Hence we stop here.
Step-4: Combine all the unmarked pairs and make them as a single state in the
minimized DFA.
 The unmarked Pairs are Q2,Q1 and Q4,Q3 hence we combine them.
Following is the Minimized DFA with Q1Q2 and Q3Q4 as the combined states.

 Q0 remains as our starting state.


 Q1 and Q2 were our final states so even we combine them they will remain
as the combined final state.
 Q5 is the another final state we have.
 If we check the original Transition Table
 δ(Q0,0) was Q1 and δ(Q1,1) was Q2. As the states are combined,
the transition of Q0 on both the inputs 0 and 1 will be to the state
Q1Q2.
 δ(Q1,0) was Q3, δ(Q1,1) was Q4 and δ(Q2,0) was Q4, δ(Q1,1) was
Q3. As the states are combined, the transition of Q1Q2 on both the
inputs 0 and 1 will be to the state Q3Q4.
 δ(Q3,0) was Q5, δ(Q3,1) was Q5 and δ(Q4,0) was Q5, δ(Q4,1) was
Q5. As the states are combined, the transition of Q3Q4 on both the
inputs 0 and 1 will be to the state Q5.
 δ(Q5,0) was Q5 and δ(Q5,1) was Q5. Hence the transition of state
Q5 on both the inputs will be to the state Q5 itself.
Transition table for Minimized DFA
Grammar in Automata-
Language Of Grammar- Language of Grammar is the set of all strings that can be generated from that grammar.

If the language consists of finite number of strings, then it is called as a Finite language.

If the language consists of infinite number of strings, then it is called as an Infinite language.
Formal Definition-
A Grammar is a 4-tuple such that-

G = (V , T , P , S)

where-

 V = Finite non-empty set of non-terminal symbols


 T = Finite set of terminal symbols
 P = Finite non-empty set of production rules
 S = Start symbol
Grammar Constituents-

A Grammar is mainly composed of two basic elements-

1. Terminal symbols

2. Non-terminal symbols

1. Terminal Symbols-
 Terminal symbols are those which are the constituents of the sentence generated using a grammar.
 Terminal symbols are denoted by using small case letters such as a, b, c etc.
2. Non-Terminal Symbols-

Non-Terminal symbols are those which take part in the generation of the sentence but are not part of it.

 Non-Terminal symbols are also called as auxiliary symbols or variables.


 Non-Terminal symbols are denoted by using capital letters such as A, B, C etc.
Examples of Grammar- Example-01:

Consider a grammar G = (V , T , P , S) where-

 V={S} // Set of Non-Terminal symbols


 T={a,b} // Set of Terminal symbols
 P = { S → aSbS , S → bSaS , S → ∈ } // Set of production rules
 S={S} // Start symbol
This grammar generates the strings having equal number of a’s and b’s

Example-02:
Consider a grammar G = (V , T , P , S) where-

 V={S,A,B} // Set of Non-Terminal symbols


 T={a,b} // Set of Terminal symbols
 P = { S → ABa , A → BB , B → ab , AA → b } // Set of production rules
 S={S} // Start symbol

Important Concept-
For any given grammar, the language generated by it is always unique.

 For any given language, we may have more than one grammar generating that language.

Example-
Consider the following two grammars-

Grammar G1-

S → AB

A→a

B→b

The language generated by this grammar is-

L(G1) = { ab }
Grammar G2-

S → AB

A→∈

B → ab

The language generated by this grammar is-

L(G2) = { ab }

Here,

 Both the grammars generate a unique language.


 But given a language L(G) = { ab }, we have two different grammars generating that language.
This justifies the above concept.

Types of Grammars-
Grammars are classified on different basis as-

DFA to Regular Expression-

The two popular methods for converting a given DFA to its regular expression are-
Arden’s Theorem-
Arden’s Theorem is popularly used to convert a given DFA to its regular expression.

It states that-

Let P and Q be two regular expressions over ∑.

If P does not contain a null string ∈, then-

R = Q + RP has a unique solution i.e. R = QP*

Conditions-
To use Arden’s Theorem, following conditions must be satisfied-

 The transition diagram must not have any ∈ transitions.


 There must be only a single initial state.
Steps-

To convert a given DFA to its regular expression using Arden’s Theorem, following steps are followed-

Step-01:

Form a equation for each state considering the transitions which comes towards that state.

 Add ‘∈’ in the equation of initial state.


Step-02:

Bring final state in the form R = Q + RP to get the required regular expression.

Important Notes- Note-01:


Arden’s Theorem can be used to find a regular expression for both DFA and NFA.

Note-02:

If there exists multiple final states, then-

 Write a regular expression for each final state separately.


 Add all the regular expressions to get the final regular expression.
Example-01:
Find regular expression for the following DFA using Arden’s Theorem-
Step-01:
Form a equation for each state-

 A = ∈ + B.1 ……(1)
 B = A.0 ……(2)
Step-02:

Bring final state in the form R = Q + RP.

Using (1) in (2), we get-

B = (∈ + B.1).0

B = ∈.0 + B.1.0

B = 0 + B.(1.0) ……(3)

Using Arden’s Theorem in (3), we get-

B = 0.(1.0)*

Thus, Regular Expression for the given DFA = 0(10)*

Regular Expression

A Regular Expression can be recursively defined as follows −


 ε is a Regular Expression indicates the language containing an
empty string. (L (ε) = {ε})
 φ is a Regular Expression denoting an empty language. (L (φ) =
{ })
 x is a Regular Expression where L = {x}
 If X is a Regular Expression denoting the
language L(X) and Y is a Regular Expression denoting the
language L(Y), then
o X + Y is a Regular Expression corresponding to the
language L(X) ∪ L(Y) where L(X+Y) = L(X) ∪ L(Y).
X . Y is a Regular Expression corresponding to the
o
language L(X) . L(Y) where L(X.Y) = L(X) . L(Y)
o R* is a Regular Expression corresponding to the
language L(R*)where L(R*) = (L(R))*
 If we apply any of the rules several times from 1 to 5, they are
Regular Expressions.
Some RE Examples
Regular
Express Regular Set
ions

(0 + 10*) L = { 0, 1, 10, 100, 1000, 10000, … }

(0*10*) L = {1, 01, 10, 010, 0010, …}

(0 + ε)(1
L = {ε, 0, 1, 01}
+ ε)

Set of strings of a’s and b’s of any length including the null string. So L = { ε, a, b,
(a+b)*
aa , ab , bb , ba, aaa…….}

(a+b)*ab Set of strings of a’s and b’s ending with the string abb. So L = {abb, aabb, babb,
b aaabb, ababb, …………..}

Set consisting of even number of 1’s including empty string, So L= {ε, 11, 1111,
(11)*
111111, ……….}

(aa)*(bb Set of strings consisting of even number of a’s followed by odd number of b’s , so L
)*b = {b, aab, aabbb, aabbbbb, aaaab, aaaabbb, …………..}
(aa + ab String of a’s and b’s of even length can be obtained by concatenating any
+ ba + combination of the strings aa, ab, ba and bb including null, so L = {aa, ab, ba, bb,
bb)* aaab, aaba, …………..}

Regular Sets
Any set that represents the value of the Regular
Expression is called a Regular Set.
Properties of Regular Sets
Property 1. The union of two regular set is regular.
Property 2. The intersection of two regular set is
regular.
Property 3. The complement of a regular set is
regular.
Property 4. The difference of two regular set is
regular.
Property 5. The reversal of a regular set is regular.
Property 6. The closure of a regular set is regular.
Property 7. The concatenation of two regular sets is
regular.
9. Identities for regular expression –
There are many identities for the regular expression. Let p, q
and r are regular expressions.
∅+r=r
∅.r= r.∅ = ∅
∈.r = r.∈ =r
∈* = ∈ and ∅* = ∈
r+r=r
r*.r* = r*
r.r* = r*.r = r+.
(r*)* = r*
∈ +r.r* = r* = ∈ + r.r*
(p.q)*.p = p.(q.p)*
(p + q)* = (p*.q*)* = (p* + q*)*
(p+ q).r= p.r+ q.r and r.(p+q) = r.p + r.q

Pumping Lemma

The language accepted by Finite Automata is known as Regular Language.


Pumping Lemma is used to prove that a Language is not Regular.
The Pumping Lemma is a tool in formal language theory that demonstrates
the non-regularity of certain languages.
What is Pumping Lemma?
The term Pumping Lemma is made up of two words:-
Pumping: The word pumping refers to generating many input strings by
pushing a symbol in an input string repeatedly.
Lemma: The word Lemma refers to the intermediate theorem in a proof.
There are two Pumping Lemmas, that are defined for
Regular Languages
Context-Free Languages
Pumping Lemma For Regular Languages
Theorem: If A is a Regular Language, then A has a Pumping Length ‘P’ such
that any string ‘S’ where |S ≥ P may be divided into three parts S = xyz such
that the following conditions must be true:
1.) xyiz ∈ A for every i ≥ 0
2.) |y| > 0
3.) |xy| ≤ P
In simple words, if a string y is ‘pumped’ or insert any number of times, the
resultant string still remains in A.
Pumping Lemma is used as proof of the irregularity of a language.
It means that if a language is regular, it always satisfies the pumping lemma.
If at least one string is made from pumping, not in language A, then A is not
regular.
We use the CONTRADICTION method to prove that a language is not
regular.
Steps to prove that a language is not Regular using Pumping Lemma:
Step 1: Assume that Language A is Regular.
Step 2: It has to have a Pumping Length (say P).
Step 3: All strings longer than P can be pumped |S| ≥ P.
Step 4: Now, find a string ‘S’ in A such that |S| ≥ P.
Step 5: Divide S into x y z strings.
Step 6: Show that xyiz ∉ A for some i.
Step 7: Then consider how S can be divided into x y z.
Step 8: Show that none of the above strings satisfies all three pumping
conditions simultaneously.
Step 9: S cannot be pumped == CONTRADICTION.
Let’s apply these above steps to check whether a Language is not Regular
with the help of Pumping Lemma.

Implementation of Pumping lemma for regular languages


Example: Using Pumping Lemma, prove that the language A = {anbn | n≥0}
is Not Regular.
Solution: We will follow the steps we have learned above to prove this.
Assume that A is Regular and has a Pumping length = P.
Let a string S = apbp.
Now divide the S into the parts, x y z.
To divide the S, let’s take the value of P = 7.
Therefore, S = aaaaaaabbbbbbb (by putting P=7 in S = apbp).

Case 1: Y consists of a string having the letter only ‘a’.

Case 2: Y consists of a string having the letter only ‘b’.

Case 3: Y consists of a string with the letters ‘a’ and ‘b’.


For all the above cases,
we need to show xyiz ∉ A for some i.
Let the value of i = 2. xyiz => xy2z
In Case 1.
xy2z = aa aaaa aaaa abbbbbbb
No of ‘a’ = 11, No. of ‘b’ = 7.
Since the No of ‘a’ != No. of ‘b’, but the original language has an equal
number of ‘a’ and ‘b’; therefore, this string will not lie in our language.

In Case 2.
xy2z = aaaaaaabb bbbb bbbb b
No of ‘a’ = 7, No. of ‘b’ = 11.
Since the No of ‘a’ != No. of ‘b’, but the original language has an equal
number of ‘a’ and ‘b’; therefore, this string will not lie in our language.

In Case 3. xy2z = aaaa aabb aabb bbbbb


No of ‘a’ = 8, No. of ‘b’ = 9.
Since the No of ‘a’ != No. of ‘b’, but the original language has an equal
number of ‘a’ and ‘b’, and also, this string did not follow the anbn pattern;
therefore, this string will not lie in our language.
We can see at i = 2 all the above three strings do not lie in the language A =
{anbn | n≥0}.
Therefore, the language A = {anbn | n≥0} is not Regular.

Context Free Grammar-


A context Free Grammar (CFG) is a 4-tuple such that-
G = (V , T , P , S)
where-
V = Finite non-empty set of variables / non-terminal symbols
T = Finite set of terminal symbols
P = Finite non-empty set of production rules of the form A → α where
A ∈ V and α ∈ (V ∪ T)*
S = Start symbol

Why Context Free Grammar Is Called So?

Context Free Grammar provides no mechanism to restrict the usage of the production
rule A → α within some specific context unlike other types of grammars.
That is why it is called as “Context Free” Grammar.

Example-01:

Consider a grammar G = (V , T , P , S) where-


V={S}
T={a,b}
P = { S → aSbS , S → bSaS , S → ∈ }
S={S}

This grammar is an example of a context free grammar.


It generates the strings having equal number of a’s and b’s.

Example-02:

Consider a grammar G = (V , T , P , S) where-


V={S}
T={(,)}
P = { S → SS , S → (S) , S → ∈ }
S={S}

This grammar is an example of a context free grammar.


It generates the strings of balanced parenthesis.
Context Free Grammar (CFG) is of great practical importance. It is used
for following purposes-

For defining programming languages


For parsing the program by constructing syntax tree
For translation of programming languages
For describing arithmetic expressions
For construction of compilers

Context Free Language-

The language generated using Context Free Grammar is called as Context Free Language

Properties-

The context free languages are closed under union.


The context free languages are closed under concatenation.
The context free languages are closed under kleen closure.
The context free languages are not closed under intersection and
complement.
The family of regular language is a proper subset of the family of context
free language.
Each Context Free Language is accepted by a Pushdown automaton.
Remember
If L1 and L2 are two context free languages, then-
 L1 ∪ L2 is also a context free language.
 L1.L2 is also a context free language.
 L1* and L2* are also context free languages.
 L1 ∩ L2 is not a context free language.
 L1′ and L2′ are not context free languages.

Ambiguity in Context Free Grammar-

A grammar is said to be ambiguous if for a given string generated by the


grammar, there exists-
more than one leftmost derivation
or more than one rightmost derivation
or more than one parse tree (or derivation tree).

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 o
the form-
A → BC or A → 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
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,

Greibach Normal Form (GNF)


GNF stands for Greibach normal form. A CFG(context free grammar) is
in GNF(Greibach normal form) if all the production rules satisfy one of
the following conditions:
A start symbol generating ε. For example, S → ε.
A non-terminal generating a terminal. For example, A → a.
A non-terminal generating a terminal which is followed by any
number of non-terminals. For example, S → aASB.
For example:
1. G1 = {S → aAB | aB, A → aA| a, B → bB | b}
2. G2 = {S → aAB | aB, A → aA | ε, B → bB | ε}
The production rules of Grammar G1 satisfy the rules specified for
GNF, so the grammar G1 is in GNF. However, the production rule of
Grammar G2 does not satisfy the rules specified for GNF as A → ε and
B → ε contains ε(only start symbol can generate ε). So the grammar
G2 is not in GNF.

Steps for converting CFG into GNF


Step 1: Convert the grammar into CNF.
If the given grammar is not in CNF, convert it into CNF. You can refer
the following topic to convert the CFG into CNF: Chomsky normal
form
Step 2: If the grammar exists left recursion, eliminate it.
If the context free grammar contains left recursion, eliminate it. You
can refer the following topic to eliminate left recursion: Left Recursion
Step 3: In the grammar, convert the given production rule into GNF
form.
If any production rule in the grammar is not in GNF form, convert it.

Example:
1. S → XB | AA
2. A → a | SA
3. B → b
4. X → a

As the given grammar G is already in CNF and there is no left recursion, so


we can skip step 1 and step 2 and directly go to step 3.
The production rule A → SA is not in GNF, so we substitute S → XB | AA
in the production rule A → SA as:
1. S → XB | AA
2. A → a | XBA | AAA
3. B → b
4. X → a
The production rule S → XB and B → XBA is not in GNF, so we substitute
X → a in the production rule S → XB and B → XBA as:
1. S → aB | AA
2. A → a | aBA | AAA
3. B → b
4. X → a
Now we will remove left recursion (A → AAA), we get:
1. S → aB | AA
2. A → aC | aBAC
3. C → AAC | ε
4. B → b
5. X → a
Now we will remove null production C → ε, we get:
1. S → aB | AA
2. A → aC | aBAC | a | aBA
3. C → AAC | AA
4. B → b
5. X → a
The production rule S → AA is not in GNF, so we substitute A → aC |
aBAC | a | aBA in production rule S → AA as:
1. S → aB | aCA | aBACA | aA | aBAA
2. A → aC | aBAC | a | aBA
3. C → AAC
4. C → aCA | aBACA | aA | aBAA
5. B → b
6. X → a
The production rule C → AAC is not in GNF, so we substitute A → aC |
aBAC | a | aBA in production rule C → AAC as:
1. S → aB | aCA | aBACA | aA | aBAA
2. A → aC | aBAC | a | aBA
3. C → aCAC | aBACAC | aAC | aBAAC
4. C → aCA | aBACA | aA | aBAA
5. B → b
6. X → a
Hence, this is the GNF form for the grammar G.

ORRRRRRRRRR

Greibach Normal Form


Introduction
GNF (Greibach Normal Form) is a specialized form of Context-Free
Grammar (CFG) where each production rule begins with a terminal symbol,
followed by optional variables.
A CFG (Context-Free Grammar) is a Greibach Normal Form(GNF) if
all of its production rules satisfy one of the following conditions:
1. A non-terminal generating terminal. For example, B -> b.
2. Start symbol generating ε. For example, S → ε.
3. A non-terminal generates a terminal followed by any number of non-
terminals. For example, A -> aBC…N.
For example:
G1 = {S → aAB | bBA, A → aA | a, B → bB | b}
G2 = {S → aAB | aBA, A → aA | ε, B → bB | ε}

The production rules of Grammar G1 satisfy the above rules specified for GNF.
Therefore, G1 is in GNF.
However, the production rule of G2 does not satisfy the rules specified for GNF as
A → ε and B → ε contains ε, but only the start symbol can generate ε. So the
grammar G2 is not in GNF.
Notes:
1. There can be more than one GNF for a given grammar.
2. GNF produces the same language as CFG produces.

Steps to convert a CFG into Greibach Normal


Form(GNF)
3. Step 1 - Convert the Grammar into Chomsky Normal Form(CNF): If the
given Grammar is not in CNF, convert it.
4. Step 2 - If the grammar contains left recursion, remove it.
5. Step 3 - Convert the production rule into GNF form in the grammar.

Let’s understand with an example,


Question: Consider the following Grammar G. Convert it into GNF.
S → AA | BC
A → a|SA
B→a
C→c

Solution:
We can skip steps 1 and 2 and move directly to step 3 because the given
grammar G is already in CNF, and there is no left recursion.
1. The production rule A → SA is not in GNF, so we substitute S -> AA | BC in the
production rule A → SA as
S → AA | BC
A → a | AAA | BCA
B→a
C→c

2. The production rule S → BC and A → BCA is not in GNF, so we substitute B →


a in the production rule S → BC and A → BCA as
S → AA | aC
A → a | AAA | aCA
B→a
C→c

3. Next, we will remove the left recursion (A → AAA), we get


S → AA | aC
A → aX | aCAX
X → AAX | ε
B→a
C→c

4. Now remove the null production X → ε, we get


S → AA | aC
A → aX | aCAX | a | aCA
X → AAX | AA
B→a
C→c

5. Now, S → AA and X → AA are not in GNF, so we substitute A → aX | aCAX |


a | aCA in production rule S → AA and X → AA as
S → aXA | aCAXA | aA | aCAA | aC
A → aX | aCAX | a | aCA
X → AAX
X → aXA | aCAXA | aA | aCAA
B→a
C→c

6. Lastly, X → AAX is not in GNF, so we substitute A → aX | aCAX | a | aCA in


production rule X → AAX as
S → aXA | aCAXA | aA | aCAA | aC
A → aX | aCAX | a | aCA
X → aXAX | aCAXAX | aAX | aCAAX
X → aXA | aCAXA | aA | aCAA
B→a
C→c
Hence, it is the GNF of Grammar G, as all the production rules follow one of the
rules mentioned above.
Push down automata in TOC
A push down automata (PDA) is a way to implement a context free
grammar (CFG) in a similar way to design the deterministic finite
automata (DFA) for a regular grammar.
A DFA can remember a finite amount of information but a PDA
can remember an infinite amount of information.
Pushdown automata is simply an NFA augmented with an "external
stack memory". The addition of stack is used to provide a last-in-first-
out memory management capability to Pushdown
automata. Pushdown automata can store an unbounded amount of
information on the stack. It can access a limited amount of
information on the stack. A PDA can push an element onto the top of
the stack and pop off an element from the top of the stack. To read
an element into the stack, the top elements must be popped off and
are lost. A PDA is more powerful than FA. Any language which can be
acceptable by FA can also be acceptable by PDA. PDA also accepts a
class of language which even cannot be accepted by FA.
Basically, a PDA is as follows −
"Finite state machine+ a stack"
PDA has three components, which are as follows −
An input tape.
A control unit.
A stack with infinite size.
A PDA may or may not read input symbols, but it has to read
the top of the stack in every transaction.

Basic Structure of PDA

PDA Components:
Input tape: The input tape is divided in many cells or symbols. The input
head is read-only and may only move from left to right, one symbol at a
time.
There are many cells or symbols on this tape. A single symbol may only be
entered at a time using the input head, which is read-only.
Finite control: The finite control has some pointer which points the current
symbol which is to be read.
Stack: The stack is a structure in which we can push and remove the items
from one end only. It has an infinite size. Items are temporarily stored on the
stack in a PDA.

Formal definition of PDA


A PDA can be formally described as 7-tuples
(Q, ∑,S, δ,q0,I,F)
Q is a finite number of states.
∑ is the input alphabet.
S is a stack symbol.
δ is the transition function: QX(∑U{e})XSXQ or mapping function which is
used for moving from current state to next state
q0 is the initial state (q0 belongs to Q).
I is the initial state top symbol.
F is a set of accepting states (F belongs to Q).

You might also like