0% found this document useful (0 votes)
159 views134 pages

Tafl Notes 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
159 views134 pages

Tafl Notes 2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 134

of functions, the classes of P and NP, Quantum Computation: quantum computers, Church-

Turing thesis. Applications: G.1 Defining syntax of programming language, Appendix J:


Security
Textbook 2: 10.1 to 10.7, 12.1, 12.2, 12.8, 12.8.1, 12.8.2
Textbook 1: Appendix: G.1(only), J.1 & J.2
RBT: L1, L2, L3
Course Outcomes: The student will be able to :
 Acquire fundamental understanding of the core concepts in automata theory and Theory of
Computation
 Learn how to translate between different models of Computation (e.g., Deterministic and
Non-deterministic and Software models).
 Design Grammars and Automata (recognizers) for different language classes and become
knowledgeable about restricted models of Computation (Regular, Context Free) and their
relative powers.
 Develop skills in formal reasoning and reduction of a problem to a formal model, with an
emphasis on semantic precision and conciseness.
 Classify a problem with respect to different models of Computation.
Question Paper Pattern:
 The question paper will have ten questions.
 Each full Question consisting of 20 marks
 There will be 2 full questions (with a maximum of four sub questions) from each module.
 Each full question will have sub questions covering all the topics under a module.
 The students will have to answer 5 full questions, selecting one full question from each module.
Textbooks:
1. Elaine Rich, Automata, Computability and Complexity, 1st Edition, Pearson
education,2012/2013
2. K L P Mishra, N Chandrasekaran , 3rd Edition, Theory of Computer Science, PhI, 2012.
Reference Books:
1. John E Hopcroft, Rajeev Motwani, Jeffery D Ullman, Introduction to AutomataTheory,
Languages, and Computation, 3rd Edition, Pearson Education, 2013
2. Michael Sipser : Introduction to the Theory of Computation, 3rd edition, Cengage learning,2013
3. John C Martin, Introduction to Languages and The Theory of Computation, 3rd Edition, Tata
McGraw –Hill Publishing Company Limited, 2013
4. Peter Linz, “An Introduction to Formal Languages and Automata”, 3rd Edition, Narosa
Publishers, 1998
5. Basavaraj S. Anami, Karibasappa K G, Formal Languages and Automata theory, Wiley India,
2012
6. C K Nagpal, Formal Languages and Automata Theory, Oxford University press, 2012.
Faculty can utilize open source tools (like JFLAP) to make teaching and learning more interactive.
MODULE 1

FINITE AUTOMATA
Finite automata are basic modules of a computer system, which addresses simple real time problems. Figure
1.1 shows conceptual view of finite automata. It has an input tape, read head and finite controller. Input
tape is divided into finite cells and each cell stores one input symbols. Read head is used to read the input
symbol from the input tape. The finite controller used to record the next state of finite automat.

Figure 1.1: Conceptual view of finite automata


Before discussing finite automata in detail, it is very much important to know the basic requirement for
the finite automata. They are
i. Sets.
ii. Graphs.
iii. Languages.
iv. Star closure.
v. Plus closure.
Sets (S):
A set is collection of elements, each element has its own identity. A set can be represented by
enclosing its elements in curly braces. for example, the set of small letters a, b, c is shown as
S= {a, b, c}
Set {2, 4, 6,…} denotes the set of all positive even integers. we can use more explicit notation, in
which we write
S={i | i is > 0 and is even}
We can perform following operations on sets, they are

a. union (𝖴),
b. intersection (∩), and
c. difference (−)
d. complementation defined as
S1 U S2 = {x | x ϵ S1 or x ϵ S2}

S1 Ո S2 = {x | x ϵ S1 and x ϵ S2}

S1 - S2 = {x | x ϵ S1 and x ∉ S2}
The complement of a set S, denoted by Ṡ consists of all elements not in S. To make this meaningful,
we need to know what the universal set U of all possible elements is. If U is specified, then
Ṡ={x | x ϵ U, x ∉ S}
The set with no elements, called the empty set or the null set, is denoted by ɛ. From the definition of a set,
it is obvious that

SHREEDEVI PRAMOD,BRCE Page 1


S U ɛ = S - ɛ =S
SՈɛ=ɛ
ἕ= U
De Morgan's laws: The following useful identities, known as De Morgan's laws.

are needed on several occasions.


Subset and Proper Subset: A set S1 is said to be a subset of S, every element of S1 is also an element of
S. We write this as S1 ⊆ S. If S1 ⊆ S, but S contains an element not in S1, we say that S1 is a proper subset
of S; we write this as S1 ⊂ S.
Disjoint Sets: If S1 and S2 have no common element, that is, S1 ∩ S2 = ɛ then the sets are said to be
disjoint.
Finite and Infinite sets: A set is said to be finite if it contains a finite number of elements; otherwise it is
infinite. The size of a finite set is the number of elements in it; this is denoted by |S|.
Power- Set: A given set has many subsets. The set of all subsets is called the power-set and is denoted by
2s. Observe that 2s is a set of sets.
If S is the set {a, b, c}, then its powerset is
2s= {ɛ,{a}, {b}, {c}, {a, b},{b, c},{a, c}, {a, b. c}}
Here |S| = 3 and |2s| = 8.
Cartesian product: Ordered sequences of elements from other sets are said to be the Cartesian product.
For the Cartesian product of two sets, which itself is a set of ordered pairs, we write
S= S1 x S2
Let S1 = {2, 4} and S2 = {2, 3, 5, 6}. Then S1 × S2 = {(2, 2), (2, 3), (2, 5), (2, 6), (4, 2), (4, 3), (4, 5), (4,
6)}. Note that the order in which the elements of a pair are written matters. The pair (4, 2) is in S1 × S2,
but (2, 4) is not.

Graphs:
A graph is a construct consisting of two finite sets, the set V = {υ1, υ2,…, υn} of vertices and the set E =
{e1, e2,…, em} of edges. Each edge is a pair of vertices from V, for instance,

ei=(v, vj).

Languages:

A finite, nonempty set of symbols are called the alphabet and is represented with symbol ∑. From the
individual symbols we construct strings, which are finite sequences of symbols from the alphabet. For
example, if the alphabet ∑ = {0}, then ∑* to denote the set of strings obtained by concatenating zero or
more symbols from ∑.

We can write ∑*= {ɛ, 0, 00, 000 …}

SHREEDEVI PRAMOD,BRCE Page 2


If ∑ = {0, 1}
0 and 1 are input alphabets or symbols, then:
(02=00, 03=000, 12=11, 13=111, where as in mathematics they are numbers 02=0, 12=1)
L=∑*={ ɛ, 0, 1, 00, 01, 10, 11, 000, 001, 010 ..........}

Kleen closure(*closure):
*closure = {∑0U∑1U∑2U∑3 ........ }
∑0={ ɛ} ∑1={0, 1} ∑2={00, 01, 10, 11}
∑3={000, 001, 010, 011, 100, 101, 110, 111}
Therefore ∑*={ ɛ , 0, 1, 00, 01, 10, 11, 000, 001, 010, 011, 100, 101,……}
+
Closure on ∑ = {0, 1}
∑+= {∑1U∑2U∑3…}
∑*=∑+ + ɛ

Therefore ∑+=∑*- ɛ
Formal Languages and Automata Theory: IT is a mathematical model used to design computer programs
and sequential logic circuits. It is state machines which captures and makes transition to accept or reject.
Finite automata has input tape, read head and finite controller Input tape: It is divided into finite cell and
each cell is used to store one input symbol. Read head: Read head is used to read input symbol from input
tape. Finite Controller: Finite controller records, the transition to next state. The default value of finite
controller is initial state q0. States are represented with circles; they are of three types,

Figure 1.2: Different types of states used in finite automata

States are connected by lines called transition. It has two ends, one end is called initiation point and the
other end is called termination point (>).

Initiation Termination

The combination of transitions and states is called a transition diagram.

Figure 1.3: A typical transition diagram

Figure 1.4 shows a transition diagram to map cities and their roads.

SHREEDEVI PRAMOD,BRCE Page 3


Figure 1.4: Finite automata with real time example.
Finite Automata are classified into two types. They are:
1) Deterministic Finite Automata (DFA)
2) Non Deterministic Finite Automata (NFA)
In Deterministic Finite Automata number of outgoing transitions are well defined from each state depending
upon input symbols. For example, if there is one Input symbol, then you find one outgoing transition from
each state, for two input symbols two outgoing transitions are present and so on.

Example 1.1: Draw the DFA, the input symbol is ∑= {0}

Figure 1.5: DFA’s for input symbol ∑= {0}.

Example 1.2: Draw the DFA, the input symbol is ∑= {0, 1}

Figure 1.6: DFA’s for input symbol ∑= {0, 1}.

Definition of DFA: we define DFA as,

MDFA = (Q, ∑, δ, Initial state, Final state)

Where, Q is set of states, ∑ =set of input symbols, δ: transition function is

From the table 1.

Q={q0,q1,q2} ∑ ={a, b, c}

δ:
δ (q0, a)=q1 δ (q0,b)=q0 δ (q0,c)=q2
δ (q1,a)=q1 δ (q1,b)=q2 δ (q1,c)=q0
δ (q2,a)=q2 δ (q2,b)=q1 δ (q2,c)=q2
Note: Initial State is q0 and final state is q2

SHREEDEVI PRAMOD,BRCE Page 4


Table 1.1: Transition table

Q\∑ a b c

q0 q1 q0 q2

q1 q1 q2 q0

*q2 q2 q1 q2

Nondeterministic Finite Automata (NFA):

If outgoing transitions are not dependent on number of input symbols, then such automata are called as
Nondeterministic Finite Automata.

For example: ∑ = {0, 1}

Figure 1.7: Nondeterministic Finite Automata for ∑ = {0, 1}

There is no outgoing transition from initial state, for a symbol ‘1 ‘and similarly there is no outgoing
transition from final state for ‘0’.

Transition Function for string:

Note: 1) δ is the transition function, used for symbols.

2) For the string (Set of symbols), is used

{}=ɛ

{a}=a

(q0, ɛ)= q0 //q0 is reading empty string, string is empty therefore, transition to same state q0.

(q0, a)= (q0, ɛa)= δ ( (q0, ɛ),a)= δ (,a)


Therefore (q0, a)= δ (q0, a)

Therefore for single symbol string =δ

(q0, wa)

Where ‘w’ is the multi-symbol string and ‘a’ is a symbol, hence we separate the transition function for
strings and symbols as given below:

δ( (q0, w), a)

SHREEDEVI PRAMOD,BRCE Page 5


Example 1.3: Write a transition table for a DFA shown in figure 1.8.

DFA over alphabet ∑ = {a, b}

Figure 1.8: Transition diagram o0f a DFA.

Table 1.2: Transition table for the figure 1.8

MDFA =(Q, ∑, δ , IS, FS)


Q ={ q0, q1}
∑ ={a ,b}
δ (q0, a) =q1 S δ q0,b) =q0
δ (q1, a) =q1 δ (q1, b) =q0 IS
=q0 FS={q0}
Example 1.4: Construct transition table for the diagram shown in figure 1.9, over the alphabet ∑={0, 1, 2}

Figure 1.9: Transition diagram

Table 1.3: Transition table for the figure 1.9

We can define above DFA as: MDFA=(A, ∑, δ, IS, {FS})

δ:
δ (A0, 0)=A1 δ (A0, 1)=A2 δ(A0, 2)=A0
δ (A1, 0)=A2 δ (A1, 1)=A0 δ(A1, 2)=A1

SHREEDEVI PRAMOD,BRCE Page 6


δ (A2, 0)=A2 δ (A2, 1)=A1 δ(A2, 2)=A0

A ={A0,A1,A2}; ∑ ={0,1,2}; IS=A0; FS=A2


String Acceptance:
The first symbol of string begins with initial state. Later, traverses zero or more than zero intermediate
state(s) for the intermediate symbol(s). Finally last symbol of the string ends with final state of automata.
This is called string accepted or otherwise string is not accepted.

Example 1.5: Find out whether the given string is accepted by the DFA. Assume the string as abab.
From the given string ∑ = {a,b} . Note that always string is traversed by left side to right side,

Figure 1.10: Transition diagram for example 1.5.

(q0, a)= δ (q0, a)= q 2 1

(q0, ab)= δ ( (q0, a) b ) from 1 (q0, a)= q2

= δ (q2, b) =q3 2
(q0, aba)= δ( (q0, ab), a) from equation 2 (q0, ab)= q3
= δ(q3, a)=q1
(q0, abab)= δ( (q0, aba), b)
= δ( (q1, b)= q0
Given string is accepted by a DFA.

Example 1.6: Find out the language accepted by the DFA.

SHREEDEVI PRAMOD,BRCE Page 7


Figure 1.12: Transition diagram of Example 1.6.

δ (q0, a)=q1; (q0, aa)= δ ( (q0, a),a)= δ (q1, a)= q1

L={a, aa, ba, baa, aba, …..}

In general we can write:

L= {w |w is a string ending with symbol a}

Example 1.7: Find out the language accepted by the DFA.

Figure 1.13: Transition diagram of Example 1.7

Initial state is also a final state, hence it accept empty string ɛ.

(q0, ɛ)=q0;

(q0, aa)= δ( (q0, a)a)= δ(q2, a)=q0;

(q0, bb)= δ( (q0, b)b)= δ(q1, b)=q0;


Therefore language L={ ɛ, aa, bb, aabb, abab…}

In general we can write L={w |w is a string consist of even number of a’s and even number of b’s}

Design of DFA :

Example 1.8: Design a DFA which accept all the strings ends with symbol a over the alphabet ∑={a, b}.

L={a, aa, ba, aba, bba…}

i. Draw the NFA for the smallest string a of the language.


The NFA for the string a is

Figure 1.14 : NFA for the string a


Convert all states of NFA to DFA, that is from each state two outgoing transitions, one for symbol ‘a’
another for ‘b’. From initial state only one outgoing transition on symbol ‘a’, one more outgoing transition
is required that is on ‘b’. If the condition in the given problem ends with, the outgoing

SHREEDEVI PRAMOD,BRCE Page 8


transition on the other symbol b, definitely loop to a initial state.

Figure 1.14: second outgoing transition from state q0

Figure 1.15 : q0 is termination for symbol b and q1 is termination for symbol a

Termination state for symbol ‘a’ is q1 and for ‘b’ it is q0. Outgoing transition from q1 on
symbol ‘a’ is loop, and for ‘b’ it is to q0 .

Figyre 1.16 :DFA which accept all the strings ends with a.

We can define above DFA as MDFA =(Q, ∑, δ , IS, {FS})


Q ={ q0, q1}
∑ ={a , b}
δ (q0, a) =q1 δ (q0,b) =q0 δ
(q1, a) =q1 δ (q1, b) =q0 IS
=q0 FS={q1}
Example 1.8: Design a DFA which accept all the strings ends with substring 01 over the alphabet

∑={0, 1}.

L={01, 001, 101, 0001,1001…}.

i. Draw the NFA for the smallest string 01 of the language.


The NFA for the string 01 is

Figure 1.17: NFA for the string 01.

ii. Convert all states of NFA to DFA, that is from each state two outgoing transitions, one for
symbol ‘0’ another for ‘1’. From initial state only one outgoing transition on symbol ‘0’, one

SHREEDEVI PRAMOD,BRCE Page 9


more outgoing transition is required that is on ‘1’. If the condition in the given problem is,
ends with, the outgoing transition on the other symbol 1, definitely loop to a initial state.

Figure 1.18 : conversion of NFA state A0 to DFA

Figure 1.19 : A0 is termination for symbol 1 and A1 is termination for symbol 0

Termination state for symbol ‘0’ is A1 and for ‘1’ it is A0. Outgoing transition from A1 on
symbol ‘0’ is loop. From A2 on symbol ‘0’ to A1 and on symboll ‘1’ to A0.

Figyre 1.20 :DFA which accept all the strings ends with 01.

We can define above DFA as MDFA=(A, ∑, δ, IS, {FS})

A={ A0, A1, A2}; ∑={0, 1}


δ:
δ (A0, 0)=A1 δ (A0, 1)=A0 δ
(A1, 0)=A1 δ (A1, 1)=A2 δ
(A2, 0)=A1 δ (A2, 1)=A0
IS=A0; FS=A2
Example 1.9: Design a DFA which accept all the strings ends with substring 012 over the alphabet

∑={0, 1, 2}.

L={012, 0012, 1012, 00012,10012…}.

i. Draw the NFA for the smallest string 012 of the language.
The NFA for the string 012 is

Figure 1.21: NFA for the string 012.

ii. Convert all states of NFA to DFA, that is from each state three outgoing transitions, one for
symbol ‘0’ second for ‘1’ third from ‘2’. From initial state only one outgoing transition on
symbol ‘0’, two more outgoing transitions are required that is on ‘1’ and ‘2’. If the condition

SHREEDEVI PRAMOD,BRCE Page 10


in the given problem ends with, the outgoing transition on the other symbols ‘1’and ‘2’,
definitely loop to a initial state.

Figure 1.22: conversion of NFA state A0 to DFA state.

Figure 1.23: A0 is termination for symbol 1, 2 and A1 is termination for symbol 0

Termination state for symbol ‘0’ is A1 and for ‘1’ and ‘2’ is A0. Outgoing transition from A1 on
symbol ‘0’ is loop and for’2’ is A 0. From A2 on symbol ‘0’ to A1 and on symbol ‘1’ to A0. From
A3 on symbol ‘0’ to A1 and on symbols ‘1’ and ‘2’ to A0.

Figure 1.24 :DFA which accept all the strings ends with 012.

We can define above DFA as MDFA=(A, ∑, δ, IS, {FS})

A={ A0, A1, A2, A3}; ∑={0, 1, 2}


δ:
δ (A0, 0)=A1 δ (A0, 1)=A0 δ(A0, 2)=A0
δ (A1, 0)=A1 δ (A1, 1)=A2 δ(A1, 2)=A0
δ (A2, 0)=A1 δ (A2, 1)=A0 δ(A2, 2)=A3
δ (A3, 0)=A1 δ (A3, 1)=A0 δ(A3, 2)=A0
IS=A0; FS=A3
Example 1.10: Design a DFA which accept all the strings begins with symbol ‘a’ over the alphabet

∑={a, b}.

L={a, aa, ab, abb, aba, aaa…}

i. Draw the NFA for the smallest string a of the language.


The NFA for the string a is

Figure 1.25: NFA for the string a.

SHREEDEVI PRAMOD,BRCE Page 11


Convert all states of NFA to DFA, that is from each state two outgoing transitions, one for
symbol ‘a’ another for ‘b’. From initial state only one outgoing transition on symbol ‘a’, one
more outgoing transition is required that is on ‘b’. If the condition in the given problem is,
begin with, the outgoing transition on the other symbol ‘b’, is to new state called dead state.
Dead state: In finite automata the string travers from initial state to final state, in some
situation we make finite automata to die called dead state. Dead state is a non-final state,
from which all outgoing transitions are loop to the same state.

Figure 1.26 : DFA which accept all the string begin with a.
ii. Condition is begin, outgoing transitions from final state is loop.

We can define above DFA as MDFA =(Q, ∑, δ , IS, {FS})


Q ={ q0, q1, q2}
∑ ={a , b}
δ (q0, a) =q1 δ (q0,b) =q2 δ
(q1, a) =q1 δ (q1, b) =q1 δ
(q2, a) =q2 δ (q2, b) =q2 IS
=q0 FS={q1}
Example 1.11: Design a DFA which accept all the strings begins with substring 01 over the alphabet

∑={0, 1}.

L={01, 010, 011, 0100, 0101…}.

i. Draw the NFA for the smallest string 01 of the language.


The NFA for the string 01 is

Figure 1.27: NFA for the string 01.

ii. Convert all states of NFA to DFA, that is from each state two outgoing transitions, one for
symbol ‘0’ another for ‘1’. From initial and intermediate states only one outgoing transition,
one more outgoing transition is required. If the condition in the given problem is, begin with,
the outgoing transition from both the states to new state called dead state.

SHREEDEVI PRAMOD,BRCE Page 12


Figure 1.28 : DFA which accept all the string begin with 01.

iii. Condition is begin, outgoing transitions from final state A2 on symbol ‘0’ and ‘1’ is loop.

We can define above DFA as MDFA=(A, ∑, δ, IS, {FS})


A={ A0, A1, A2 A3}; ∑={0, 1}

δ:
δ (A0, 0)=A1 δ (A0, 1)=A0
δ (A1, 0)=A1 δ (A1, 1)=A2
δ (A2, 0)=A1 δ (A2, 1)=A0 δ
(A3, 0)=A3 δ (A3, 1)=A3
IS=A0; FS=A2

Example 1.12: Design a DFA which accept all the strings begins with 01 or ends with 01 or both over the
alphabet ∑={0, 1}.

L={01, 010, 011, 0100, 001, 101, 0001,1001, 0101, 01101…}

We have already designed both the DFAs, joining of these DFAs, is the solution for the given problem.

Figure 1.29: DFA which accept all the string begin with 01.

Figure 1.30: DFA which accept all the string ends with 01.

SHREEDEVI PRAMOD,BRCE Page 13


Figure 1.31: DFA which accept all the string begin with 01, end with 01 begin and end with 01.

Example 1.13: Design a DFA which accept all the binary strings divisible by 2.

L={ ɛ, 10, 100, 110, …}

To design DFA we need to make the following assumptions.

From the above assumption, we can draw the following DFA.

Figure 1.32: DFA which accepts all the strings divisible by 2.

We can define above DFA as MDFA =(Q, ∑, δ , IS, {FS})


Q ={ q0, q1}
∑ ={0, 1}
δ (q0, 0) =q0 δ (q0,1) =q1 δ
(q1, 0) =q0 δ (q1, 1) =q1 IS
=q0 FS={q0}
Example:
Example 1.14: Design a DFA which accept all the binary strings divisible by 5.

L={ ɛ, 101, 1010, 1111, …}

Figure 1.33: DFA which accept all the strings divisible by 5.

SHREEDEVI PRAMOD,BRCE Page 14


We can define, designed DFA as MDFA=(A, ∑, δ, IS, {FS})
A={ q0, q1, q2 q3, q4} ∑={0, 1}

δ:
δ (q0, 0)=q0 δ (q0, 1)=q1 ; δ (q1, 0)=q2 δ (q1, 1)=q3
δ (q2, 0)=q4 δ (q2, 1)=q0 ; δ (q3, 0)=q1 δ (q3, 1)=q2
δ (q4, 0)=q3 δ (q4, 1)=q4 ;

IS=q0; FS=q0

Example 1.15: Design a DFA which accept string over x, y every block of length 3 contains at least one x.

To design a DFA we need to make following assumptions,

1. If there is no ‘x’ in the string, such a string should not be accepted.

Figure 1.34: NFA which is not accepting three y’s string.

2. One ‘x’ in the string, accepted by finite automata.

Figure 1.35: NFA which is accepting three symbol string in which one symbol is x.

3. The position of A1 and A5 is same as A0 and A4. In between A0 and A4 the symbol is ‘x’,
inbetwwen A1 and A5 is also x and so on.

Figure 1.36: DFA which accept string over x, y if every block of length 3 contains at least one x.

We can define designed DFA as MDFA=(A, ∑, δ, IS, {FS})


A= { A0, A1, A2, A4, A4, A5, A6} ∑={x, y}

δ:
δ (A0, x)= A4 δ (A0, y)= A1 ; δ (A1, x)=A5 δ (A1, y)=A2
δ (A2, x)=A6 δ (A2, y)=A3; δ (A3, x)=A3 δ (A3, y)=A3
δ (A4, x)=A5 δ (A3, y)=A5; δ (A5, x)=A6 δ (A5, y)=A6 ;

SHREEDEVI PRAMOD,BRCE Page 15


δ (A6, x)=A4 δ (A6, y)=A1

IS=A0; FS={ A6}

Example 1.16: Design a DFA which accept even number of r and even number of s over the symbol ∑={r,
s}.

i. Draw separate DFA, one accept even number of r’s and another one accept even number of
s’s.

Figure 1.37 : DFA’s accept even symbols string over r, s.

ii. Join two DFA, we get four new states AB, AC, BC, BD. Designed DFA accept even number of r
and even number of s, out of four states AB satisfied this condition, hence this state is a final
state.

Figure 1.38: DFA which accept even number of r and s.

We can define designed DFA as MDFA=(State, ∑, δ, IS, {FS})


State={ AC, AD, BC, BD} ∑={r, s}
δ:
δ (AC, r)= BC δ (AC, s)= AD ; δ (BC, r)=AC δ (BC, s)=BD
δ (AD, r)=BD δ (AD, s)=BD; δ (BD, r)=AD δ (BD, s)=BC

IS=AC; FS={ AC}

Example 1.17: Design a DFA which accept all the strings described by the language L={N0(w)≥ 1 and
N1(w)=2}

i. Exchange I symbol of first string with the last symbol of the first string, the string obtained is
the second string of the language.
ii. Draw NFA for two smallest strings, for both NFA initial and final states are same.

SHREEDEVI PRAMOD,BRCE Page 16


Figure 1.39: Two DFAs for the string 011 and 110 with same initial and final state.

Figure 1.40: DFA which accept all the strings described by the language L={N 0(w)≥ 1 and N1(w)=2}.

We can define designed DFA as MDFA=(A, ∑, δ, IS, {FS})

A={ A0, A1, A2 A3, A4, A5, A6} ∑={0, 1}


δ:
δ (A0, 0)=A1 δ (A0, 1)=A4 ; δ (A1, 0)=A0 δ (A1, 1)=A2
δ (A2, 0)=A2 δ (A2, 1)=A3 ; δ (A4, 0)=A2 δ (A4, 1)=A5
δ (A5, 0)=A3 δ (A5, 1)=A6 ; δ (A6, 0)=A6 δ (A6, 1)=A6

IS=A0; FS={A3}

Nondeterministic Finite Automata (NFA)

In DFA the outgoing transitions are defined in advance, where as in NFA the outgoing transitions are not
defined in advance. In DFA, the string is either accepted or not accepted, where as in NFA both are
possible. The finite automata shown in the figure 1.41 is a NFA. There is no outgoing transition for the
symbol ‘a’ from the state q1.

Figure 1.41: Nondeterministic Finite Automata

We can define above NFA as MDFA =(Q, ∑, δ , IS, {FS})


Q ={ q0, q1,}
∑ ={a , b}
δ:

δ (q0, a) =q1 δ (q0,b) =q0


δ (q1, b) =q0
δ (q2, a) =q2 δ (q2, b) =q2
IS =q0 FS={q1}

SHREEDEVI PRAMOD,BRCE Page 17


The NFA shown in figure 1.42 is, accepting as well as not accepting the string hod.

Figure 1.42:Non-deterministic finite automata

Example 1.18: Design NFA which accept all strings, if second symbol from right side is 1 over the
alphabet ∑={0, 1}.

L={ 10, 110, 010, 0110…}

i. First step draw the final state.

Figure 1.43: Final state

ii. Draw the intermediate state and transition for the symbols ‘0’ and ‘1’.

Figure 1.44: Intermediate and final states

iii. Draw the initial state and transition for the symbol ‘1’.

Figure 1.45: Non-deterministic finite automata accepting two strings 10, 11.

iv. Designed NFA has to accept all the strings, if second symbol from right side is symbol ‘1’, to
do this, mark loop to initial state on symbols ‘0’ and ‘1’.

Figure 1.46: NFA which accepts all strings, if second symbol from right side is 1 over the alphabet ∑={0, 1}.

v. Name the states and stop.

Figure 1.47: NFA which accept all strings, if second symbol from right side is 1 over the alphabet ∑={0, 1}.

SHREEDEVI PRAMOD,BRCE Page 18


Definition of NFA: NFA can be defined as

MDFA =(Q, ∑, δ , IS, {FS})


Q ={ q0, q1, q2}
∑ ={0, 1}
δ:
δ (q0, 0) =q0 δ (q0,1) ={q0, q1}
δ (q1, 0) =q2
δ (q1, 1) =q2
IS =q0 FS={q2}

Equivalence of NFA and DFA:

We have already seen both NFA and DFA, both are same in the definition but they differ in transition
function. If number of states in NFA has two states q0 and q1. Here, q0 is initial state and q1 is final state.
The equivalent states in DFA’s are *q0], [q1] and [q0, q1]. Where q0 is initial state and (q1), (q0, q1) are the
final states. The pair (q0, q1) is final state because; one of its states is final state. They are represented as
shown in figure 1.48

Figure 1.48: Equivalence states of NFA and DFA.

Example 1.19: Convert the NFA shown in figure 1.49 into its equivalent DFA.

Figure 1.49: Non-deterministic finite automata of Example 1.19.

The conversion begins with the initial state of the NFA which is also an initial state of DFA. Conversion
generates number of new states, for every new state we are finding the outgoing transitions fordifferent
symbols.

Table 1.4: Different tables for the conversion of NFA to DFA.

SHREEDEVI PRAMOD,BRCE Page 19


No further new states; mark the final state of DFA. Any pair of state which consists of final state of the
NFA, the particular pair is a final state in DFA.

Figure 1.50: Equivalence DFA of figure 1.49

Application of Finite automata:

Finite automat is used to search a text. There are two methods used to search text, they are:

i. Nondeterministic Finite Automata(NFA)


ii. Deterministic Finite Automata(DFA)

To search a text “shine” and “hire” the NFA shown in figure 1.51 is used

The input alphabets are ∑={s, h, i, n, e, r }

Figure 1.51: NFA used to search strings shine and hire

To search a text “shine” and “hire” the DFA shown in figure 1.52 is used

SHREEDEVI PRAMOD,BRCE Page 20


Figure 1.52: DFA used to search strings ending with shine and hire

Example 1.20: Design a finite automata, that reads string made up of letters HONDA, and recognize
those string that contain the word HOD as a substring.

i. Find out the alphabet from the string HONDA. The input symbols are ∑={H, O, N, D, A}
ii. Draw the NFA for the substring HOD.

Figure 1.53: NFA used to search string HOD

iii. Convert NFA state of figure 1.53 into DFA states.

Figure 1.54: DFA accept all the strings ends with HOD over the symbols {H, O, N, D, A}

NFA with ɛ-transition:

Till now whatever automata we studied were characterized by a transition on the symbols. But there are
some finite automata having transition for empty string also, such NFAs are called NFA- ɛ transition.

ɛ-closure (q0): states which are closed / related to state q0 on empty transition(ɛ) is called ɛ-closure (q0).

In DFA and NFA (q0, ɛ)=q0, where as in ɛ-NFA

i. (q, ɛ)= ɛ-closure( (q, ɛ))= ɛ-closure(q).


ii. (q, x)= ɛ-closure(δ( (q, ɛ), x)= ɛ-closure(δ(ɛ-closure(q), x).
iii. (q, wx)= ɛ-closure(δ( (q, w), x).

Example 1.21: Find out ɛ-closure of all the states of ɛ-NFA shown in figure 1.55.

SHREEDEVI PRAMOD,BRCE Page 21


Figure 1.55: NFA with ɛ-transition of Example 1.21.

Figure 1.56: ɛ-closure of all the states of figure 1.55

Example 1.22: Convert the ɛ-NFA shown in figure 1.57 into NFA

Figure 1.57: ɛ-NFA of Example 1.22.

First find out the ɛ-closure of all the states.

Note: state ɛ-closure consist of final state of ɛ-NFA, the particular state is a final state in NFA.

(q0, 0)= ɛ-closure(δ( (q0, ɛ), 0).

= ɛ-closure(δ({q0q1}, 0).

= ɛ-closure(δ(q0, 0) U δ(q1, 0)).

=ɛ-closure(q0 U q2).

=ɛ-closure(q0) U ɛ-closure(q2).

={q0q1} U {q0q1q2}

={q0q1q2}

(q0, 1)= ɛ-closure(δ( (q0, ɛ), 1).

= ɛ-closure(δ({q0q1}, 1).

= ɛ-closure(δ(q0, 1) U δ(q1, 1)).

SHREEDEVI PRAMOD,BRCE Page 22


=ɛ-closure(q0 U φ).

=ɛ-closure(q0) U ɛ-closure(φ).

={q0q1}

(q1, 0)= ɛ-closure(δ( (q1, ɛ), 0).

= ɛ-closure(δ(q1, 0)).

= ɛ-closure(q2).

={q0q1q2}

(q1, 1)= ɛ-closure(δ( (q1, ɛ), 1).

= ɛ-closure(δ(q1, 1)).

= ɛ-closure(φ).

(q2, 0)= ɛ-closure(δ( (q2, ɛ), 0).

= ɛ-closure(δ({q0q1q2}, 0).

= ɛ-closure(δ(q0, 0) U δ(q1, 0)U δ(q2, 0)).

=ɛ-closure(q0 U q2 U φ).

=ɛ-closure(q0) U ɛ-closure(q2) U ɛ-closure(φ).

={q0q1} U {q0q1q2} U (φ)

={q0q1q2}

(q2, 1)= ɛ-closure(δ( (q2, ɛ), 1).

= ɛ-closure(δ({q0q1q2}, 1).

= ɛ-closure(δ(q0, 1) U δ(q1, 1) U δ(q2, 1)).

=ɛ-closure(q0 U φ U φ).

=ɛ-closure(q0) U ɛ-closure(φ) U ɛ-closure(φ)

=(q0 q1) U (φ) U (φ)

= {q0q1}

SHREEDEVI PRAMOD,BRCE Page 23


Table 1.5: Equivalence table of ɛ-NFA and NFA.

Figure 1.58: NFA without ɛ-transition.

Conversion of ɛ-NFA to DFA:

It is same as conversion of NFA to DFA, begins with initial state of ɛ-NFA.

Example 1.23: Convert the ɛ-NFA shown in figure1.59 into DFA.

Figure 1.59: ɛ-NFA of Example 1.23.

First find out the ɛ-closure of all the states

Table 1.6: Equivalence table of ɛ-NFA and NFA.

(q0, 0)= ɛ-closure(δ( (q0, ɛ), 0).

= ɛ-closure(δ({q0q1}, 0).

SHREEDEVI PRAMOD,BRCE Page 24


= ɛ-closure(δ(q0, 0) U δ(q1, 0)).

=ɛ-closure(φ U q1).

=ɛ-closure(φ) U ɛ-closure(q1).

= φ U q1

=q1

(q0, 1)= ɛ-closure(δ( (q0, ɛ), 1).

= ɛ-closure(δ({q0q1}, 1).

= ɛ-closure(δ(q0, 1)U δ(q1, 1)).

=ɛ-closure(q0U φ).

=ɛ-closure(q0) U ɛ-closure(φ).

={q0q1}U φ

=[q0q1]

(q0, 2)= ɛ-closure(δ( (q0, ɛ), 2).

= ɛ-closure(δ({q0q1}, 2).

= ɛ-closure(δ(q0, 2)U δ(q1, 2)).

=ɛ-closure(φ U q2).

=ɛ-closure(q2).

=[q1q2 ]

(q1, 0)= ɛ-closure(δ( (q1, ɛ), 0).

= ɛ-closure(δ(q1, 0).

= ɛ-closure(q1).

= q1

(q1, 1)= ɛ-closure(δ( (q1, ɛ), 1).

= ɛ-closure(δ(q1, 1).

= ɛ-closure(φ).

SHREEDEVI PRAMOD,BRCE Page 25


= φ

(q1, 2)= ɛ-closure(δ( (q1, ɛ), 2).

= ɛ-closure(δ(q1, 2)

= ɛ-closure(q2)

= [q1 q2]

([q0q1], 0)= ɛ-closure(δ( ([q0q1], ɛ), 0)

= ɛ-closure(δ( (q0, ɛ)U (q1, ɛ)), 0).

= ɛ-closure(δ((q0, q1) U q1), 0)).

= ɛ-closure(δ((q0, q1), 0)).

= ɛ-closure(δ(q0, 0) U δ(q1, 0)).

= ɛ-closure(φ U q1)

= ɛ-closure(q1)

= q1

([q0q1], 1)= ɛ-closure(δ( ([q0q1], ɛ), 1)

= ɛ-closure(δ( (q0, ɛ)U (q1, ɛ)), 1).

= ɛ-closure(δ((q0, q1) U q1), 1)).

= ɛ-closure(δ((q0, q1), 1)).

= ɛ-closure(δ(q0, 1) U δ(q1, 1)).

= ɛ-closure(q0 U φ)

= ɛ-closure(q0)

= [q0, q1]

([q0q1], 2)= ɛ-closure(δ( ([q0q1], ɛ), 2)

= ɛ-closure(δ( (q0, ɛ)U (q1, ɛ)), 2).

= ɛ-closure(δ((q0, q1) U q1), 2)).

= ɛ-closure(δ((q0, q1), 2)).

= ɛ-closure(δ(q0, 2) U δ(q1, 2)).

SHREEDEVI PRAMOD,BRCE Page 26


= ɛ-closure(φ U q2 )

= ɛ-closure(q2)

= [q1 q2]

([q1q2], 0)= ɛ-closure(δ( ([q1q2], ɛ), 0)

= ɛ-closure(δ( (q1, ɛ)U (q2, ɛ), 0)).

= ɛ-closure(δ(q1U (q1 q2)), 0).

= ɛ-closure(δ(q1, q2), 0 )).

= ɛ-closure(δ(q1, 0 ) U δ (q2, 0 ))

= ɛ-closure(q1 U φ).

= ɛ-closure(q1)

= q1

([q1q2], 1)= ɛ-closure(δ( ([q1q2], ɛ), 1)

= ɛ-closure(δ( (q1, ɛ)U (q2, ɛ), 1)).

= ɛ-closure(δ(q1U (q1 q2)), 1).

= ɛ-closure(δ(q1, q2), 1 )).

= ɛ-closure(δ(q1, 1 )U δ (q2, 1 ))

= ɛ-closure(φ U φ).

= ɛ-closure(φ)

([q1q2], 2)= ɛ-closure(δ( ([q1q2], ɛ), 2)

= ɛ-closure(δ( (q1, ɛ)U (q2, ɛ), 2)).

= ɛ-closure(δ(q1U (q1 q2)), 2).

= ɛ-closure(δ(q1, q2), 2)).

= ɛ-closure(δ(q1, 2 )U δ (q2, 2 ))

= ɛ-closure(q2 U φ).

= ɛ-closure(q2)

SHREEDEVI PRAMOD,BRCE Page 27


= [q1 q2]

Table 1.7: Equivalence table of ɛ-NFA and DFA.

( φ, (0,1,2))= φ

Mark the final states, any pair which has a final state of ɛ -NFA the particular pair is afinal state in DFA.

Table 1.7: Equivalence table of ɛ-NFA and DFA .

Figure 1.60: DFA of figure 1.59.

SHREEDEVI PRAMOD,BRCE Page 28


MODULE 2

REGULAR EXPRESSIONS

Language represented in the form of expression (symbols separated by operator) is called regularexpression.

∑= {a, b, 0, 1}

Language L= ∑*= {a, b, 0, 1}* ={ Ɛ, a, b, 0, 1, aa, bb, 00, 11…}

Regular expression R= { Ɛ + a + 0 + 1 + aa + bb + 00 + 11…}

Before studying regular expression in detail, we will discuss some fundamental rules of the regular
expression.

i. Φ+1=1 (φ is like, number 0).


ii. Ɛ + 1= Ɛ+1 (Ɛ is like number 1)
iii. φƐ=φ
iv. Ɛ+φ=Ɛ
v. 1+1=1
vi. 0+0=0
vii. 0+1=0+1
viii. 0+01=0(Ɛ+1)
ix. 0Ɛ=0
x. 0φ=0
xi. Φ*={Ɛ+ φ+ φφ…}={Ɛ+φ}=Ɛ
xii. 0++Ɛ=0*
xiii. 0*+Ɛ=0*
xiv. (1+Ɛ)+=1*
xv. (1+φ)+=1+
xvi. 00*=0+
xvii. Ɛ+11*=Ɛ+1+=1*
xviii. (Ɛ+11)(Ɛ+11)*=(Ɛ+11)+=(11)*
xix. (00)*(1+01)=(00)*(Ɛ+0)1= (Ɛ+00+0000.. )(Ɛ+0)1=0*1
xx. Write a regular expression set of strings made up of 0’s and 1’s ending with 00
RE=(0+1)*00.
xxi. Write a regular expression for all the set of string made up of 0’s and 1’s which begin with 0and
end with 1
RE=0(1+0)*1
xxii. Strings begins with 0 or 1 is represented as (0+1)(0+1)*
xxiii. All strings begins with 0 or 1 and not having two consecutive 0’s
RE=(0+1)(1+10)
xxiv. Write regular expression which consist of two consecutive 0’s
RE=(0+1)*00(0+1)*
xxv. All the strings of 0’s and 1’s whose last two symbols are same
RE=(0+1)*(00+11)
xxvi. Sets of all strings which starts with either 00 or 11 or ends with 00 or 11

SHREEDEVIPRAMOD,CSE,BRCE 30
RE=(00+11) (0+1)* +(0+1)*(00+11).
xxvii. Find the regular expression of the language L={anbm :n≥0, m≥0}
L={Ɛ+a+aa..}{Ɛ+b+bb…}
RE=a*b*
xxviii. Find the regular expression of the language L={anb2m+2 : n≥0, m≥0}
RE=(aa)*(bb)*bb.
xxix. Find the regular expression of the language L={a2nbm : n≥4, m≤3}
RE=(aaaa)*(Ɛ+b+bb+bbb)
xxx. Find the regular expression of the language L={anbm/n+m is even}
RE=(aa)*(bb)*+a(aa)*b(bb)*

Conversion from Regular expression to finite automata:

Three basic regular expressions are used in conversion (0+1), 01 and 0*.

The finite automata which accept either 0 or 1 is shown in figure 2.1.

Figure 2.1: Finite automata

But in regular expression each symbol is having its own finite automata.

Definition for the finite automat which accept the symbol ‘1’ is

M1= (Q1,∑1, δ, IS1, FS1)

Q1={C, D}; ∑1=1; δ(C, 1) = D; IS=C; FS=D

Definition for the finite automat which accept the symbol ‘0’ or ‘1’ is

M= (Q,∑, δ, IS, FS)

Q={Q0 U Q1 U E U F} ={ A, B, C, D, E. F);

∑= {0, 1}

δ(A, 0)= B; δ (C, 1)= D; δ(E, Ɛ)={A, C}; δ(D, Ɛ)=F: δ(D, Ɛ)=F

IS=E; FS=F

For the symbol ‘0’ the finite automata is shown in figure 2.2

Figure 2.2: Finite automata for regular expression 0.

SHREEDEVIPRAMOD,CSE,BRCE 31
Definition for the finite automat which accept the symbol ‘0’ is

M0= (Q0,∑0, δ, IS0, FS0)

Q0={A, B}; ∑0=0; δ(A, 0)= B; IS=A; FS=B

For the symbol ‘1’ is shown in figure 2.3

Figure 2.3: Finite automata for regular expression 1.

Definition for the finite automat which accept the symbol ‘1’ is

M1= (Q1, ∑1, δ, IS1, FS1)

Q1={C, D}; ∑1=1; δ(C, 1)= D; IS=C; FS=D

We are joining these two finite automata with disturbing the regular expression 0+1 as shown in figure
2.4. To join these two finite automata we need more extra states, one for the initial state and second is for
the final state, states A and B no more initial states and B and D are no more final states. All these states
will now become intermediate states as shown in figure 2.4.

Figure 2.4: Finite automata for regular expression 0+1.

Definition for the finite automat which accept the symbol ‘0’ or ‘1’ is

M= (Q,∑, δ, IS, FS)

Q={Q0 U Q1 U E U F} ={ A, B, C, D, E. F);

∑={0, 1}

δ(A, 0)= B; δ (C, 1)= D; δ(E, Ɛ)={A, C}; δ(D, Ɛ)=F: δ(D, Ɛ)=F

IS=E; FS=F

For the regular expression 01 the finite automata is shown in figure 2.6.

Figure 2.5: Finite automata for regular expression 0 and 1.

SHREEDEVIPRAMOD,CSE,BRCE 32
Rewrite the expression 01 as 0Ɛ1.

We can join the two finite automata shown in figure2.5 and figure 2.6 without disturbing the regular
expression 01 is shown in figure 2.6.

Figure 2.6: Finite automata for regular expression 01.

Definition for the finite automat which accept the string 01 is

M= (Q,∑, δ, IS, FS)

Q={Q0 U Q1} ={ A, B, C, D);

∑={0, 1}

δ(A, 0)= B; δ (C, 1)= D; δ(B, Ɛ)= C

IS=A; FS=D

Like that we can design the finite automata for 0*.

0*= {Ɛ, 0, 00, 000…}

The regular expression for symbol ‘0’ is shown in figure 2.7

Figure 2.7: Finite automata for regular expression 0.

The designed finite automata accept all the strings of 0* ie., {Ɛ, 0, 00, 000…}

Figure 2.8: Finite automata for regular expression 0*.

Definition for the finite automat which accept the string 0* is

M= (Q,∑, δ, IS, FS)

Q={Q0 U E U F} ={ A, B, E, F);

∑=0

SHREEDEVIPRAMOD,CSE,BRCE 33
δ

δ(A, 0)= B; δ (E, Ɛ)= {A, F}; δ(B, Ɛ)= {F, A}

IS=E; FS=F

Example 2.1: Design a finite automat for the regular expression 0*1

i. Draw the finite automata for the symbol ‘0’

Figure 2.9: Finite automata for regular expression 0.

ii. Convert the finite automata of symbol ‘0’ to 0*

Figure 2.10: Finite automata for regular expression 0*.

iii. Draw the finite automata for the symbol ‘1’

Figure 2.11: Finite automata for regular expression 1.

Join finite automata of 0* and 1, which will be the solution for the regular expression 0*1

Figure 2.12: Finite automata for regular expression 0*1.

Example 2.2: Design a finite automat for the regular expression 0(0+1)*1
i. Draw the finite automata for the symbol ‘0’ and ‘1’

Figure 2.13: Finite automata for regular expression 0.

SHREEDEVIPRAMOD,CSE,BRCE 34
Figure 2.13: Finite automata for regular expression 1.

ii. Construct the finite automata for the expression 0 + 1

Figure 2.14: Finite automata for regular expression 0+1.

iii. Construct the finite automata for the symbol (0 + 1)*

Figure 2.15: Finite automata for regular expression (0+1)*.

iv. Join the finite automata of ‘0’, (0+1)* and ‘1’

Figure 2.16: Finite automata for regular expression 0(0+1)*1.

Conversion from Finite Automata to Regular Expression:

They are of two methods of conversions, they are:

i. Recursive Method.
ii. State Elimination Method.

i. Recursive Method :
In this method it uses recursive method, to construct new regular expression.
For example the regular expression of DFA shown in figure 2.17 is

SHREEDEVIPRAMOD,CSE,BRCE 35
Figure 2.17: A typical Finite automata.

In order to obtain the value of regular expression , we use all the (transition) possible regular
expression of the DFA. The possible regular expression of the figure 2.17 is shown in the table2.1.

Table 2.1: Recursive table

Equations to obtain the regular expression of all the transitions are as follows:

If state 2 and 3 are final states then the regular expression is written as:

Example 2.3: Find the regular expression of DFA shown in figure 2.17 by using recursive method.

Example 2.4: Find the regular expression of DFA shown in figure 2.18 by using recursive method.

SHREEDEVIPRAMOD,CSE,BRCE 36
State Elimination Method:

In state elimination method, we obtain regular expression, by eliminating all the intermediate states. After
eliminating the intermediate states, it is easy to find the regular expression for the remaining states (initial
and final).

Figure2.19: Finite automata

The regular expression of the finite automata shown in figure2.20 is:

SHREEDEVIPRAMOD,CSE,BRCE 37
Figure2.20: Finite automata

RE=01

Figure2.21: Finite automata

The regular expression of the finite automata shown in figure 2.2 is:

Figure2.22: Finite automata

RE=0a*1

From the above examples we come to know that eliminating state has one predecessor state A 0 and one
successor state A2. In complex finite automata the eliminating state may have more number of predecessors
and successors. One Such example is shown in the figure 2.23.

Figure 2.23: Eliminating state E has n predecessor and n successor.

Before eliminating any state from the finite automata, we need to find out the path passing through the
eliminating state. The paths passing through the eliminating state are P1ESn, P1ES1, PnES1 and PnESn.
After eliminating state E, the automata is shown in the figure 2.24.

Figure 2.24: Finite automata after eliminating state E.

After eliminating all predecessors and successors, leaving initial state and final state, the remaining
machine looks shown in the figure 2.25.

SHREEDEVIPRAMOD,CSE,BRCE 38
Figure 2.25: Finite automata after eliminating all predecessors and successors.

The regular expression is, language accepted by the initial state of finite automata and Su*.

The language accepted by the initial state is {R*+Su*t+R*Su*t + su*tR*…}, this is like {x*+y*+xy + yx
…}

We can simplify it as (x+y)* similarly, we can simplify the language for the initial state as (R+Su*t)*.
Therefore, regular expression of finite automata is (R+Su*t)*Su*. Note that in some cases the same state is
initial as well as final state.

Figure 2.26: Same state is initial as well as final state.

The regular expression is R*.

To eliminate state, three rules are used, they are:

Figure 2.27: One path passing through eliminating state A1

Figure 2.28: Two paths are passing through eliminating state A1

SHREEDEVIPRAMOD,CSE,BRCE 39
Figure 2.29: Four paths are passing through eliminating state A1

Example 2.5: Construct regular expression for the finite automat shown in figure 2.30.

Figure 2.30: Finite Automata

No intermediate in the given finite automata, hence we can’t eliminate any of the state.

R=b; S=a; u=a; t=b;

Therefore RE =(R+Su*t)*Su*

= (b+aa*b)*aa*

=b(Ɛ+aa*)*aa*

=b(aa*)*aa*

Example 2.6: Construct regular expression for the finite automat shown in figure 2.31 by using state
elimination method.

Figure 2.31: Finite Automata.

We can eliminate one intermediate state A1, the paths passing through the eliminating state are A0A1A0,
A0A1A2, A2A1A0 and A2A1A2.

SHREEDEVIPRAMOD,CSE,BRCE 40
Figure 2.32: Final finite Automata

RE=(R+Su*t)*Su*

= ((2+02*1)+(1+02*0)(0+12*0)*(2+02*0))*((1+02*0)(0+12*0)*.

Application of Regular expression:

Pumping lemma:

It is used to prove language as non-regular for example

L=(00, 000, 0000…}

The automat is shown in figure 2.33 which accept 00.

Figure 2.33: Finite Automata

The automat is shown in figure 2.34 which accept 000.

Figure 2.34: Finite Automata pumped at intermediate state one time.

The automat is shown in figure 2.35 accept 0000.

Figure 2.35: Finite Automata pumped at intermediate state one time.

If more 0s, there will be more number of pumping. In such a case it is very difficult to count the number
of pumping. To count the number of pump easily we draw circles around the intermediate state so that we
can easily count the number of pumping to prove the language as non-regular.

SHREEDEVIPRAMOD,CSE,BRCE 41
Figure 2.35: Finite Automata pumped at intermediate state finite time.

We can write uv iw Є L for i={1, 2, 3...}.

To prove the language as non-regular, smallest string of language is divided into uvw by considering two
conditions.

i. V Ɛ.
ii. |UV|≤n where n is the smallest string of the language. To prove the language as a non-regular,
throughout the proof the value of n is constant.

Example 2.7: Prove that the language L={ :i≥1} is not regular.

uviw ЄL for i={1, 2…}

0(00)i000000 ЄL

I=1

000000000 ЄL

I=2

00000000000 L

When i=2 the string obtained is not belongs to language, hence proved language is not regular.

Example 2.8: Prove that the language L={anbn+1 :n≥2} is not regular.

i=1

aaabbbb Є L

I=2

Aaabaabbbb L

When i=2 the string obtained is not belongs to language, hence proved the language is non-regular.

SHREEDEVIPRAMOD,CSE,BRCE 42
Closure Properties of Regular Expression:

Till now we have seen many regular expressions and their applications. These regular expressions are
bounded/ related by operators called closure properties of regular expression. The different operators used
to relate regular expressions are:

i. Boolean.
ii. Reversal.
iii. String Homomorphism and Inverse Homomorphism.
i. Boolean Operator:
They are of different types
a. Union b. Intersection c. Difference d. Compliment.
a. Union Operation.
To perform union operation it uses following steps.
 This operation is applied on two machines defined as
M1=(Q1, ∑1, δ1, IS1, FS1)
M2=(Q2, ∑2, δ2, IS2, FS2)
 Find the language L1 and L2 of the two machines.
 Perform union operation on the two languages L1 and L2 to obtain language L3
 Combine two machines to perform union operation.
 Apply language L3 on combined machines to obtain machine for union operation.

Same steps are used for the other Boolean operation.

M1UM2=(Q1UQ2, ∑1U∑2, δ1Uδ2, ?, ?)


For example consider the machine 1 is shown in figure 2.36

Figure 2.36: Finite Automata

Machine 2 is shown in figure 2.37

Figure 2.37: Finite Automata

The union operation on two languages is

In order to obtain the finite automata for the union operation, we need to join two finite automata. The
transition table for union operation is shown in table…

Table 2.1: Transition table for Boolean operation

SHREEDEVIPRAMOD,CSE,BRCE 43
Equivalent diagram is shown in the figure 2.38

Figure 2.38: Finite Automata after combining figure 2.36 and 2.37.

According to union operation it has to accept 0, 00

Figure 2.39: Finite Automata to accept 0, 00.

it has to accepts 1, 01, 11, 011

Figure 2.40: Finite Automata accepts 1, 01, 11, 011.

It also accepts 10, 010…

Figure 2.41: Finite Automata accepts 10, 010...

The final automata for the union operation is shown in figure 2.42

SHREEDEVIPRAMOD,CSE,BRCE 44
Figure 2.42: Union of two finite automata of figure 2.36 and 2.37.

For intersection M1 2={10, 100, 010, 1010…}

Figure 2.43: Intersection of two finite automata of figure 2.36 and 2.37.

For differentiation M1-2 ={1, 11, 01, 011, 101}

Figure 2.44: Difference of two finite automata of figure 2.36 and 2.37.

Compliment:

To compliment the regular expression it uses the following steps:

i. Obtain the Ɛ-NFA/NFA for the regular expression.


ii. Convert the Ɛ-NFA to DFA.
iii. Compliment DFA.
iv. Convert DFA to regular expression.

Example 2.9: Find the compliment of regular expression 0.

i. Obtain the Ɛ-NFA/NFA for the regular expression.

Figure 2.45: finite automata for regular expression 0.

ii. Convert the Ɛ-NFA/NFA to DFA.

δ(A, 0)=B; δ(B, 0)=φ; δ(φ,0)=φ

SHREEDEVIPRAMOD,CSE,BRCE 45
Figure 2.46 : Equivalent DFA for the figure 2.45.

iii. Compliment DFA. Reverse all transitions and convert all final to non final and vice-versa.

Figure 2.47: Complimented DFA of figure 2.46.

State B and φ are not reachable from the initial State. Hence the two states are removed from the finite
automata.

Figure 2.48. Complimented DFA for the regular expression 0.

The regular expression of finite automata shown in figure 2.48 is

RE=Ɛ

Reversal of regular expression:

i. Reverse all transitions.


ii. Convert initial state to final and final state to intermediate state.
iii. Draw epsilon-transition between new initial state to all previous final states, mark
new state as an initial state.

Figure 2.49: Non-deterministic finite automata.

Figure 2.50: Reversed NFA of the figure 2.49

String Homomorphism:

String homomorphism is a function on the language, it replaces string by symbol

SHREEDEVIPRAMOD,CSE,BRCE 46
Figure 2.51: Homomorphism.

Inverse Homomorphism:

It is an inverse string homomorphism

Figure 2.52: Inverse string homomorphism

Example 2.10: Consider the homomorphism, h(0)=abb; h(1)= a; h(011)=abbaa

If the homomorphism from an alphabet {0, 1, 2} to {a, b} is defined by h(0)=ab; h(1)=b; &
h(2)=aa, then:

i. What is h(2201).
ii. If L is language 1*02* what is h(L).
iii. If L is language(ab+baa)*bab then what is h-1(L)
i. h(2201)=aaaaabb
ii. h(L)=h(1*02*)=b*ab(aa)*
iii. (ab+baa)*bab=(0+12)*10

Equivalence and minimization:

In Equivalence, two different DFA’s are given, we need to find out the two DFA’s are similar or not.
By filling table and Preshapat tree we can decide the two DFAs are similar | not similar.

Preshapat Tree: To find pair of nodes similar | not similar Preshapat tree is used. It consists of root
node, intermediate nodes and leaves nodes. The root node of the tree is pair of initial states of two
DFAs. Next we construct branches of tree depending on number of input symbols. If leaves node do
not have a cross in the difference table and same pairs are repeating, we stop constructing tree and
decide the root pair is similar or else dissimilar, any of leaves node has a cross in the difference table.

Figure 2.53: Deterministic finite automata.

Figure 2.54: Difference table with Preshapat tree.


Leaves node 14 has cross in the difference table, so two DFA’s are not equal.

SHREEDEVIPRAMOD,CSE,BRCE 47
For example

Figure 2.55: Deterministic finite automata.

Note that root of the tree is pair of initial states. Leaves nodes of the tree do not have cross in the
difference table and nodes are repeating, hence the two DFA’s are similar.

Figure 2.56: Difference table with Preshapat tree.

Example 2.11: Find out the two DFAs are similar or not.

Figure 2.57: Deterministic finite automata.

SHREEDEVIPRAMOD,CSE,BRCE 48
Figure 2.58: Transition tree with table.

The leaves node AG has X in the table; hence two DFAs are dissimilar.

In case of minimization, the two states are minimized to one state if they are similar. For example state X
and Y are equal, they are replaced by one state as shown in figure 2.59

Figure 2.59: Similar states are replced into single state.

To decide the states are similar | not similar we use Shanprepat tree.

Shanprepat Tree: To find equivalence between pair of states Shanprepat tree is used. It consists of root
node, intermediate nodes and leaves nodes. The root node of the tree is pair of states used to find the similar
| not similar states. Next we construct branches of tree depending on number of input symbols. If leaves
node do not have a cross in the difference table and same pairs are repeating, we stop constructing tree and
decide the root pair is similar or else decide two states are dissimilar, any of leaves node has a cross in the
difference table.

Example 2.12: Using table filling method, minimize the DFA shown in figure 2.60 and draw the transition
diagram of the resulting DFA.

Figure 2.60: Deterministic finite automata.

Figure 2.61: Differnce table with Shanprepat Tree

Figure 2.62: Minimized DFA of the figure 2.60

SHREEDEVIPRAMOD,CSE,BRCE 49
Example 2.13: Using table filling method, minimize the DFA shown in figure 2.63 and draw the
transition diagram of the resulting DFA.

Figure 2.63: Deterministic finite automata.

From the given DFA we draw the dissimilar table as shown in figure. As compared to final state C all
states are different (non-final states), hence X is marked.

Figure 2.64: Differnce table with Shanprepat Tree.

The leaves node decides the root node. Any leaves node has X in the table, the root node is marked as X
or else pair node repeats. Stop when the pair nodes start repeating, and decide the root node pair is similar.

Figure 2.64: Differnce table with Shanprepat Tree.

SHREEDEVIPRAMOD,CSE,BRCE 50
Figure 2.65: Differnce table with Shanprepat Tree.

Figure 2.66: Differnce table with Shanprepat Tree.

Figure 2.66: Differnce table with Shanprepat Tree.

From the table, we come to know that A=E, B=H and D=F. the resulting DFA is shown in figure..

Figure 2.67: Minimized DFA of the figure 2.63

SHREEDEVIPRAMOD,CSE,BRCE 51
Example 2.14: Using table filling method, minimize the DFA shown in table and draw the transition
diagram of the resulting DFA.

The table shows the dissimilarity in the states of the given problem.

Figure 2.68: Differnce table with Shanprepat Tree.

SHREEDEVIPRAMOD,CSE,BRCE 52
Figure 2.69: Differnce table with minimized DFA.

53
Module -III
Context-Free Grammars and Pushdown Automata (PDA)
1. Introduction to Rewrite Systems and Grammars

What is Rewrite System?


A rewrite system (or production system or rule based system) is a list of rules, and an
algorithm for applying them. Each rule has a left-hand side and a right hand side.
X→Y
(LHS) (RHS)
Examples of rewrite-system rules: S  aSb, aS  , aSb  bSabSa

When a rewrite system R is invoked on some initial string w, it operates as follows:


simple-rewrite(R: rewrite system, w: initial string) =
1. Set working-string to w.
2. Until told by R to halt do:
1.1 Match the LHS of some rule against some part of working-string.
1.2 Replace the matched part of working-string with the RHS of the rule that
was matched.
3. Return working-string.
If it returns some string s then R can derive s from w or there exists a derivation in R of s
from w.
Examples:
1. A rule is simply a pair of strings where, if the string on the LHS matches, it is
replaced by the string on the RHS.
2. The rule axa  aa squeeze out whatever comes between a pair of a’s.
3. The rule ab*ab*a  aaa squeeze out b’s between a’s.
Rewrite systems can be used to define functions. We write rules that operate on an input string
to produce the required output string. Rewrite systems can be used to define languages. The
rewrite system that is used to define a language is called a grammar.

Grammars Define Languages


A grammar is a set of rules that are stated in terms of two alphabets:
• a terminal alphabet, , that contains the symbols that make up the strings in L(G),
• a nonterminal alphabet, the elements of which will function as working symbols that
will be used while the grammar is operating. These symbols will disappear by the time
the grammar finishes its job and generates a string.
• A grammar has a unique start symbol, often called S.

A rewrite system formalism specifies:


• The form of the rules that are allowed.
• The algorithm by which they will be applied.
• How its rules will be applied?

Using a Grammar to Derive a String

SHREEDEVIPRAMOD,CSE,BRCE 53
Simple-rewrite (G, S) will generate the strings in L(G). The symbol ⇒ to indicate steps in a
derivation.
Given: S  aS ------ rule 1
S   ------ rule 2
A derivation could begin with: S ⇒ aSb ⇒ aaSbb ⇒ aabb

Generating Many Strings


LHS of Multiple rules may match with the working string.
Given: S  aSb------------- rule 1
S  bSa------------- rule 2
S   ---------------- rule 3
Derivation so far: S ⇒ aSb ⇒ aaSbb ⇒
Three are three choices at the next step:
S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb (using rule 1),
S ⇒ aSb ⇒ aaSbb ⇒ aabSabb (using rule 2),
S ⇒ aSb ⇒ aaSbb ⇒ aabb (using rule 3).

One rule may match in more than one way.


Given: S  aTTb -----------rule 1
T  bTa ------------rule 2
T   ------------------------- rule 3
Derivation so far: S ⇒ aTTb ⇒
Two choices at the next step:
S ⇒ aTTb ⇒ abTaTb ⇒
S ⇒ aTTb ⇒ aTbTab ⇒

When to Stop
Case 1: The working string no longer contains any nonterminal symbols (including, when it is
). In this case, we say that the working string is generated by the grammar.
Example: S ⇒ aSb ⇒ aaSbb ⇒ aabb
Case 2: There are nonterminal symbols in the working string but none of them appears on the
left-hand side of any rule in the grammar. In this case, we have a blocked or non-terminated
derivation but no generated string.
Given: S  aSb ----- rule 1
S bTa ----- rule 2
S   ----- rule 3

Derivation so far: S ⇒ aSb ⇒ abTab ⇒


Case 3: It is possible that neither case 1 nor case 2 is achieved.
Given: S  Ba------------- rule 1
B  bB ------------ rule 2
Then all derivations proceed as: S ⇒ Ba ⇒ bBa ⇒ bbBa ⇒ bbbBa ⇒ bbbbBa ⇒ ...
The grammar generates the language Ø.

2. Context –Free Grammar and Languages

SHREEDEVIPRAMOD,CSE,BRCE 54
Recall Regular Grammar which has a left-hand side that is a single nonterminal and have a
right-hand side that is  or a single terminal or a single terminal followed by a single
nonterminal.
X →Y
( NT) ( or T or T NT)

Example: L = {w Î {a, b}* : |w| is even} RE = ((aa) (ab) (ba) (bb))*

Context Free Grammars


X →Y
(NT) (No restriction)

SHREEDEVIPRAMOD,CSE,BRCE 55
No restrictions on the form of the right hand sides. But require single non-terminal on left hand
side.
Example: S  , S  a, S  T, S  aSb, S aSbbT are allowed.
ST aSb, a aSb,   a are not allowed.
The name for these grammars “Context Free” makes sense because using these rules the
decision to replace a nonterminal by some other sequence is made without looking at the
context in which the nonterminals occurs.
Definition Context-Free Grammar
A context-free grammar G is a quadruple, (V, , R, S), where:
• V is the rule alphabet, which contains nonterminals and terminals.
•  (the set of terminals) is a subset of V,
• R (the set of rules) is a finite subset of (V - ) V*,
• S (the start symbol) is an element of V - .
Given a grammar G, define x ⇒G y to be the binary relation derives-in-one-step, defined so
that ∀ x,y  V* (x ⇒G y iff x = A, y =    and there exists a rule A   is in RG )
Any sequence of the form w0 ⇒G w1 ⇒G w2 ⇒G . . . ⇒G wn is called a derivation in G. Let
⇒G* be the reflexive, transitive closure of ⇒G. We’ll call ⇒G* the derive relation.

A derivation will halt whenever no rules on the left hand side matches against working-string.
At every step, any rule that matches may be chosen.

Language generated by G, denoted L(G), is: L(G) = {w  * : S ⇒G* w}. A language L is


context-free iff it is generated by some context-free grammar G. The context-free languages
(or CFLs) are a proper superset of the regular languages.

Example: L = AnBn = {anbn : n ≥ 0} = {, ab, aabb, aaabbb, …}


G = {{S, a, b}, {a, b}, R, S}, where: R = { S  aSb , S  }
Example derivation in G: S ⇒ aSb ⇒ aaSbb ⇒ aaaSbbb ⇒ aaabbb or S ⇒* aaabbb

Recursive Grammar Rules


A grammar is recursive iff it contains at least one recursive rule. A rule is recursive iff it is X
 w1Yw2, where: Y ⇒* w3Xw4 for some w1, w2, w3, and w4 in V*. Expanding a non- terminal
according to these rules can eventually lead to a string that includes the same non- terminal
again.
Example1: L = AnBn = {anbn : n ≥ 0} Let G = ({S, a, b}, {a, b}, {S  a S b, S  }, S)

Example 2: Regular grammar whose rules are {S  a T, T  a W, W  a S, W  a }

Example 3: The Balanced Parenthesis language


Bal = {w  { ),(}*: the parenthesis are balanced} = { , (), (()), ()(), (()()) .............................. }
G={{S,),( }, {),(},R,S} where R={ S   S  SS S  (S) }
Some example derivations in G:
S ⇒ (S) ⇒ ()
S ⇒ (S) ⇒ (SS) ⇒ ((S)S) ⇒ (() S)) ⇒ (() (S)) ⇒ (()())
SHREEDEVIPRAMOD,CSE,BRCE 56
So, S ⇒* () and S ⇒* (()())
Recursive rules make it possible for a finite grammar to generate an infinite set of strings.
Self-Embedding Grammar Rules
A grammar is self-embedding iff it contains at least one self-embedding rule. A rule in a
grammar G is self-embedding iff it is of the form X  w1Yw2, where Y ⇒* w3Xw4 and both
w1w3 and w4w2 are in +. No regular grammar can impose such a requirement on its strings.
Example: S  aSa is self-embedding
S  aS is recursive but not self- embedding
S  aT
T  Sa is self-embedding
Example : PalEven = {ww : w  {a, b}*}= The L of even length palindrome of a’s and b’s.
R

L = {, aa, bb, aaaa, abba, baab, bbbb, ........... }


G = {{S, a, b}, {a, b}, R, S}, where:
R = { S  aSa ------------ rule 1
S  bSb ----------- rule 2
S   -------------- rule 3 }.
Example derivation in G:
S ⇒ aSa ⇒ abSba ⇒ abba

Where Context-Free Grammars Get Their Power


If a grammar G is not self-embedding then L(G) is regular. If a language L has the property
that every grammar that defines it is self-embedding, then L is not regular.

More flexible grammar-writing notations


a. Notation for writing practical context-free grammars. The symbol | should be read as
“or”. It allows two or more rules to be collapsed into one.
Example:
SaSb
SbSa can be written as S→aSb |bSa|ε
S  

b. Allow a nonterminal symbol to be any sequence of characters surrounded by angle


brackets.

Example1: BNF for a Java Fragment


<block> ::= {<stmt-list>} | {}
<stmt-list> ::= <stmt> | <stmt-list> <stmt>
<stmt> ::= <block> | while (<cond>) <stmt> |
if (<cond>) <stmt> |
do <stmt> while (<cond>); |
<assignment-stmt>; |
return | return <expression> |
<method-invocation>;
SHREEDEVIPRAMOD,CSE,BRCE 57
Example2: A CFG for C++ compound statements:
<compound stmt>  { <stmt list> }
<stmt list>  <stmt> <stmt list> | epsilon
<stmt>  <compound stmt>
<stmt>  if ( <expr> ) <stmt>
<stmt>  if ( <expr> ) <stmt> else <stmt>
<stmt>  while ( <expr> ) <stmt>
<stmt>  do <stmt> while ( <expr> ) ;
<stmt>  for ( <stmt> <expr> ; <expr> ) <stmt>
<stmt>  case <expr> : <stmt>
<stmt>  switch ( <expr> ) <stmt>
<stmt>  break ; | continue ;
<stmt>  return <expr> ; | goto <id> ;

Example3: A Fragment of English Grammar


Notational conventions used are
• Nonterminal = whose first symbol is an uppercase letter
• NP = derive noun phrase
• VP = derive verb phrase

S  NP VP
NP  the Nominal | a Nominal | Nominal | ProperNoun | NP PP
Nominal  N | Adjs N
N  cat | dogs | bear | girl | chocolate | rifle
ProperNoun  Chris | Fluffy
Adjs  Adj Adjs | Adj
Adj  young | older | smart
VP  V | V NP | VP PP
V  like | likes | thinks | shots | smells
PP  Prep NP
Prep  with

3. Designing Context-Free Grammars

If L has a property that every string in it has two regions & those regions must bear some
relationship to each other, then the two regions must be generated in tandem. Otherwise,
there is no way to enforce the necessary constraint.

SHREEDEVIPRAMOD,CSE,BRCE 58
Example 1: L = {anbncm : n, m ≥ 0} = L = {, ab, c, abc, abcc, aabbc, …….}
The cm portion of any string in L is completely independent of the anbn portion, so we should
generate the two portions separately and concatenate them together.
G = ({S, A, C, a, b, c}, {a, b, c}, R, S} where:
R = { S  AC /* generate the two independent portions
A  aAb |  /* generate the anbn portion, from the outside in
C  cC |  } /* generate the cm portion
Example derivation in G for string abcc:
S ⇒ AC ⇒ aAbC ⇒ abC ⇒ abcC ⇒ abccC ⇒ abcc

Example 2: L={ aibjck : j=i+k, i ,k ≥ 0} on substituting j=i+k ⇒ L = {aibibkck : i, k ≥ 0}


L = {, abbc, aabbbbcc, abbbcc …….}
The aibi portion of any string in L is completely independent of the bkck portion, so we should
generate the two portions separately and concatenate them together.
G = ({S, A, B, a, b, c}, {a, b, c}, R, S} where:
R = { S  AB /* generate the two independent portions
A  aAb |  /* generate the aibi portion, from the outside in
B  bBc |  } /* generate the bkck portion
Example derivation in G for string abbc:
S ⇒ AB ⇒ aAbB ⇒ abB ⇒ abbBc ⇒ abbc

Example 3: L={ aibjck : i=j+k, j ,k ≥0} on substituting i=j+k ⇒ L = {akajbjck : j, k ≥0}


L = {, ac, ab, aabc, aaabcc, …….}
The aibi is the inner portion and akck is the outer portion of any string in L.
G = ({S, A, a, b, c}, {a, b, c}, R, S} where:
R = { S  aSc | A /* generate the akck outer portion
A  aAb |  /* generate the ajbj inner portion}
Example derivation in G for string aabc:
S ⇒ aSc ⇒ aAc ⇒ aaAbc ⇒ aabc

Example 4: L = {anwwR bn: w  {a, b}*} = {, ab, aaab, abbb, aabbab, aabbbbab, ........... }
The anbn is the inner portion and wwR is the outer portion of any string in L.
G = {{S, A, a, b}, {a, b}, R, S}, where:
R = {S  aSb------------- rule 1
S  A---------------- rule 2
A aAa ------------- rule 3
A bAb ------------ rule 4
A   ---------------- rule 5 }.
Example derivation in G for string aabbab:
S ⇒ aSb ⇒ aAb ⇒ aaAab ⇒ aabAbab ⇒ aabbab

SHREEDEVIPRAMOD,CSE,BRCE 59
Example 5: Equal Numbers of a’s and b’s. = {w  {a, b}*: #a(w) = #b(w)}.
L = {, ab, ba, abba, aabb, baba, bbaa, …….}
G = {{S, a, b}, {a, b}, R, S}, where:
R = { S  aSb ---------- rule 1
S  bSa ----------- rule 2
S  SS ------------ rule 3
S   -------------- rule 4 }.
Example derivation in G for string abba:
S ⇒ aSa ⇒ abSba ⇒ abba

Example 6
L = {aibj : 2i = 3j + 1} = {a2b1 , a5b3 , a8b5 ......... }
G = {{S, a, b}, {a, b}, R, S}, where:
aibj 2i = 3j + 1
2 1
ab 2*2= 3*1 + 1 = 4
5 3
ab 2*5= 3*3 + 1 = 10
8 5
ab 2*8= 3*5 + 1 = 16
R={ S  aaaSbb | aab }
Example derivation in G for string aaaaabbb:
S ⇒ aaaSbb ⇒ aaaaabbb

4. Simplifying Context-Free Grammars

Two algorithms used to simplify CFG


a. To find and remove unproductive variables using removeunproductive(G:CFG)
b. To find and remove unreachable variables using removeunreachable(G:CFG)

a. Removing Unproductive Nonterminals:

Removeunproductive (G: CFG) =


1. G = G.
2. Mark every nonterminal symbol in G as unproductive.
3. Mark every terminal symbol in G as productive.
4. Until one entire pass has been made without any new symbol being marked do:
For each rule X   in R do:
If every symbol in  has been marked as productive and X has not
yet been marked as productive then:
Mark X as productive.
5. Remove from G every unproductive symbol.
6. Remove from G every rule that contains an unproductive symbol.
7. Return G.

SHREEDEVIPRAMOD,CSE,BRCE 60
Example: G = ({S, A, B, C, D, a, b}, {a, b}, R, S), where
R={ S  AB | AC
A  aAb | 
B  bA
C  bCa
D  AB }
1) a and b terminal symbols are productive
2) A is productive( because A  aAb )
3) B is productive( because B  bA )
4) S & D are productive(because S  AB & D  AB )
5) C is unproductive
On eliminating C from both LHS and RHS the rule set R obtained is
R = { S  AB A  aAb |  B  bA D  AB }

b. Removing Unreachable Nonterminals


Removeunreachable (G: CFG) =
1. G = G.
2. Mark S as reachable.
3. Mark every other nonterminal symbol as unreachable.
4. Until one entire pass has been made without any new symbol being marked do:
For each rule X  A (where A  V - ) in R do:
If X has been marked as reachable and A has not then:
Mark A as reachable.
5. Remove from G every unreachable symbol.
6. Remove from G every rule with an unreachable symbol on the left-hand side.
7. Return G.

Example
G = ({S, A, B, C, D, a, b}, {a, b}, R, S), where
R = {S  AB
A  aAb | 
B  bA
D  AB }
S, A, B are reachable but D is not reachable, on eliminating D from both LHS and RHS the
rule set R is
R = { S  AB
A  aAb | 
B  bA }

SHREEDEVIPRAMOD,CSE,BRCE 61
5. Proving the Correctness of a Grammar

Given some language L and a grammar G, can we prove that G is correct (ie it generates
exactly the strings in L)
To do so, we need to prove two things:
1. Prove that G generates only strings in L.
2. Prove that G generates all the strings in L.

6. Derivations and Parse Trees

Algorithms used for generation and recognition must be systematic. The expansion order is
important for algorithms that work with CFG. To make it easier to describe such algorithms,
we define two useful families of derivations.
a. A leftmost derivation is one in which, at each step, the leftmost nonterminal in the
working string is chosen for expansion.
b. A rightmost derivation is one in which, at each step, the rightmost nonterminal in the
working string is chosen for expansion.

Example 1 : S → AB | aaB A → a | Aa B → b
Left-most derivation for string aab is S ⇒ AB ⇒ AaB ⇒ aaB ⇒ aab
Right-most derivation for string aab is S ⇒ AB ⇒ Ab ⇒ Aab ⇒ aab

Example 2: SiCtS | iCtSeS | x Cy


Left-most Derivation for string iytiytxex is S ⇒ iCtS ⇒ iytS ⇒ iytiCtSeS ⇒ iytiytSeS ⇒
iytiytxe ⇒ iytiytxex
Right-most Derivation for string iytiytxex is S ⇒ iCtSeS ⇒ iCtSex ⇒ iCtiCtSex ⇒ iCtiCtxex
⇒ iCtiytxex ⇒ iytiytxex

Example 3: A Fragment of English Grammar are


S  NP VP
NP  the Nominal | a Nominal | Nominal | ProperNoun | NP PP
Nominal  N | Adjs N
N  cat | dogs | bear | girl | chocolate | rifle
ProperNoun  Chris | Fluffy
Adjs  Adj Adjs | Adj
Adj  young | older | smart
VP  V | V NP | VP PP
V  like | likes | thinks | shots | smells
PP  Prep NP
Prep  with

SHREEDEVIPRAMOD,CSE,BRCE 62
Left-most Derivation for the string “the smart cat smells chocolate”
S ⇒ NP VP
⇒ the Nominal VP
⇒ the Adjs N VP
⇒ the Adj N VP
⇒ the smart N VP
⇒ the smart cat VP
⇒ the smart cat V NP
⇒ the smart cat smells NP
⇒ the smart cat smells Nominal
⇒ the smart cat smells N
⇒ the smart cat smells chocolate

Right-most Derivation for the string “the smart cat smells chocolate”
S ⇒ NP VP
⇒ NP V NP
⇒ NP V Nominal
⇒ NP V N
⇒ NP V chocolate
⇒ NP smells chocolate
⇒ the Nominal smells chocolate
⇒ the Adjs N smells chocolate
⇒ the Adjs cat smells chocolate
⇒ the Adj cat smells chocolate
⇒ the smart cat smells chocolate

Parse Trees
Regular grammar: in most applications, we just want to describe the set of strings in a language.
Context-free grammar: we also want to assign meanings to the strings in a language, for which
we care about internal structure of the strings. Parse trees capture the essential grammatical
structure of a string. A program that produces such trees is called a parser. A parse tree is an
(ordered, rooted) tree that represents the syntactic structure of a string according to some formal
grammar. In a parse tree, the interior nodes are labeled by non terminals of the grammar, while
the leaf nodes are labeled by terminals of the grammar or .
A parse tree, derived by a grammar G = (V, S, R, S), is a rooted, ordered tree in which:
1. Every leaf node is labeled with an element of ∑ 𝖴{  },
2. The root node is labeled S,
3. Every other node is labeled with some element of: V –∑, and
4. If m is a nonleaf node labeled X and the children of m are labeled x1, x2, …,
xn, then R contains the rule X → x1, x2, …, xn.

SHREEDEVIPRAMOD,CSE,BRCE 63
Example 1: S  AB | aaB A  a | Aa Bb
Left-most derivation for the string aab is S ⇒ AB ⇒ AaB ⇒ aaB ⇒ aab
Parse tree obtained is

Example 2: SiCtS | iCtSeS | x Cy


Left-most Derivation for string iytiytxex isS ⇒ iCtS ⇒ iytS ⇒ iytiCtSeS ⇒ iytiytSeS
⇒ iytiytxeS ⇒ iytiytxex

Example 3: Parse Tree -Structure in English for the string “the smart cat smells
chocolate”. It is clear from the tree that the sentence is not about cat smells or smart cat
smells.

the smart cat smells chocolate

A parse tree may correspond to multiple derivations. From the parse tree, we cannot tell which
of the following is used in derivation:
S ⇒ NP VP ⇒ the Nominal VP ⇒
S ⇒ NP VP ⇒ NP V NP ⇒
Parse trees capture the important structural facts about a derivation but throw away the details
of the nonterminal expansion order. The order has no bearing on the structure we wish to assign
to a string.

Generative Capacity
Because parse trees matter, it makes sense, given a grammar G, to distinguish between:
1. G’s weak generative capacity, defined to be the set of strings, L(G), that G generates,
and
2. G’s strong generative capacity, defined to be the set of parse trees that G generates.
When we design grammar, it will be important that we consider both their weak and their
strong generative capacities.

7. Ambiguity

Sometimes a grammar may produce more than one parse tree for some (or all ) of the strings
it generates. When this happens we say that the grammar is ambiguous. A grammar is
ambiguous iff there is at least one string in L(G) for which G produces more than one parse
tree.

SHREEDEVIPRAMOD,CSE,BRCE 64
Example 1: Bal={w  { ),(}*: the parenthesis are balanced}.
G={{S,),( }, {),(},R,S} where R={ S   S  SS S  (S) }
Left-most Derivation1 for the string (())() is S ⇒ S⇒(S)S ⇒ ((S))S ⇒ (())S ⇒ (())(S) ⇒ (())()
Left-most Derivation2 for the string (())() is S ⇒ SS ⇒SSS ⇒SS ⇒ (S)S ⇒ ((S))S ⇒ (())S
⇒ (())(S) ⇒ (())()

Since both the parse trees obtained for the same string (())() are different, the grammar is ambiguous.

Example 2: S iCtS | iCtSeS | x C y


Left-most Derivation for the string iytiytxex is S ⇒ iCtS ⇒ iytS ⇒ iytiCtSeS ⇒ iytiytSeS ⇒
iytiytxeS ⇒ iytiytxex
Right-most Derivation for the string iytiytxex is S ⇒ iCtSeS ⇒ iCtSex ⇒ iCtiCtSex
⇒iCtiCtxex ⇒ iCtiytxex ⇒ iytiytxex

Since both the parse trees obtained for the same string iytiytxex are different, the grammar is
ambiguous.

Example 3: S  AB | aaB A  a | Aa Bb


Left-most derivation for string aab is S ⇒ AB ⇒ AaB ⇒ aaB ⇒ aab
Right-most derivation for string aab is S ⇒ aaB ⇒ aab

Since both the parse trees obtained for the same string aab are different, the grammar is
ambiguous.

SHREEDEVIPRAMOD,CSE,BRCE 65
Why Is Ambiguity a Problem?
With regular languages, for most applications, we do not care about assigning internal
structure to strings.

With context-free languages, we usually do care about internal structure because, given a
string w, we want to assign meaning to w. It is generally difficult, if not impossible, to assign
a unique meaning without a unique parse tree. So an ambiguous G, which fails to produce a
unique parse tree is a problem.

Example : Arithmetic Expressions


G = (V, , R, E), where
V = {+, *, (, ), id, E},
 = {+, *, (, ), id},
R = {E  E + E, E  E  E, E  (E), E  id }

Consider string 2+3*5 written as id +id*id, left-most derivation for string id +id*id is
E ⇒ E*E ⇒ E+E*E ⇒ id+E*E ⇒ id+id*E ⇒ id+id*id.
Similarly the right-most derivation for string id +id*id is
E ⇒ E+E ⇒ E+E*E ⇒ E+E*id ⇒ E+id*id ⇒ id+id*id.
The parse trees obtained for both the derivations are:-

Should the evaluation of this expression return 17 or 25? Designers of practical languages
must be careful that they create languages for which they can write unambiguous grammars.
Techniques for Reducing Ambiguity
No general purpose algorithm exists to test for ambiguity in a grammar or to remove it when
it is found. But we can reduce ambiguity by eliminating
a.  rules like S → 
b. Rules with symmetric right-hand sides
• A grammar is ambiguous if it is both left and right recursive.
• Fix: remove right recursion
• S → SS or E→E+E
c. Rule sets that lead to ambiguous attachment of optional postfixes.

SHREEDEVIPRAMOD,CSE,BRCE 66
a. Eliminating ε-Rules
Let G =(V, , R, S) be a CFG. The following algorithm constructs a G such that L(G ) =
L(G)-{} and G contains no  rules:
removeEps(G: CFG) =
1. Let G = G.
2. Find the set N of nullable variables in G.
3. Repeat until G contains no modifiable rules that haven’t been processed:
Given the rule PQ, where Q  N, add the rule P if it is not already present
and if    and if P  .
4. Delete from G all rules of the form X  .
5. Return G.

Nullable Variables & Modifiable Rules


A variable X is nullable iff either:
(1) there is a rule X  , or
(2) there is a rule X  PQR… and P, Q, R, … are all nullable.
So compute N, the set of nullable variables, as follows:
2.1. Set N to the set of variables that satisfy (1).
2.2. Until an entire pass is made without adding anything to N do
Evaluate all other variables with respect to (2).
If any variable satisfies (2) and is not in N, insert it.
A rule is modifiable iff it is of the form: P  Q, for some nullable Q.
Example: G = {{S, T, A, B, C, a, b, c}, {a, b, c}, R, S),
R = {S  aTa T  ABC A  aA | C B  Bb | C C  c |  }
Applying removeEps
Step2: N = { C }
Step2.2 pass1: N = { A, B, C }
Step2.2 pass2: N = { A, B, C, T }
Step2.2 pass3: no new element found.
Step2: halts.
Step3: adds the following new rules to G.
{ S  aa
T  AB | BC | AC | A | B | C
Aa
Bb}

The rules obtained after eliminating -rules :


{ S  aTa | aa
T  ABC | AB | BC | AC | A | B | C
A  aA | C | a
B  Bb | C | b
Cc}

SHREEDEVIPRAMOD,CSE,BRCE 67
What If ε ∈ L?
Sometimes L(G) contains  and it is important to retain it. To handle this case the algorithm
used is
atmostoneEps(G: CFG) =
1. G = removeEps(G).
2. If SG is nullable then /* i. e.,   L(G)
2.1 Create in G a new start symbol S*.
2.2 Add to RG the two rules:S*   and S*  SG.
3. Return G.

Example: Bal={w  { ),(}*: the parenthesis are balanced}.

R={ S  SS R={ S  SS R={ S*  


S  (S) S  (S) S*  S
S } S ()} S  SS
S  (S)
S  ( )}

The new grammar built is better than the original one. The string (())() has only one parse
tree.

But it is still ambiguous as the string ()()() has two parse trees?

Replace S  SS with one of:


S  S S1 /* force branching to the left
S  S1S /* force branching to the right

So we get: S*   | S
S  SS1 /* force branching only to the left
S  S1 /* add rule
S1  (S) | ()

SHREEDEVIPRAMOD,CSE,BRCE 68
Unambiguous Grammar for Bal={w  { ),(}*: the parenthesis are balanced}.
G={{S,),( }, {),(},R,S} where
S*  n | S
S  SS1 | S1
S1  (S) | ()
The parse tree obtained for the string ()()() is

Unambiguous Arithmetic Expressions


Grammar is ambiguous in two ways:
a. It fails to specify associatively.
Ex: there are two parses for the string id + id + id, corresponding to the bracketing (id +
id) + id and id + (id + id)
b. It fails to define a precedence hierarchy for the operations + and *.
Ex: there are two parses for the string id + id * id, corresponding to the bracketing (id +
id)* id and id + (id * id)
The unambiguous grammar for the arithmetic expression is:
EE+T
ET
TT*F
TF
F  (E)
F  id
For identical operators: forced branching to go in a single direction (to the left). For
precedence Hierarchy: added the levels T (for term) and F (for factor)
The single parse tree obtained from the unambiguous grammar for the arithmetic expression
is:

SHREEDEVIPRAMOD,CSE,BRCE 69
Proving that the grammar is Unambiguous
A grammar is unambiguous iff for all strings w, at every point in a leftmost derivation or
rightmost derivation of w, only one rule in G can be applied.
S*   ---(1)
S*  S ---(2)
S  SS1 ---(3)
S  S1 ---(4)
S1  (S) ---(5)
S1  () ---(6)
S* ⇒ S ⇒SS1 ⇒SS1S1 ⇒S1S1S1 ⇒ () S1S1 ⇒ () () S1 ⇒ () () ()

Inherent Ambiguity
In many cases, for an ambiguous grammar G, it is possible to construct a new grammar G
that generate L(G) with less or no ambiguity. However, not always. Some languages have the
property that every grammar for them is ambiguous.We call such languages inherently
ambiguous.
Example: L = {aibjck: i, j , k  0, i=j or j=k}.
Every string in L has either (or both) the same number of a’s and b’s or the same number of
b’s and c’s. L = {anbncm: n, m  0}  {anbmcm: n, m  0}.
One grammar for L has the rules:
S  S1 | S2
S1  S1c | A /* Generate all strings in {anbncm}.
A  aAb | 
S2  aS2 | B /* Generate all strings in {anbmcm}.
B  bBc | 
Consider the string a2b2c2 .
It has two distinct derivations, one through S1 and the other through S2
S ⇒ S1 ⇒ S1c⇒ S1cc ⇒ Acc ⇒ aAbcc ⇒ aaAbbcc ⇒ aabbcc
S ⇒ S2 ⇒ aS2 ⇒ aaS2 ⇒ aaB ⇒ aabBc ⇒ aabbBcc⇒ aabbcc
Given any grammar G that generates L, there is at least one string with two derivations in G.

Both of the following problems are undecidable:


• Given a context-free grammar G, is G ambiguous?
• Given a context-free language L, is L inherently ambiguous

SHREEDEVIPRAMOD,CSE,BRCE 70
8. Normal Forms
We have seen in some grammar where RHS is  , it makes grammar harder to use. Lets see
what happens if we carry the idea of getting rid of  -productions a few steps farther. To make
our tasks easier we define normal forms.
Normal Forms - When the grammar rules in G satisfy certain restrictions, then G is said to be
in Normal Form.
• Normal Forms for queries & data can simplify database processing.
• Normal Forms for logical formulas can simplify automated reasoning in AI systems
and in program verification system.
• It might be easier to build a parser if we could make some assumptions about the form
of the grammar rules that the parser will use.
Normal Forms for Grammar
Among several normal forms, two of them are:-
• Chomsky Normal Form(CNF)
• Greibach Normal Form(GNF)

Chomsky Normal Form (CNF)


In CNF we have restrictions on the length of RHS and the nature of symbols on the RHS of
the grammar rules.
A context-free grammar G = (V, Σ, R, S) is said to be in Chomsky Normal Form (CNF), iff
every rule in R is of one of the following forms:
X  a where a   , or
X  BC where B and C  V-
Example: S  AB, A  a, B  b
Every parse tree that is generated by a grammar in CNF has a branching factor of exactly 2
except at the branches that leads to the terminal nodes, where the branching factor is 1.
Using this property parser can exploit efficient data structure for storing and manipulating
binary trees. Define straight forward decision procedure to determine whether w can be
generated by a CNF grammar G. Easier to define other algorithms that manipulates grammars.

Greibach Normal Form (GNF)


GNF is a context free grammar G = (V, , R, S), where all rules have one of the following
forms: X  a where a   and   (V-)*
Example: S aA | aAB, A  a, B  b
In every derivation precisely one terminal is generated for each rule application. This property
is useful to define a straight forward decision procedure to determine whether w can be
generated by GNF grammar G. GNF grammars can be easily converted to PDA with no 
transitions.

SHREEDEVIPRAMOD,CSE,BRCE 71
Converting to Chomsky Normal Form
Apply some transformation to G such that the language generated by G is unchanged.
1. Rule Substitution.
Example: X aYc Y b Y  ZZ equivalent grammar constructed is X abc | aZZc

There exists 4-steps algorithm to convert a CFG G into a new grammar Gc such that: L(G) =
L(Gc) – {}
convertChomsky(G:CFG) =
1. G' = removeEps(G:CFG) S  
2. G'' = removeUnits(G':CFG) AB
3. G''' = removeMixed(G'':CFG) A  aB
4. G'v = removeLong(G''' :CFG) S  ABCD
return Gc

Remove Epsilon using removeEps(G:CFG)


Find the set N of nullable variables in G.
X is nullable iff either X   or (X  A, A  ) : X  
Example1: G: S  aACa
AB|a
BC|c
C  cC | 
Now, since C , C is nullable
since B  C , B is nullable
since A  B , A is nullable
Therefore N = { A,B,C}
removeEps returns G':
S  aACa | aAa | aCa | aa
AB|a
BC|c
C  cC | c

Remove Unit Productions using removeUnits(G:CFG)


Unit production is a rule whose right hand side consists of a single nonterminal symbol.
Ex: A  B. Remove all unit production from G'.
Consider the remaining rules of G'.
G: S  aACa | aAa | aCa | aa
AB|a
BC|c
C  cC | c
Remove A  B But B  C | c, so Add A  C | c
Remove B  C Add B  cC (B  c, already there)
Remove A  C Add A  cC (A  c, already there)

SHREEDEVIPRAMOD,CSE,BRCE 72
removeUnits returns G'' :
S  aACa | aAa | aCa | aa
A  cC | a | c
B  cC | c
C  cC | c

Remove Mixed using removeMixed(G'':CFG)


Mixed is a rule whose right hand side consists of combination of terminals or terminals with
nonterminal symbol. Create a new nonterminal Ta for each terminal a  . For each Ta, add the
rule Ta  a
Consider the remaining rules of G'' :
S  aACa | aAa | aCa | aa
A  cC | a | c
B  cC | c
C  cC | c

removeMixed returns G''' :


S  TaACTa | TaATa | TaCTa | TaTa
A  TcC | a | c
B  TcC | c
C  TcC | c
Ta  a
Tc  c

Remove Long using removeLong(G''' :CFG)


Long is a rule whose right hand side consists of more than two nonterminal symbol.
R: A  BCDE is replaced as: A  BM2
M2 CM3
M3 DE
Consider the remaining rules of G''':
S  TaACTa | TaATa | TaCTa
Now, by applying removeLong we get :
S  TaS1
S1  AS2
S2  CTa
S  TaS3
S3  ATa
S  TaS2

SHREEDEVIPRAMOD,CSE,BRCE 73
Now, by apply removeLong returns G'v :
S  TaS1 | TaS3 | TaS2 | TaTa
S1  AS2
S2  CTa
S3  ATa
A  TcC | a | c
B  TcC | c
C  TcC | c
Ta  a
Tc  c

Example 2: Apply the normalization algorithm to convert the grammar to CNF


G: S  aSa | B
B  bbC | bb
C  cC | 
removeEps(G:CFG) returns
G: S  aSa | B
B  bbC | bb
C  cC | c
removeUnits(G':CFG) returns
G : S  aSa | bbC | bb
B  bbC | bb
C  cC | c
removeMixed(G'':CFG) returns
G''': S  TaSTa | TbTbC | TbTb
B  TbTbC | TbTb
C  TcC | c
Ta a
Tb b
Tc c
removeLong(G''' :CFG) returns
G'v: S  Ta S1 | Tb S2 | TbTb
S 1  S Ta
S 2  Tb C
B  Tb S2 | TbTb
C  Tc C | c
Ta a
Tb b
Tc c

SHREEDEVIPRAMOD,CSE,BRCE 74
Example 3: Apply the normalization algorithm to convert the grammar to CNF
G: S → ABC
A → aC | D
B → bB | ε | A
C → Ac | ε | Cc
D → aa

removeEps(G:CFG) returns
G: S → ABC | AC | AB | A
A → aC | D | a
B → bB | A | b
C → Ac | Cc | c
D → aa

removeUnits(G':CFG) returns
G : S → ABC | AC | AB | aC | aa | a
A → aC | aa | a
B → bB | aC | aa | a | b
C → Ac | Cc | c
D → aa

removeMixed(G'':CFG) returns
G : S → ABC | AC | AB | Ta C | Ta Ta | a
A → Ta C | Ta Ta | a
B → Tb B | Ta C | Ta Ta | a | b
C → A Tc | C Tc | c
D → Ta Ta
Ta a
Tb b
Tc c

removeLong(G''' :CFG) returns


Gv: S → AS1 | AC | AB | Ta C | Ta Ta | a
S1 → BC
A → Ta C | Ta Ta | a
B → Tb B | Ta C | Ta Ta | a | b
C → A Tc | C Tc | c
D → Ta Ta
Ta a
Tb b
Tc c

SHREEDEVIPRAMOD,CSE,BRCE 75
9. Pushdown Automata
An acceptor for every context-free language. A pushdown automata , or PDA, is a finite state
machine that has been augmented by a single stack.
Definition of a (NPDA) Pushdown Automaton
M = (K, S, G, Δ , s, A), where:
K is a finite set of states,
S is the input alphabet,
G is the stack alphabet,
s ∈ K is the initial state,
A ⊆ K is the set of accepting states, and
Δ is the transition relation.

Configuration
A configuration of PDA M is an element of K X S* X G*. Current state, Input that is still left
to read and, Contents of its stack.
The initial configuration of a PDA M, on input w, is (s, w, ).

will be written as cba

If s1s2…sn is pushed onto the stack: the value after the push is s1s2…sncba

SHREEDEVIPRAMOD,CSE,BRCE 76
Yields-in-one-step written |-M relates configuration1 to
A computation by M is a finite sequence of configurations C0, C1, C2,,,,,,,,,,,,,Cn for some n ≥0
such that:
• C0 is an initial configuration,
• Cn is of the form (q, , ), for some q ∈ K and some string in G*, and
• C0 |-M C1 |-M C2 |-M ,,,,,,,,,,,, |-M Cn.

Nondeterminism
If M is in some configuration (q1, s, ) it is possible that:
● Δ contains exactly one transition that matches. In that case, M makes the specified
move.
● Δ contains more than one transition that matches. In that case, M chooses one of
them.
● Δ contains no transition that matches. In that case, the computation that led to that
configuration halts.

Accepting
Let C be a computation of M on input w then C is an accepting configuration
iif C= (s, w, ) |-M* (q, , ), for some q ∈ A.
A computation accepts only if it runs out of input when it is in an accepting state and the stack
is empty.
C is a rejecting configuration iif C= (s, w, ) |-M* (q, w, ),
where C is not an accepting computation and where M has no moves that it can makes from (q,
w, ). A computation can reject only if the criteria for accepting have not been met and there
are no further moves that can be taken.
Let w be a string that is an element of S* . Then:
• M accepts w iif atleast one of its computations accepts.
• M rejects w iif all of its computations reject.
The language accepted by M, denoted L(M), is the set of all strings accepted by M. M rejectsa
string w iff all paths reject it.
It is possible that, on input w, M neither accepts nor rejects. In that case, no path accepts and
some path does not reject.

Transition
Transition ((q1, c, 1), (q2, 2)) says that “If c matches the input and g1 matches the current top
of the stack, the transition from q1 to q2 can be taken. Then, c will be removed from the input,
1 will be popped from the stack, and 2 will be pushed onto it. M cannot peek at the top of the
stack without popping
• If c = , the transition can be taken without consuming any input.
• If 1 = , the transition can be taken without checking the stack or popping anything.
Note: it’s not saying “the stack is empty”.
• If 2 = , nothing is pushed onto the stack when the transition is taken.

SHREEDEVIPRAMOD,CSE,BRCE 77
Example1: A PDA for Balanced Parentheses. Bal={w  { ),(}*: the parenthesis are
balanced}
M = (K, S, G, Δ, s, A),
where:
K = {s} the states
S = {(, )} the input alphabet
 = {(} the stack alphabet
A = {s} the accepting state
Δ = {(s, ( )) -------------------- (1)
((s, ), ( ) , (s, )) --------- (2) }

An Example of Accepting -- Input string = (())()


(S, (() )(), ) |- (S, ())(), ( ) |- (S,))()),(() |- (S, )(), () |- (S, (),) |- (S, ), () |- (S, , )
The computation accepts the string ((())() as it runs out of input being in the accepting state S
and stack empty.
Transition State Unread Stack
input
S (())()
1 S ())() (
1 S ))() ((
2 S )() (
2 S ()
1 S ) (
2 S

Example1 of Rejecting -- Input string = (()))


(S, ())),) |- (S, ())),() |- (S, ))),(() |- (S, )),() |- (S, ), )

Unread
Transition State Stack
input
S (()))
1 S ())) (
1 S ))) ((
2 S )) (
2 S )

The computation has reached the final state S and stack is empty, but still the string is
rejected because the input is not empty.

SHREEDEVIPRAMOD,CSE,BRCE 78
Example2 of Rejecting -- Input string = ((())
Transition State Unread input Stack
S ((()) 
1 S (()) (
1 S ()) ((
1 S )) (((
2 S ) ((
2 S  (

(S, ((()),) |- (S, (()),( |- (S,( )),(() |- (S, )),((() |- (S, ),(() |- (S,  ,()
The computation has reached the final state S and runs out of input, but still the string is
rejected because the stack is not empty.

Example 2: A PDA for AnBn = {anbn: n ≥ 0}


M = (K, S, G, Δ, s, A),
where:
K = {s, f} the states
S = {a, b} the input alphabetΓ
= {a} the stack alphabet
A = {s, f} the accepting state
Δ = { ((s, a, ), (s, a )) -----(1)
((s, b, a ), (f, )) -----(2)
((f, b, a ), (f, )) } -----(3)

An Example of Accepting -- Input string = aabb


(f, aabb, ) |- (f, abb, a ) |- (f, bb, aa) |- (f, b, a) |- (f, , )

The computation has reached the final state f, the input string is consumed and the stack is
empty. Hence the string aabb is accepted.

Example3: A PDA for {wcwR: w ∈ {a, b}*}


M = (K, S, G, Δ, s, A),
where:
K = {s, f} the states
S = {a, b, c} the input alphabet
Γ = {a, b} the stack alphabet
A = {f} the accepting state

SHREEDEVIPRAMOD,CSE,BRCE 79
-----(1)
Δ = {((s, a, ), (s, a)
-----(2)
((s, b, ), (s, b))
-----(3)
((s, c, ), (f, ))
-----(4)
((f, a, a), (f, ))
-----(5)
((f, b, b), (f, ))}

An Example of Accepting -- Input string = abcba


(s, abcba, ) |- (s, bcba, a ) |- (s, cba,ba) |- (f, ba, ba) |- (f, a, a) |- (f, , )
The computation has reached the final state f, the input string is consumed and the stack is
empty. Hence the string abcba is accepted.

Example 4: A PDA for AnB2n = {anb2n: n ≥ 0}


M = (K, S, G, Δ, s, A),
where:
K = {s, f} the states
S = {a, b} the input alphabetΓ
= {a} the stack alphabet
A = {s, f} the accepting state

Δ = { ((s, a, ), (s, aa )) ----- (1)


((s, b, a ), (f, )) -----(2)
((f, b, a ), (f, )) } -----(3)

An Example of Accepting -- Input string = aabbbb


(s, aabbbb, ) |- (s, abbbb, aa ) |- (s, bbbb,aaaa) |- (f, bbb, aaa) |- (f, bb, aa) |-(f, b, a) |- (f, , )

SHREEDEVIPRAMOD,CSE,BRCE 80
10. Deterministic and Nondeterministic PDAs

A PDA M is deterministic iff:


• ΔM contains no pairs of transitions that compete with each other, and
• whenever M is in an accepting configuration it has no available moves.
• If q is an accepting state of M, then there is no transition ((q, e , e) ,(p, a)) for
any p or a.
Unfortunately, unlike FSMs, there exist NDPDA s for which no equivalent DPDA exists.

Exploiting Nondeterministic
Previous examples are DPDA, where each machine followed only a single computational
path. But many useful PDAs are not deterministic, where from a single configuration there
exist multiple competing moves. As in FSMs, easiest way to envision the operation of a
NDPDA M is as a tree.

Each node in the tree corresponds to a configuration of M and each path from the root to a
leaf node may correspond to one computation that M might perform. The state, the stack and
the remaining input can be different along different paths. As a result, it will not be possible
to simulate all paths in parallel, the way we did for NDFSMs.

Example 1: PDA for PalEven ={wwR: w ∈ {a, b}*}.


The L of even length palindrome of a’s and b’s. = {, aa, bb, aaaa, abba, baab, bbbb,............ }
M = (K, S, G, Δ, s, A),
where:
K = {s, f} the states
S = {a, b} the input alphabet
Γ = {a, b} the stack alphabet
A = {f} the accepting state
Δ = {((s, a, ), (s, a)) -----(1)
((s, b, ), (s, b)) -----(2)
((s, , ), (f, )) -----(3)
((f, a, a), (f, )) -----(4)
((f, b, b), (f, ))} -----(5)

SHREEDEVIPRAMOD,CSE,BRCE 81
SHREEDEVIPRAMOD,CSE,BRCE 82
Example 2: PDA for {w  {a, b}* : #a(w) = #b(w)}= Equal Numbers of a’s and b’s.
L = {, ab, ba, abba, aabb, baba, bbaa, …….}
M = (K, S, G, Δ, s, A),
where:
K = {s} the states
S = {a, b} the input alphabet
Γ = {a, b} the stack alphabet
A = {s} the accepting state
Δ ={((s, a, ), (s, a)) -----(1)
((s, b, ), (s, b)) -----(2)
((s, a, b), (s, )) -----(3)
((s, b, a), (s, ))} -----(4)

Example 3: The a Region and the b Region are Different. L = {ambn : m ≠ n; m, n > 0}
It is hard to build a machine that looks for something negative, like ≠. But we can break L
into two sublanguages: {ambn : 0 < n < m} and {ambn : 0 < m < n}
• If stack and input are empty, halt and reject
• If input is empty but stack is not (m > n) (accept)
• If stack is empty but input is not (m < n) (accept)

Start with the case where n = m

Δ = { ((1, a, ), (1, a )) -----(1)


((1, b, a ), (2, )) -----(2)

((2, b, a ), (2, )) } ------ (3)

If input is empty but stack is not (m > n) (accept):

SHREEDEVIPRAMOD,CSE,BRCE 83
Δ = { ((1, a, ), (1, a )) -----(1)
((1, b, a ), (2, )) -----(2)
((2, b, a ), (2, )) -----(3)
((2, , a ), (3, )) -----(4)

((3, , a ), (3, )) }------ (5)

If stack is empty but input is not (m < n) (accept):

Δ = { ((1, a, ), (1, a )) -----(1)


((1, b, a ), (2, )) -----(2)
((2, b, a ), (2, )) -----(3)
((2, b,  ), (4, )) -----(4)

((4, b, ), (4, )) } ------(5)

Putting all together the PDA obtained is


M = (K, S, G, Δ, s, A),
where:
K = {1,2,3,4} the states
S = {a, b} the input alphabet
Γ = {a} the stack alphabet
A = {3,4} the accepting state

Δ = { ((1, a, ), (1, a )) -----(1)


((1, b, a ), (2, )) -----(2)
((2, b, a ), (2, )) -----(3)
((2, , a ), (3, )) -----(4)
((3, , a ), (3, )) } -----(5)
((2, b,  ), (4, )) -----(6)

SHREEDEVIPRAMOD,CSE,BRCE 84
((4, b, ), (4, )) } -----(7)

SHREEDEVIPRAMOD,CSE,BRCE 85
Two problems with this M:
1. We have no way to specify that a move can be taken only if the stack is empty.
2. We have no way to specify that the input stream is empty.
3. As a result, in most of its moves in state 2, M will have a choice of three paths to take.

Techniques for Reducing Nondeterminism


We saw nondeterminism arising from two very specific circumstances:
• A transition that should be taken only if the stack is empty competes against one or
more moves that require a match of some string on the stack.
• A transition that should be taken only if the input stream is empty competes against
one or more moves that require a match against a specific input character.

Case1: A transition that should be taken only if the stack is empty competes against one or
more moves that require a match of some string on the stack.
Problem: Nondeterminism could be eliminated if it were possible to check for an empty
stack.
Solution: Using a special bottom-of-stack marker ( # )
Before doing anything, push a special character onto the stack. The stack is then logically empty
iff that special character (#) is at the top of the stack. Before M accepts a string, its stack must
be completely empty, so the special character must be popped whenever M reaches an
accepting state.

Now the transition back to state 2 no longer competes with the transition to state 4, which can
only be taken when the # is the only symbol on the stack. The machine is still

nondeterministic because the transition back to state 2 competes with the transition to state 3.

SHREEDEVIPRAMOD,CSE,BRCE 86
Case2: A transition that should be taken only if the input stream is empty competes against
one or more moves that require a match against a specific input character.
Problem: Nondeterminism could be eliminated if it were possible to check for an empty input
stream.
Solution: using a special end-of-string marker ($ )
Adding an end-of-string marker to the language to be accepted is a powerful tool for reducing
nondeterminism. Instead of building a machine to accept a language L, build one to accept L$,
where $ is a special end-of-string marker.

Now the transition back to state 2 no longer competes with the transition to state 3, since the
can be taken when the $ is read. The $ must be read on all the paths, not just the one where we
need it.

11. Nondeterminism and Halting

Recall Computation C of a PDA M = (K, S, G, Δ, s, A) on a string w is an accepting

computation iif C= (s, w, ) |-M* (q, , ), for some q  A.


A computation C of M halts iff at least one of the following condition holds:
 C is an accepting computation, or
 C ends in a configuration from which there is no transition in Δ that can be taken.
M halts on w iff every computation of M on w halts. If M halts on w and does not accept,
then we say that M rejects w.
For every CFL L, we’ve proven that there exists a PDA M such that L(M) = L.
Suppose that we would like to be able to:
1. Examine a string and decide whether or not it is L.
2. Examine a string that is in L and create a parse tree for it.
3. Examine a string that is in L and create a parse tree for it in time that is linear in the
length of the string.
4. Examine a string and decide whether or not it is in the complement of L.
For every regular language L, there exists a minimal deterministic FSM that accepts it. That
minimal DFSM halts on all inputs, accepts all strings that are in L, and rejects all strings that
are not in L.

SHREEDEVIPRAMOD,CSE,BRCE 87
But the facts about CFGs and PDAs are different from the facts about RLs and FSMs.
1. There are context-free languages for which no deterministic PDA exists.
2. It is possible that a PDA may
● not halt,
● not ever finish reading its input.
However, for an arbitrary PDA M, there exists M that halts and L(M) = L(M).
There exists no algorithm to minimize a PDA. It is undecidable whether a PDA is minimal.
Problem 2 : Let M be a PDA that accepts some language L. Then, on input w, if w  L then
M will halt and accept. But if w L then, while M will not accept w, it is possible that it will
not reject it either.
Example1: Let S = {a} and consider M =

For L(M) = {a}. The computation (1, a, e) |- (2, a, a) |- (3, e, e) causes M to accept a.
Example2: Consider M =

For L(M) = {aa} or on any other input except a:


(1, aa, e) |- (2, aa, a) |-(1, aa, aa) |- (2, aa, aaa) |- (1, aa, aaaa) |- (2, aa, aaaaa) |- ……..
M will never halt because of one path never ends and none of the paths accepts.
The same problem with NDFSMs had a choice of two solutions.
 Converting NDFSM to and equivalent DFSM using ndfsmtodfsm algorithm.
 Simulating NDFSM using ndfsmsimulate.
Neither of these approaches work on PDAs. There may not even be an equivalent
deterministic PDA.
Solution fall into two classes:

 Formal ones that do not restrict the class of the language that are being considered-
converting grammar into normal forms like Chomsky or Greibach normal form.
 Practical ones that work only on a subclass of the CFLs- use grammars in natural
forms.

12. Alternative Equivalent Definitions of a PDA

PDA M = (K, S, G, Δ , s, A):


1. Allow M to pop and to push any string in G*.
2. M may pop only a single symbol but it may push any number of them.
3. M may pop and push only a single symbol.
M accepts its input w only if , when it finishes reading w, it is in an accepting state and its
stack is empty.

SHREEDEVIPRAMOD,CSE,BRCE 88
There are two alternatives to this:
1. PDA by Final state: Accept if, when the input has been consumed, M lands in an
accepting state, regardless of the contents of the stack.
2. PDA by Empty stack: Accept if, when the input has been consumed, the stack is empty,
regardless of the state M is in.
All of these definitions are equivalent in the sense that, if some language L is accepted by a
PDA using one definition, it can be accepted by some PDA using each of the other definition.
For example:- If some language L is accepted by a PDA by Final state then it can be accepted
by PDA by Final state and empty stack. If some language L is accepted by a PDA by Final
state and empty stack then can be accepted by PDA by Final state.
We can prove by showing algorithms that transform a PDA of one sort into and equivalent
PDA of the other sort.
Equivalence
1. Given a PDA M that accepts by accepting state and empty stack, construct a new
PDA M that accepts by accepting state alone, where L(M) = L(M).
2. Given a PDA M that accepts by accepting state alone, construct a new PDA M that
accepts by accepting state and empty stack, where L(M) = L(M).
Hence we can prove that M and M accept the same strings.
1. Accepting by Final state Alone

Define a PDA M = (K, S, G, Δ , s, A). Accepts when the input has been consumed, M lands
in an accepting state, regardless of the contents of the stack. M accepts if C= (s, w, ) |-M* (q,
, g), for some q  A.
M will have a single accepting state qa. The only way for M to get to qa will be to land in an
accepting state of M when the stack is logically empty. Since there is no way to check that
the stack is empty, M will begin by pushing a bottom-of-stack marker #, onto the stack.

Whenever # is the top symbol of the stack, then stack is logically empty.
The construction proceeds as follows:
1. Initially, let M = M.
2. Create a new start state s.
Add the transition ((s,  , ),(s, #)),
3. For each accepting state a in M do:
Add the transition ((a,  ,#),(qa,  )),
4. Make qa the only accepting state in M
Example:

SHREEDEVIPRAMOD,CSE,BRCE 89
It is easy to see that M lands in its accepting state(qa) iff M lands in some accepting state
with an empty stack. Thus M and M accept the same strings.
2. Accepting by Final state and Empty stack
The construction proceeds as follows:
1. Initially, let M = M.
2. Create a new accepting state F
3. From each accepting state a in M do:
Add the transition ((a,  ,  ),(F,  )),
4. Make F the only accepting state in M
5. For every element g of Γ,
Add the transition to M ((F, , g), (F, )).
In other words, iff M accepts, go to the only accepting state of M′ and clear the stack. Thus
M′ will accept by accepting state and empty stack iff M accepts by accepting state.
Example:-

Thus M and M accept the same strings.

13. Alternatives that are not equivalent to the PDA

We defined a PDA to be a finite state machine to which we add a single stack.


Two variants of that definition, each of which turns out to define a more powerful class of a
machine.
1. First variant: add a first-in, first-out (FIFO) queue in place of a stack. Such machines
are called tag systems or Post machines.
2. Second variant: add two stacks instead of one. The resulting machines are equivalent in
computational power to Turing Machines.

SHREEDEVIPRAMOD,CSE,BRCE 90
Sl.No Sample Questions
1. Define context free grammars and languages.

2. Show a context-free grammar for each of the following languages L:


a) BalDelim = {w : where w is a string of delimeters: (, ), [, ], {, }, that are properly balanced}.
b) {aibj : 2i = 3j + 1}.
c) {aibj : 2i ≠ 3j + 1}.
i
d) {a bjck : i, j, k ≥ 0 and (i ≠ j or j ≠ k)}.
3. Define CFG. Design CFG for the language L={ anbm : n ≠ m}

4. Apply the simplification algorithm to simplify the given grammar


S → AB|AC A → aAb | ε B → bA C →bCa D → AB
5. Prove the correctness of the grammar for the language:
L={w ∈ {a, b}* : #a(w) = #b(w)}.
6. Define leftmost derivation and rightmost derivation. Given the following CFG.
E E + T|T T T*F|F F(E)|a|b|c
Draw parse tree for the following sentences and also derive the leftmost and rightmost derivations
i) (a+b)*c ii) (a) + b*c
7. Consider the following grammar G: S → 0S1 | SS | 10
Show a parse tree produced by G for each of the following strings:
a) 010110
b) 00101101
8. Define ambiguous and explain inherently ambiguous grammars.

9. Prove whether the given grammar is ambiguous grammar or not.


E E + E E E*E|a|b|c
10. Prove that the following CFG is ambiguous SiCtS|iCtSeS|x Cy for the sting iytiytxex

11. Define Chomsky normal form. Apply the normalization algorithm to convert the grammar to
Chomsky normal form.
a. S → aSa S → B B → bbC
B → bb C → ε C → cC

b. S → ABC A → aC | D B → bB | ε | A
C → Ac | ε | Cc D → aa
12. Define Push down automata (NPDA). Design a NPDA for the CFG given in Question (2).

13. Design a PDA for the given language.L$, where L = {w ∈ {a, b}* : #a(w) = #b(w)}.

14. Design a PDA for the language: L={ aibjck : i+j=k ,i>=0,j>=0}

15. Design a PDA for the language L={ anb2n : n>=1}

16. Design a PDA for the language: L={ aibjck : i+k=j ,i>=0,k>=0}

17. Design a PDA for the language: L={ aibjck : k+j=i ,k>=0,j>=0}

SHREEDEVIPRAMOD,CSE,BRCE 91
Module-4
Context-Free and Non-Context-Free Languages
● The language AnBn = {anbn | n≥ 0} is context-free.
● The language AnBnCn ={ anbncn |n≥0} is not context free because a PDA's stack cannot count
all three of the letter regions and compare them.

Where Do the Context-Free Languages Fit in the Big Picture?


THEOREM: The Context-Free Languages Properly Contain the Regular
Languages.

Theorem: The regular languages are a proper subset of the context-free languages.
Proof: We first show that every regular language is context-free. We then show that there
exists at least one context-free language that is not regular.

Every regular language is context-free : We show this by construction.

 If L is regular then it is accepted by some DFSM M = (K, Σ, δ, s, A).


 From M we construct a PDA
M' = (K',Σ’, Γ’,Δ’,s’,A’) to accept L. where Δ' is constructed as follows:
For every transition (qi,c,qj)in δ, add to Δ' the transition ((qi,c,ε),(qi,ε)),So
L(M)=L(M').

So, the set of regular languages is a subset of the CFL.

There exists at least one context-free language that is not regular : The regular languages
are a proper subset the context-free languages because there exists at least one language anbn
that is context –free but not regular.

Theorem: There is a countably infinite number of context-free


languages.
Proof:

Every context-free language is generated by some context-free grammar G = (V,Σ,R,S).

There cannot be more CFLs than CFGs. So there are at most a countably infinite number of context-
free languages. There is not a one-to-one relationship between CFLs and CFGs, since there are an
infinite number of grammars that generate any given language. But we know that, every regular
language is context free and there is a countably infinite number of regular languages.

So there is at least and at most a countably infinite number of CFLs.

Showing That a Language is Context-Free


Two techniques that can be used to show that language L is context-free:

SHREEDEVIPRAMOD,CSE,BRCE 90
• Exhibit a context-free grammar for it.
• Exhibit a (possibly nondeterministic) PDA for it.

Theorem: The length of the yield of any tree T with height h and

branching factor b is <= bh.


Proof:
If h is 1, then a single rule applies. So the longest yield is of length less than or equal to b.
Assume the claim is true for h=n. We show that it is true for h=n+1.
Consider any tree with h=n+1. It consists of a root, and some number of subtrees, each of height
<=n. By the induction hypothesis, the length of the yield of each of those subtrees is <= bn. So the
length of the yield must be <=b.(bn)=bn+1=bh.

The Pumping Theorem for Context-Free languages


Statement: If L is CFL, then: ∃k≥1 (∀strings w∈L, where |w|≥k (∃u,v,x,y,z
(w=uvxyz, vy≠Ɛ, |vxy| ≤ k and ∀q ≥ 0 (uvqxyqz is in L))))
Proof: If L is context-free, then there exists a CFG G=(V,Σ,R,S) with n nonterminal symbols and
branching factor b.

Let k be bn+1.
Any string that can be generated by G and whose parse tree contains no paths with repeated
nonterminals must have length less than or equal to bn. Assuming that b≥2, it must be the case that bn+1
> bn. So let w be any string in L( G) where |w|≥k.
Let T be any smallest parse tree for w. T must have height at least n+1. Choose some path in T of
length at least n + 1. Let X be the bottom-most repeated non terminal along that path. Then w can be
rewritten as uvxyz as shown in below tree,

The tree rooted at [1] has height at most n+1.Thus its yield, vxy, has length less than or equal
to bn+1,which is k. Further, vy≠Ɛ .Since if vy were Ɛ then there would be a sma er parse tree for wand
we choose T so that h at wasn't so.
Finally, v and y can be pumped: uxz must be in L because rule2 could have been used immediately
SHREEDEVIPRAMOD,CSE,BRCE 91
at[1]. And, for any q≥1, uvqxyqz must be in L because rule1 could have been used q times before finally
using rule2.

Application of pumping lemma (Proving Language is Not Context Free)


Ex1: Prove that the Language L = {anbncn| n>=0} is Not Context-Free.
Solution: If L is CFL then there would exist some k such that any string w, where |w|>=k must
satisfy the conditions of the theorem.
Let w = akbkck, where ‘k’ is the constant from the Pumping lemma theorem. For w to satisfy
the conditions of the Pumping Theorem there must be some u,v,x,y and z, such that w=uvxyz, vy≠Ɛ,
|vxy|≤k and ∀q ≥ 0 , uvqxyqz is in L.
w=aaa…aaabbb…bbbccc…ccc, select v and y as follows:
w=aaa…aaabbb…bbbccc…ccc
v y

Let q=2, then


V2 y2
w=aaa…aaabbaabb b..bbccccc…ccc

SHREEDEVIPRAMOD,CSE,BRCE 92
The resulting string will have letters out of order and thus not in L.
So L is not context-free.

Ex 2: Prove that the Language L= {WcW: w∈{a,b}*}is Not Context-Free.


For w to satisfy the conditions of the Pumping Theorem there must be some u,v,x,y,and z, such that
w = uvxyz , vy≠Ɛ, |vxy| ≤ k and ∀q≥0, uvqxyqz is in L. We show that no such u,v,x,y and z exist.
Imagine w divided into five regions as follows:

Call the part before the c the leftside and the part after the c the right side. We consider all the
cases for where v and y could fall and show that in none of them are all the condition so f the theorem met:
• If either v or y overlaps region 3, set q to 0. The resulting string will no longer contain a c and so is
not in WcW.
• If both v and y occur before region 3 or they both occur after region 3, then set q to 2. One side will
be longer than the other and so the resulting string is not in WcW.
• If either v or y overlaps region1 ,then set q to 2. In order to make the right side match. Something
would have to be pumped into region 4. But any v,y pair that did that would violate the requirement
that lvxyl ≤ k.
• If either v or y overlaps region2, then set q to 2. In order to make the right side match, something
would have to be pumped into region 5. But any v,y pair that did that would violate the requirement
that lvxyl ≤ k.
• There is no way to divide w into uvxyz such that all the conditions of the Pumping Theorem are
met . So WcW is not context-free.

Some Important Closure Properties of Context-Free Languages

Theorem: The context- free languages are closed under Union,


Concatenation, Kleene star, Reverse, and Letter substitution.
(1) The context-free languages are closed under union:

• If L1 and L2 are context free languages then there exists a context-free grammar G1= (V1,Σ1, R1,S1)
and G2=(V2,Σ2,R2,S2) such that L1=L(G1) and L2=L(G2).

SHREEDEVIPRAMOD,CSE,BRCE 93
• We will build a new grammar G such that L(G)=L(G1)UL(G2). G will contain all the rules of both
G1 and G2.
• We add to G a new start symbol S and two new rules. S→S1 and S→S2. The two new rules allow G
to generate a string iff at least one of G1or G2 generates it.
So, G = ( V1 U V2 U {S}, Σ1 U Σ2, R1 U R2 U {S→ S1,S→S2}, S )

(2) The context-free languages are closed under concatenation


• If L1 and L2 are context free languages then there exist context-free grammar G1= (V1,Σ1,R1,S1) and
G2=(V2,Σ2,R2,S2) such that L1= L(G1) and L2= L(G2).
• We will build a new grammar G such that L (G) = L(G1)L(G2).
• G will contain all the rules of both G1and G2.
• We add to G a new start symbol S and one new rule. S→S1S2
So G= (V1UV2U{S}, Σ1 U Σ2, R1UR2U{S→S1S2),S)

(3) The context-free Languages are closed under Kleene star:


• If L1 is a context free language then there exists a context-free grammar G1=(V1,Σ1,R1,S1) such that
L1= L(G1).
• We will build a new grammar G such that L(G)=L(G1)* G will contain all the rules of G1.
• We add to G a new start symbol S and two new rules. S→ℇ and S→SS1
So G = (V1 U {S}, Σ1, R1U {S→ℇ, S→SS1), S )

(4) The context-free languages are closed under reverse


• If L is a context free language then it is generated by some Chomsky Normal Form from grammar
G= (V,Σ,R, S).

• Every rule in G is of the form X→BC or X→a, where X, B, and C are elements of (V-Σ) and a ϵΣ
• So construct, from G, a new grammar G1 , Such that L(G1)= LR .
• G1= (VG, ΣG, R', SG) , Where R' is constructed as follows:
 For every rule in G of the form X→BC, add to R' the rule X→CB
 For every rule in G of the form X→ a then add to R' the rule X→ a

(5) The context-free languages are closed under letter Substitution


• Consider two alphabets Σ1 and Σ2 .
• Let sub be any function from Σ1 to Σ2*.

SHREEDEVIPRAMOD,CSE,BRCE 94
• Then letsub is a letter substitution function from L1 to L2 iff letsub(L1) ={ w∈ Σ2*: ∃y ∈ L1 (w=y
except that every character c of y has replaced by sub(c))}.
Example : Let y= VTU ∈ L1 And sub(c) is given as : sub(V) = Visvesvaraya
sub(T) = Technological
sub(U)= University
Then , sub(VTU) = Visvesvaraya Technological University

Closure Under Intersection, Complement, and Difference


Theorem:The Context-free language are not closed under intersection,
complement or difference.
1) The context-free languages are not closed under intersection
The proof is by counter example. Let: L1={anbncm|n,m≥0} L2={ambncn|n,m≥0} Both L1and L2
are context-free since there exist straight forward CFGs for them.
But now consider: L =L1∩L2= { anbncn | n,m≥0}. If the context-free languages were closure under
intersection. L would have to be context-free. But we have proved that L is not CFG by using pumping
lemma for CFLs.

(2) The context-free languages are not closure under


Given any sets L1and L2 , L1∩ L2 = ¬(¬L1𝖴¬L2)
• The context-free languages are closed under union.
• But we just showed that they are not, thus they are not closed under complement either.
• So, if they were also closed under complement, they would necessarily be closed under
intersection.

(3) The context-free languages are not closed under difference


(subtraction) :
Given any language L and ¬L=Σ*- L.
Σ* is context-free So, if the context-free languages were closed under difference, the complement of
any CFL would necessarily be context-free But we just showed that is not so.

Closure Under Intersection With the Regular Languages


Theorem: The context-free languages are closed under intersection with the
regular languages.
Proof: The proof is by construction.

SHREEDEVIPRAMOD,CSE,BRCE 95
• If L1 is context-free, then there exists some PDA M1= (K1,Σ,𝛤1,𝚫1,S1,A1) that accepts it.

• If L2 is regular then there exists a DFSM M2= (K2,Σ,𝛿,S2,A2) that accepts it.

• We construct a new PDA, M3 that accepts L1 ∩ L2. M3 will work by simulating the parallel
execution of M1 and M2.

• M3= (K1X K2, Σ , 𝛤1, 𝚫3,(S1,S2),A1X A2), Where 𝚫3 is built as follows:

• For each transition ((q1, a, 𝛽) ,(p1, 𝗒)) in 𝚫1 and each transition ((q2, a ) ,p2) in 𝛿, add 𝚫3 the
transition: (((q1,q2),a,𝛽) ,((p1,p2), 𝗒)).
• For each transition ((q1,ℇ,𝛽) ,(p1, 𝗒)) in 𝚫1and each state q2 in k2, add to 𝚫3 the transition:
(((q1,q2),ℇ,𝛽) ,((p1,p2), 𝗒)).

Closure Under Difference with the Regular Language.


Theorem: The difference (L1-L2) between a context-free language L1 and a
regular language L2is context-free.
Proof: L1-L2= L1∩¬L2
• If L2 is regular, then, since the regular languages are closed under complement,¬L2 is also
regular.
• Since L1 is context-free, by Theorem we already proved that L1∩ ¬L2 is context-free.

Using the Pumping Theorem in Conjunction with the Closure


Properties
Languages that impose no specific order constraints on the symbols contained in their strings
are not always context-free. But it may be hard to prove that one isn't just by using the Pumping
Theorem. In such a case it is proved by considering the fact that the context-free languages areclosed
under intersection with the regular languages.

Deterministic Context-Free Languages


The technique used to show that the regular languages are closed under complement starts with a given
(possibly nondeterministic ) FSM M1, we used the following procedure to construct a newFSM
M2 such that L(M2)=¬L(M1):

SHREEDEVIPRAMOD,CSE,BRCE 96
The regular languages are closed under complement, intersection and difference. Why are the context-
free languages different? Because the machines that accept them may necessarily be nondeterministic.
1. From M1, construct an equivalent DFSM M', using the algorithm ndfsmtodfsm, presented in the
proof of Theorem5.3. (If M1 is already deterministic. M'=M1.)
2. M' must be stated completely. so if it is described with an implied dead state, add the dead state and
all required transitions to it.
3. Begin building M2 by setting it equal to M'. Then swap the accepting and the non-accepting states.
So M2 M'= (KM',.Σ,𝜹M',sM',KM'-AM').
We have no PDA equivalent of ndfstodfsm because there provably isn't one. We defined a PDA M
to be deterministic iff:
•ΔM contains opairs of transitions that compete with each other, and
• if q is an accepting state of M, then there is no transition ((q,ε,ε),(p,a)) for any p or a.

What is a Deterministic Context-Free language?


 Let $ be an end-of-string marker. A language L is deterministic context-free iff L$ can be
accepted by some deterministic PDA.

EXAMPLE: Why an End-of-String Marker is Useful


Let L= a* 𝖴 { anbn| n>0}
Consider any PDA M that accepts L. When it begins reading a’s, M must push them onto the
stack in case there are going to be b's. But if it runs out of input without seeing b's, it needs a way to
pop those a's from the stack before it can accept. Without an end-of-string marker, there is no way to
allow that popping to happen only when all the input has been read.

For example, the PDA accepts L, but it is nondeterministic because the transition to state3 (where the
a's will be popped) can compete with both of the other transitions from state1.

SHREEDEVIPRAMOD,CSE,BRCE 97
With an end-of-string marker, we can build the deterministic PDA, which can only take the transition
to state3, the a-popping state. When it sees the $:

NOTE: Adding the end-of-string marker cannot convert a language that was not
context-free into one that is.

CFLs and Deterministic CFLs


Theorem: Every deterministic context-free language is context-free.
Proof:
If L is deterministic context-free, then L$ is accepted by some deterministic PDA M=(K,Σ,𝐹,,s,A) .
From M, we construct M' such that L (M') = L. We can define the following procedure to construct M':
without$(M:PDA)=
1.Initially. set M' to M.
/*Make the copy that does not read any input.
2.For every state q in M, add to M' a new state q'.
3.For every transition ((q, ε ,𝜸1),(p,𝜸2)) in M do:
3.1. Add to M the transition ((q',ε,𝜸1),(p',𝜸2)).
/*Link up the two copies.
4. For every transition ((q,$,𝜸1),(p,𝜸2)) in M do:
4.1. Add to M’ the transition ((q,ε,𝜸1),(p',𝜸2)).
4.2. Remove ((q,$,𝜸1),(p,𝜸2)) from M’
/*Set the accepting state s of M'.
5.AM' = {q':q∈A}.

SHREEDEVIPRAMOD,CSE,BRCE 98
Closure Properties of the Deterministic Context-Free
Languages
1) Closure Under Complement
Theorem: The deterministic context-free languages are closed under
complement.
Proof: The proof is by construction. If L is a deterministic context-free language over the alphabet Σ,
then L$ is accepted by some deterministic PDA M = (K, Σ U{$}, 𝐹, , s, A).
We need to describe an algorithm that constructs a new deterministic PDA that accepts (¬L)$.
We defined a construction that proceeded in two steps:
• Given an arbitrary FSM, convert it to an equivalent DFSM, and then
• Swap accepting and non accepting states.
A deterministic PDA may fail to accept an input string w for any one of several reasons:
1. Its computation ends before it finishes reading w.
2. Its computation ends in an accepting state but the stack is not empty.
3.Its computation loops forever, following ε-transitions, without ever halting in either an
accepting or a non accepting state.
4. Its computation ends in a non accepting state.
If we simply swap accepting and non accepting states we will correctly fail to accept every string that
M would have accepted (i.e., every string in L$). But we will not necessarily accept every string in (
¬L)$. To do that, we must also address issues 1through 3 above.

An additional problem is that we don't want to accept ¬L(M). That includes strings that do not end
in $. We must accept only strings that do end in $ and that are in (¬L)$.

2) Non closure Under Union


Theorem: The deterministic context-free languages are not closed under union.
Proof: We show a counter example:
Let, L1={ aibjck| i,j,k ≥0 and i≠ j } and L2={ aibjck | i,j,k≥0 and j ≠ k}
Let, L' = L1U L2= { aibjck | i,j,k≥0 and ( ( i≠ j) and ( j ≠ k) ) }.
Let, L" = ¬L'.

SHREEDEVIPRAMOD,CSE,BRCE 99
={ aibjck |i,j,k≥0 and (i=j=k)} U {w∈{a,b,c}*: the letters are out of order}.
Let, L"'=L"∩a*b*c* = {anbncn | n ≥ 0}
But L'" is AnBnCn={ anbncn |n≥0},which we have shown is not context-free.

3) Non Closure Under Intersection


Theorem: The deterministic context-free languages are not closed under inter
section.
Proof: We show a counter example:
Let, L1= { aibjck | i,j,k≥0 and i= j }and L2={ aibjck |i,j,k≥0 and j=k}
Let, L' = L1∩ L2 ={ anbncn | n ≥ 0}
L1 and L2 are deterministic context-free. The
deterministic PDA shown accepts L1$, A similar one
accepts L2. But we have shown that their intersection L'
is not context-free much less deterministic context-free.

A hierarchy within the class of context-free languages


Some CFLs are not Deterministic
Theorem: The class of deterministic context-free languages is a proper subset of
the class of context-free languages. Thus there exist nondeterministic PDAs for
which no equivalent deterministic PDA exists.
Proof: We show that there exists at least one context-free language that is not deterministic
context-free.
Consider L = { aibjck |i,j,k ≥ 0 and ( ( i≠ j) or ( j ≠ k) ) }. L is context-free.
If L were deterministic context-free, then, its complement
L'={ aibjck |i,j,k ≥ 0 and (i=j=k) } U {w∈{a,b,c}*:the letters are out of order}
Would also be deterministic context-free and thus context-free. If L' were context-free, then
L"=L'∩ a*b*c* would also be context-free (since the context-free languages are closed under inter
section with the regular languages).
But L"= AnBnCn ={anbncn|n≥0},which is not context free. So
L is context-free but not deterministic context-free.

SHREEDEVIPRAMOD,CSE,BRCE 100
Since L is context-free, it is accepted by some (non deterministic) PDA M. M is an example
of an on deterministic PDA for which no equivalent deterministic PDA L exists. If such a deterministic
PDA did exist and accept L, it could be converted into a deterministic PDA that accepted L$. But, if
that machine existed. L would be deterministic context-free and we just showed that it is not.

Inherent Ambiguity versus Non determinism


There are context-free languages for which unambiguous grammars exist and there are others
that are inherently ambiguous, by which we mean that every corresponding grammar is ambiguous.
Example:
The language L1= {aibjck | i, j, k ≥ 0 and ( (i= j) or ( j = k) )} can also be described as
{anbncm|n,m ≥0}U{ anbmcm |n,m ≥0}. L1 is inherently ambiguous because every string that is also in
AnBnCn ={anbncn|n≥0} is an element of both sub languages and so has at least two derivations in any
grammar for L1.
• Now consider the language L2={anbncmd|n,m≥0}U{anbmcme| n,m≥0} is not inherently ambiguous.
• Any string in is an element of only one of them (since each such string must end in d or e but not
both).
There exists no PDA that can decide which of the two sublanguages a particular string is in until it
has consumed the entire string.
What is the relationship between the deterministic context-free languages and the
languages that are not inherently ambiguous?
The answer is shown in below Figure.

There exist deterministic context-free languages that are not


regular. One example is AnBn={anbn|n,m≥0}.

• Thereexist context-free languages and not inherently


ambiguous. Examples:

(a) PalEven={wwR:w∈{a,b}*}.

(b) {anbncmd|n,m≥0}U{anbmcme|n,m≥0}.

• There exist languages that are in the outer donut because they are inherently ambiguous. Two
examples are:
 {aibjck|i,j,k ≥ 0 and ((i≠j)or(j≠k))}
 {aibjck|i,j,k ≥ 0 and ((i=j) or (j=k))}

SHREEDEVIPRAMOD,CSE,BRCE 101
Regular Language is Deterministic Context-Free
Theorem: Every regular language is deterministic context-free.
Proof: The proof is by construction. {$} is regular. So, if L is regular then so is L$ (since the regular
languages are closed under concatenation).So there is a DFSM M that accepts it. Using the construction
to show that every regular language is context-free Construct, from M a PDA P that accepts L$. P will
be deterministic.

Every Deterministic CFL has an Unambiguous Grammar


Theorem: For every deterministic context-free language there exists an
unambiguous grammar.
Proof: If a language L is deterministic context-free, then there exists a deterministic PDA M that
accepts L$. We prove the theorem by construction of an unambiguous grammar G such that L(M)=
L(G). We construct G as follows:
The algorithm PDAtoCFG proceeded in two steps:
1. Invoke convenPDAtorestricted(M) to build M', an equivalent PDA in restricted normal form.
2. Invoke buildgrammar(M'), to build an equivalent grammar G
So the construction that proves the theorem is:

buildunambiggrammar(M:deterministicPDA)=
1. Let G=buildgrammar(convertPDAtodetnormalform(M)).
2. Let G' be the result of substituting ε for $ in each rule in which $ occurs.
3. Return G'.

NOTE: The algorithm convertPDAtodetnormalform, is described in the theorem that proves the
deterministic context-free languages are closed under complement.

The Decidable Questions


Membership

"Given a language L and a string w, is w in L?'

This question can be answered for every context-free language and for every context-free
language L there exists a PDA M such that M accepts L. But existence of a PDA that accepts L does
not guarantee the existence of a procedure that decides it.

SHREEDEVIPRAMOD,CSE,BRCE 102
It turns out that there are two alternative approaches to solving this problem, both of which
work:

● Use a grammar: Using facts about every derivation that is produced by a grammar in Chomsky
normal form, we can construct an algorithm that explores a finite number of derivation paths
and finds one that derives a particular string w iff such a path exists.
● Use a PDA : While not all PDAs halt, it is possible, for any context-free language L, to craft
a PDA M that is guaranteed to halt on all inputs and that accepts all strings in L and rejects all
strings that are not in L.

Using a Grammar to Decide

Algorithm for deciding whether a string w is in a language L:

decideCFLusingGrammar(L: CFL,w: string) =

1. If L is specified as a PDA, use PDA to CFG, to construct a grammar G such that L(G) =L (M).

2. If L is specified as a grammar G, simply use G.

3. If w =ε then if SG is nullable then accept, otherwise reject.

4. If w ≠ ε then:

4.1. From G, construct G' such that L (G') = L(G)-{ε} and G' is in Chomsky normal form.

4.2. If G derives to, it does so in (2 • |w| - 1) steps. Try all derivations in G of that number

of steps. If one of them derives w, accept. Otherwise reject.

Using a PDA to Decide


A two-step approach.
 We first show that, for every context-free language L, it is possible to build a PDA that
accepts L-{ε} and that has no ε-transitions.
 Then we show that every PDA with no ε-transitions is guaranteed to halt

Elimination of ε-Transitions
Theorem: Given any context-free grammar G=(V,Σ,R,S), there exists a PDA M
such that L(M)=L(G)-{ε} and M contains no transitions of the form

SHREEDEVIPRAMOD,CSE,BRCE 103
((q1,ε,α),(q2,𝜷)). In other words, every transition reads exactly one input
character.
Proof: The proof is by a construction that begins by converting G to Greibach normal form. Now
consider again the algorithm cfgtoPDAtopdown, which builds, from any context-free grammar G, a
PDA M that, on input w, simulates G deriving w, starting from S.
M= ({p,q},Σ,V,Δ, p,{q}), where Δ contains:
1. The start-up transition ((p,ε,ε),(q,S)), which pushes the start symbol on to the stack and
goes to state q.
2. For each rule X→s1s2...sn, in R, the transition ((q,ε,X),(q,s1s2...sn)), which replaces X by
s1s2...sn. If n=0 (i.e., the right-hand side of the rule is ε), then the transition ( (q, ε, X), (q, ε)).
3. For each character c ∈ Σ. the transition ((q, c, c), (q,ε)), which compares an expected
character from the stack against the next input character.
If G contains the rule X→cs2...sn, (where c ∈Σ and s2 through sn, are elements of V-Σ), it is not
necessary to push c onto the stack, only to pop it with a rule from step 3.
Instead, we collapse the push and the pop into a single transition. So we create a transition that can
be taken only if the next input character is c. In that case, the string s2 through sn is pushed onto the
stack.
Since terminal symbols are no longer pushed onto the stack. We no longer need the transitions created
in step3 of the original algorithm.
So, M=({p,q},Σ,V,Δ,p,{q}), where Δ contains:
1. The start-up transitions: For each rule S→cs2...sn the transition ((p,c,ε),(q,s2...sn)).
2. For each rule X→cs2...sn (where c∈Σ and s2 through sn, are elements of V-Σ), the
transition ((q,c,X),(q,s2...sn)).
cfgtoPDAnoeps(G:context-freegrammar)=
1. Convert G to Greibach normal form, producing G'.
2. From G' build the PDA M described above.

Halting Behavior of PDAs Without ε-Transitions


Theorem: Let M be a PDA that contains no transitions of the form ((q 1,ε,s1),(q2,s2)). i.e., no ε-
transitions. Consider the operation of M on input w∈ Σ*. M must halt and either accept or reject w.
Let n=|w|.
We make three additional claims:
a) Each individual computation of M must halt within n steps.

SHREEDEVIPRAMOD,CSE,BRCE 104
b) The total number of computations pursued by M must be less than or equal to bn, where b
is the maximum number of competing transitions from any state in M.
c) The total number of steps that will be executed by all computations of M is bounded by nbn

Proof:

a) Since each computation of M must consume one character of w at each step and M will halt when
it runs out of input, each computation must halt within n steps.
b) M may split into at most b branches at each step in a computation. The number of steps in a
computation is less than or equal to n. So the total number of computations must be less than or equal
to bn.
c) Since the maximum number of computations is bn and the maximum length of each is n, the
maximum number of steps that can be executed before all computations of M halt is nbn.
So a second way to answer the question, "Given a context-free language L and a string w, is w
in L?" is to execute the following algorithm:
decideCFLusingPDA(L:CFL,w:string)=
1. If L is specified as a PDA, use PDAtoCFG, to construct a grammar G such that L(G)=L(M).
2. If L is specified as a grammar G, simply use G.
3. If w=ε then if SG is nullable then accept, otherwise reject.
4.If w≠ε then:
4.1. From G, construct G' such that L(G')=L(G)-{ε} and G' is in Greibach normal form.
4.2. From G' construct, using cfgtoPDAnoeps, a PDA M' such that L(M')=L(G') and M' has
no ε-transitions.
4.3.We have proved previously that, all paths of M' are guaranteed to halt within a finite
number of steps. So run M' on w, Accept if M' accepts and reject otherwise.

Emptiness and Finiteness


Decidability of Emptiness and Finiteness
Theorem: Given a context-free language L. There exists a decision procedure that answers each of
the following questions:
1. Given a context-free language L, is L=⦰?
2. Given a context-free language L, is L infinite?
Since we have proven that there exists a grammar that generates L iff there exists a PDA that accepts
it. These questions will have the same answers whether we ask them about grammars or about PDAs.

SHREEDEVIPRAMOD,CSE,BRCE 105
Proof :
decideCFLempty( G: context-free grammar) =
1. Let G' =removeunproductive(G).
2. If S is not present in G' then return True else return False.
decideCFLinfinite(G:context-freegrammar)=
1. Lexicographically enumerate all strings in Σ* of length greater than bn and less than or
equal to bn+1+bn.
2. If, for any such string w, decideCFL(L,w) returns True then return True. L is infinite.
3. If, for all such strings w, decideCFL(L,w) returns False then return False. L is not infinite.

The Undecidable Questions


• Given a context-free language L, is L=Σ*?
• Given a CFL L, is the complement of L context-free?
• Given a context-free language L, is L regular?
• Given two context-free languages L1 and L2 is L1=L2?
• Given two context-free languages L1 and L2, is L1⊆ L2?
• Given two context-free languages L1and L2, is L1∩L2=⦰?
• Given a context-free language L, is L inherently ambiguous?
• Given a context-free grammar G, is G ambiguous?

TURING MACHINE
The Turing machine provides an ideal theoretical model of a computer. Turing machines are useful
in several ways:
• Turing machines are also used for determining the undecidability of certain languages and
• As an automaton, the Turing machine is the most general model, It accepts type-0
languages.
• It can also be used for computing functions. It turns out to be a mathematical model of
partial recursive functions.
• Measuring the space and time complexity of problems.
Turing assumed that while computing, a person writes symbols on a one-dimensional paper (instead
of a two dimensional paper as is usually done) which can be viewed as a tape divided into cells. In

SHREEDEVIPRAMOD,CSE,BRCE 106
Turing machine one scans the cells one at a time and usually performs one of the three simple
operations, namely:
(i) Writing a new symbol in the cell being currently scanned,
(ii) Moving to the cell left of the present cell, and
(iii) Moving to the cell right of the present cell.

Turing machine model

• Each cel can store only one symbol.


• The input to and the output from the finite state automaton are affected by the R/W head which can
examine one cell at a time.
In one move, the machine examines the present symbol under the R/W head on the tape and the
present state of an automaton to determine:
(i) A new symbol to be written on the tape in the cell under the R/W head,
(ii) A motion of the R/W head along the tape: either the head moves one cell left (L),or one
cell right (R).
(iii) The next state of the automaton, and
(iv) Whether to halt or not.
Definition:
Turing machine M is a 7-tuple, namely (Q, Σ, 𝚪, , q0, b, F), where
1. Q is a finite nonempty set of states.
2. 𝚪 is a finite nonempty set of tape symbols,
3. b∈ 𝚪 is the blank.
4. Σ is a nonempty set of input symbols and is a subset o f 𝚪 and b∉Σ.

SHREEDEVIPRAMOD,CSE,BRCE 107
5. is the transition function mapping (q,x) onto (q',y,D) where D denotes the direction of
movement of R/W head; D=L orR according as the movement is to the left or right.
6. q0∈ Q is the initial state, and
7. F⊆Q is the set of final states.

Notes:

(1) The acceptability of a string is decided by the reachability from the initial state to some final state.
(2) may not be defined for some elements of QX 𝚪.

REPRESENTATION OF TURINGMACHINES
We can describe a Turing machine employing
(i) Instantaneous descriptions using move-relations.
(ii) Transition table, and
(iii) Transition diagram (Transition graph).
REPRESENTATION BY INSTANTANEOUS DESCRIPTIONS
Definition: An ID of a Turing machine M is a string 𝛼𝛽𝛾, where 𝛽 is the present state of M, the
entire input string is split as 𝛼𝛾, the first symbol of 𝛾 is the current symbol a under the R/W head and
𝛾 has all the subsequent symbols of the input string, and the string 𝛼 is the substring of the input
string formed by all the symbols to the left of a.
EXAMPLE: A snapshot of Turing machine is shown in below Fig. Obtain the instantaneous
description.

The present symbol under the R/W


head is a1. The present state is q3. So a1 is written to the right of q3 The nonblank symbols to the left of
al form the string a4a1a2a1a2a2, which is written to the left of q3. The sequence of nonblank symbols to
the right of a1 is a4a2. Thus the ID is as given in below Fig.

SHREEDEVIPRAMOD,CSE,BRCE 108
Notes: (1) For constructing the ID, we simply insert the current state in the input string to the left of
the symbol under the R/W head.
(2) We observe that the blank symbol may occur as part of the left or right substring.

REPRESENTATION BY TRANSITION TABLE

We give the definition of in the form of a table called the transition table If (q, a)=(𝛾,𝛼,𝛽). We write
𝛼𝛽𝛾 under the 𝛼-column and in the q-row. So if we get 𝛼𝛽𝛾 in the table, it means that 𝛼 is written in
the current cell, 𝛽 gives the movement of the head (L or R) and 𝛾 denotes the new state into which
the Turing machine enters.
EXAMPLE:
Consider, for example, a Turing machine with five states q1,...,q5 where q1 is the initial state and q5 is the
(only) final state. The tape symbols are 0,1and b. The transition table given below describes :

REPRESENTATION BY TRANSITION DIAGRAM (TD)


The states are represented by vertices. Directed edges are used to represent transition of states.
The labels are triples of the form (𝛼,𝛽,𝛾)where 𝛼,𝛽∈𝚪and𝛾∈{L,R}.When there is a directed edge
from qi to qj with label (𝛼,𝛽,𝛾),it means that (qi,𝛼)=(qj,𝛽,𝛾).
EXAMPLE:

SHREEDEVIPRAMOD,CSE,BRCE 109
LANGUAGE ACCEPTABILITY BY TURING MACHINES
Let us consider the Turing machine M=(Q,Σ,𝚪,,q0,b,F). A string w in Σ* is said to be
accepted by M, if q0w 𝛼1p𝛼2 for some P∈F and 𝛼1,𝛼2∈𝚪*.
EXAMPLE: Consider the Turing machine M described by the table below

IDs for the strings (a) 011 (b)0011 (c)001

As (q5,1) is not defined, M halts; so the input string 011 is not accepted

M halts. As q6 is an accepting state, the input string 0011is accepted byM.

SHREEDEVIPRAMOD,CSE,BRCE 110
M halts. As q2 is not an accepting state,001 is not accepted by M.

DESIGN OF TURING MACHINES


Basic guidelines for designing a Turing machine:
1. The fundamental objective in scanning a symbol by the R/W head is to know what to do in the
future. The machine must remember the past symbols scanned. The Turing machine can remember
this by going to the next unique state.
2. The number of states must be minimized. This can be achieved by changing the states only when
there is a change in the written symbol or when there is a change in the movement of the R/W head.
EXAMPLE 1
Design aTuring machine to recognize all strings consisting of an even number of 1's.
Solution:
The construction is made by defining moves in the following manner:
(a) ql is the initial state. M enters the state q2 on scanning 1 and writes b.
(b) If M is in state q2 and scans 1, it enters ql and writes b.
(c) ql is the only accepting state.
Symbolically M= ({ql,q2},{1,b},{1,b},,q,b,{ql}), Where is defined by ,

SHREEDEVIPRAMOD,CSE,BRCE 111
EXAMPLE 2: Design a TM that accepts {0n1n| n≥ 0}
Solution: We require the following moves:
(a) If the leftmost symbol in the given input string w is 0, replace it by x and move right till we
encounter a leftmost 1in w. Change it to y and move backwards.
(b) Repeat (a) with the leftmost 0. If we move back and forth and no 0 or 1 remains. Move to a final
state.
(c) For strings not in the form 0n1n, the resulting state has to be non-final.
we construct a TM M as follows:M = (Q, Σ,𝚪, , q0,b, F)
Q = {q0,q1,q2,q3,qf}
F = {qf}
Σ = { 0,1}
𝚪= { 0,1,x,y,b}

Computation sequence of 0011:

q4 is final state, hence 0011 is accepted by M.


TECHNIQUES FOR TM CONSTRUCTION
1. TURING MACHINE WITH STATIONARY HEAD
Suppose, we want to include the option that the head can continue to be in the same cell for
some input symbol. Then we define (q,a) as (q',y,S).This means that the TM, on reading the input
symbol a, changes the state to q' and writes y in the current cell in place of a and continues to remain
in the same cell. In this model (q, a) =(q', y, D) where D = L, R or S.

SHREEDEVIPRAMOD,CSE,BRCE 112
2. STORAGE IN THE STATE
We can use a state to store a symbol as well. So the state becomes a pair(q,a) where q is the
state and a is the tape symbol stored in (q, a). So the new set of states becomes Qx𝚪.
EXAMPLE: Construct a TM that accepts the language 0 1* + 1 0*.
We have to construct a TM that remembers the first symbol and checks that it does not
appear afterwards in the input string.
So we require two states, q0, q1. The tape symbols are 0,1 and b. So the TM, having the 'storage
facility in state‘, is M=({q0,q1}X{0,1,b},{0,1},{0,1,b},,[q0,b],[q1,b]})
We describe by its implementation description.
1. In the initial state, M is in q0 and has b in its data portion. On seeing the first symbol of the input
sting w, M moves right, enters the state q1 and the first symbol, say a, it has seen.
2. M is now in [q1,a].
(i) If its next symbol is b, M enters [q1,b], an accepting state.
(ii) If the next symbol is a, M halts without reaching the final state (i.e. is not defined).
(iii) If the next symbol is ā, (ā=0 if a=1 and ā=1 if a=0), M moves right without changing
state.
3. Step2 is repeated until M reaches [q1,b] or halts ( is not defined for an input symbol in w).
3. MULTIPLE TRACK TURING MACHINE
In a multiple track TM, a single tape is assumed to be divided into several tracks. Now the tape
alphabet is required to consist of k-tuples of tape symbols, k being the number of tracks. In the case of
the standard Turing machine, tape symbols are elements of r; in the case of TM with multiple tracks,
it is 𝚪k.
4. SUBROUTINES
First a TM program for the subroutine is written. This will have an initial state and a 'return'
state. After reaching the return state, there is a temporary halt for using a subroutine, new states are
introduced. When there is a need for calling the subroutine, moves are effected to enter the initial state
for the subroutine. When the return state of the subroutine is reached, return to the main program of
TM.
EXAMPLE: Design a TM which can multiply two positive integers.
Solution: The input (m,n), m,n being given ,the positive integers represented by 0 m10n. M starts with
0m10n in its tape. At the end of the computation, 0mn (mn in unary representation) surrounded byb's is
obtained as the output.

SHREEDEVIPRAMOD,CSE,BRCE 113
The major steps in the construction are as follows:
1. 0m10n1 is placed on the tape (the output will be written after the rightmost 1).
2. The leftmost 0 is erased.
3. A block of n 0's is copied onto the right end.
4. Steps 2 and 3 are repeated m times and 10m10mn is obtained on the tape.
5. The prefix 10m1of 10m10mn is erased, leaving the product 0mn as the output.
For every 0 in 0m, 0n is added onto the right end. This requires repetition of step3. We define a
subroutine called COPY for step3. For the subroutine COPY the initial state is q1 and the final state
is q5 is given by the transition table as below:

The Turing machine M has the initial state q0. The initial ID for M is q00m10n. On seeing 0,the following
moves take place

q1 is the initial state of COPY. The following moves take place for M1:

After exhausting 0s, q1 encounters 1. M1 moves to state q4. All 2's are converted back to 0's and
M1 halts in q5. The TM M picks up the computation by starting from q 5 The q0 and q6 are the states of
M. Additional states are created to check whether reach 0 in 0 m gives rise to 0m at the end of the
rightmost 1 in the input string. Once this is over, M erases 10n1 and finds 0mn in the input tape.
M can be defined by M=({q0,q1,....q12}{0,1},{0,,2,b},,q0,b,{q12}) where is defined by table givenbelow:

SHREEDEVIPRAMOD,CSE,BRCE 114
ADDITIONAL PROBLEMS
1. Design a Turing machine to obtain complement of a binary number.
IDEA OF CONSTRUCTION:
1) If symbol is 0 change it to 1, move read write head to RIGHT
2) If symbol is 1 change it to 0, move read write head to RIGHT
3) Symbol is b (blank) don’t change, move read write head to RIGHT, and HALT.
The construction is made by defining moves in the following manner:
(a) ql is the initial state. On scanning 1, no change in state and write 0 and move head to RIGHT.
(c) If M is in state q1and scans blank, it enters q2 and writes b move to right.
(d) q2 is the only accepting state.
Symbolically, M=({ql,q2},{1,0,b},{1,0,b},,ql,b,{q2}) Where is defined by:

The computation sequence of 1010:

2. Design a TM that converts binary number into its 2’s complement representation.
IDEA OF CONSTRUCTION:
 Read input from left to right until right end blank is scanned.
 Begin scan from right to left keep symbols as it is until 1 found on input file.

SHREEDEVIPRAMOD,CSE,BRCE 115
 If 1 found on input file, move head to left one cell without changing input.
 Now until left end blank is scanned, change al 1’s to 0 and 0’s to 1.
We require the following moves:
(a) Let q1 be initial state, until blank is scanned, move head to RIGHT without changing
anything. On scanning blank, move head to RIGHT change state to q 2 without changing the
content of input.
(b) If q2 is the state, until 1 is scanned, move head to LEFT without changing anything. On
reading 1, change state to q3, move head to LEFT without changing input.
(c) If q3 is the state, until blank is scanned, move head to LEFT, if symbol is 0 change to 1,
otherwise if symbol is 1 change to 0.On finding blank change state to q4, move head to LEFT
without Changing input.
(d) q4 is the only accepting state.
We construct a TM M as follows:
M = (Q, Σ,, , q0,b, F)
Q = {q1,q2,q3,q4}
F = {q4 }
Σ = {0,1}
𝚪= {0,1,b}

3.Design a TM that add two integers


IDEA OF CONSTRUCTION:
 Read input from LEFT to RIGHT until blank (separator of two numbers) is found.
 Continue LEFT to RIGHT until blank (end of second number) is found.
 Change separator b to 1 move head to RIGHT.
 move header to Left ( to point rightmost 1)
 Change 1 to b and move right, Halt.

We require the following moves:


(a) In q1 TM skips1’s until it reads b (separator),changes to1and goes to q1
(b) In q2 TM skips1’s until it reads b (end of input), turns left and goes to q3
(c) In q3, TM reads 1 and changes to b go to q4.

SHREEDEVIPRAMOD,CSE,BRCE 116
(d) q4 is the final state, TM halts.
we construct a TM M as follows: M = (Q, Σ, 𝚪, , q0,b, F)
Q = {q1,q2,q3,q4}
F = {q4}
Σ = { b,1}
𝚪= {1,b}

4. Design a TM that accepts the set of all palindromes over {0,1}*


IDEA OF CONSTRUCTION:
 If it is 0 and changes to X, similarly if it is 1, it is changed to Y, and moves right until it finds
blank.
 Starting at the left end it checks the first symbol of the input,
 Nowmovesonestepleftandcheckwhetherthesymbolreadmatchesthemostrecentlychanged.Ifsoiti
salsochangedcorrespondingly.
 Now machine moves back left until it finds 0 or 1.
 This process is continued by moving left and right alternately until al 0’s and 1’s have been
matched.
We require the following moves:
1. If state is q0and it scans 0.
 Then go to state q1 and change the 0 to an X,
 move RIGHT over al 0’s and 1’s, until it finds either X or Y or B
 Now move one step left and change state to q3
 It verifies that the symbol read is 0, and changes the 0 to X and goes to state q5.
2. If state is q0 and it scans 1

 Then go to state q2 and change the 1 to an Y,


 Move RIGHT over al 0’s and 1’s, until it finds either X or Y or B
o Now move one step left and change state to q4
o It verifies that the symbol read is 1, and changes the 1 to Y and goes to state q5.
3. If state is q5
 Move LEFT over al 0’s and 1’s, until it finds either X or Y.
 Now move one step RIGHT and change state to q0.

SHREEDEVIPRAMOD,CSE,BRCE 117
 Now at q0 there are two cases:
1. If 0’s and 1’s are found on input , it repeats the matching cycle just described.
2. If X’s and Y’s are found on input, then it changes all the 0’s to X and all the 1’s to Y’s.
The input was a palindrome of even length, Thus, state changed to q6.
4. If state is q3 or q4
If X’s and Y’s are found on input, it concludes that: The input was a palindrome of odd
length, thus, state changed to q6.
We construct a TM M as follows:
M = (Q, Σ, 𝚪, , q0,b,F)
Q = {q0,q1,q2,q3,q4,q5,q6}
F = {q6}
Σ = { b,1,0}
𝚪 = {X,Y,b}

PRACTICE PROBLEMS
1. Design a Turing machine to replace al a’s with X and al b’s with Y.
2. Design a Turing machine to accept anbm n>m.
3. Design a Turing machine to accept anbn n<m.
4. Design a Turing machine to accept (0+1)*00(0+1)* .
5. Design a Turing machine to increment a given input.
6. Design a Turing machine to decrement a given input.
7. Design a Turing machine to subtract two unary numbers.
8. Design a Turing machine to multiply two unary numbers.
9. Design a Turing machine to accept a string 0’s followed by a 1.

SHREEDEVIPRAMOD,CSE,BRCE 118
10. Design a Turing machine to verify if the given binary number is an even number or not.
11. Design a Turing machine to shift the given input by one cell to left.
12. Design a Turing machine to shift the given input to the right by one cell .
13. Design a Turing machine to rotate a given input by one cell.
14. Design a Turing machine to erase the tape.
15. Design a Turing machine to accept anbncn .
16. Design a Turing machine to accept any string of a’s & b’s with equal number of a’s & b’s.
17. Design a Turing machine to accept anb2n.
18. Design a Turing machine to accept anbkcm: where n=m+k.
19. Design a Turing machine to accept anbkcm: where m=n+k.
20. Design a Turing machine to accept anbkcm: where k=m+n.

SHREEDEVIPRAMOD,CSE,BRCE 119
Module 5

Church-Turing thesis-1936
• Any algorithmic procedure that can be carried out by a human or a computer, can also be
carried out by a Turing machine.
• Now it is universally accepted by computer scientists that TM is a Mathematical model of an
algorithm.
• TM has an algorithm and an algorithm has a TM. If there is an algorithm problem is
decidable, TMsolves that problem
• The statement of the thesis –
“ Every function which would naturally be regarded as computable can be
computed by a Turing machine”
Implies
• Any mechanical computation can be performed by a TM
• For every computable problem there is a TM
• If there is no TM that decides P there is no algorithm that can solve problem P.
• In our general life, we have several problems and some of these have solutions, but some have
not, we simply say a problem is decidable if there is a solution otherwise undecidable.
example:
• Does Sun rises in the East? YES
• Will tomorrow be a rainy day ? ( YES/NO ? )

SHREEDEVIPRAMOD,CSE,BRCE 120
Decidable and Undecidable Languages
• A problem is said to be decidable if its language is recursive OR it has solution.

Example:
Decidable :
-Does FSM accept regular language?
- is the power of NFA and DFA same
Undecidable:
- For a given CFG is L(G) ambiguous?
L is Turing decidable (or just decidable) if there exists a Turing machine Mthat accepts all strings in L and
rejects all strings not in L. Note that by rejection means that the machine halts after a finite number of
steps and announces that the input string is not acceptable.

• There are two types of TMs (based on halting):

1. (Recursive)
TMs that always halt, no matter accepting or
non-accepting  DECIDABLE PROBLEMS
2. (Recursively enumerable)
TMs that are guaranteed to halt only on acceptance.
If non-accepting, it may or may not halt (i.e., could loop forever).
• Undecidable problems are those that are not recursive

Recursive languages
A Language L over the alphabet∑ is called recursive if there is a TM M that accepts every word in L and
rejects every word in L’
Accept (M)=L
Reject(M)=L’ loop(M)=
ø Example: b(a+b)*

Recursively Enumerable Language:


A Language L over the alphabet∑ is called recursively enumerable if there is a TM M that accepts
every word in L and either rejects or loops every word in L’ the complement of L
Accept (M)=L Reject(M)
+Loop(M)= L’
Example: (a+b)*bb(a+b)*

SHREEDEVIPRAMOD,CSE,BRCE 121
Recursively Enumerable Languages closed under complementation? (NO)

SHREEDEVIPRAMOD,CSE,BRCE 122
1. Prove that Recursive Languagess are closed under Union

2. Prove that Recursive Languages are closed under Intersection

3. Recursive languages are also closed under:


a. Concatenation
b. Kleene closure (star operator)
c. Homomorphism, and inverse homomorphism
4. RE languages are closed under:
a. Union, intersection, concatenation, Kleene closure
5. RE languages are not closed under:
a. Complementation

1. Decidable Languages about DFA : Prove that

SHREEDEVIPRAMOD,CSE,BRCE 123
2. Prove that

3. Prove that

SHREEDEVIPRAMOD,CSE,BRCE 124
SHREEDEVIPRAMOD,CSE,BRCE 125
PCP is a combinatorial problem formulated by Emil Post in 1946. This problem has many applications
in the field theory of formal languages. A correspondence system P is a finite set of ordered pairs of
non empty strings over some alphabet. Let A  w1, w2 ,…, wn B  v1, v2 ,…, vn

wi wj wk  v iv j vk
Index wj Vi
1 100 001
2 11 111
3 111 11

Let W = w2w1w3=v2v1v3 = 11100111 we have got a solution. But we may not get solution always
for various other combinations and strings of different length. Hence PCP is undecidable.

A  w1, w2 , …, wn B  v1, v2 ,…, vn


1, i, j,…, k

w1wi wj wk  v1vi v j vk

If the index start with 1 and then any other sequence then it is called MPCP

Algorithm: An algorithm is “a finite set of precise instructions for performing a


computation or for solving a problem”
• A program is one type of algorithm
• All programs are algorithms
• Not all algorithms are programs!
• The steps to compute roots of quadratic equation is an algorithm
• The steps to compute the cosine of 90° is an algorithm
Algorithms generally share a set of properties:
• Input: what the algorithm takes in as input
• Output: what the algorithm produces as output
• Definiteness: the steps are defined precisely
• Correctness: should produce the correct output
• Finiteness: the steps required should be finite
• Effectiveness: each step must be able to be performed in a finite amount of
time

SHREEDEVIPRAMOD,CSE,BRCE 126
• Generality: the algorithm should be applicable to all problems of a similar
form
Comparing Algorithms (While comparing two algorithm we use time and space complexities)

⚫ Time complexity

◦ The amount of time that an algorithm needs to run to completion

⚫ Space complexity

◦ The amount of memory an algorithm needs to run

⚫ To analyze running time of the algorithm we use following cases

◦ Best case
◦ Worst case
Average case

Asymptotic analysis
⚫ The big-Oh notation is used widely to characterize running times and space bounds
⚫ The big-Oh notation allows us to ignore constant factors and lower order terms and focus on
the main components of a function which affect its growth
⚫ Given functions f(n) and g(n), we say that f(n) is O(g(n)) if there are positive constants
c and n0 such that
f(n)  cg(n) for n  n0
⚫ Example: 2n + 10 is O(n)
◦ 2n + 10  cn
◦ (c  2) n  10
◦ n  10/(c  2)
It is true for c = 3 and n0 = 10

 7n-2 is O(n)
need c > 0 and n0  1 such that 7n-2  c•n for n  n0
this is true for c = 7 and n0 = 1

f(n)= O(g(n)) iff there exist positive constants c and n0 such that f(n) ≤ cg(n) for all n ≥ n0
O-notation to give an upper bound on a function

Big oh provides an asymptotic upper bound on a function.

SHREEDEVIPRAMOD,CSE,BRCE 127
Omega provides an asymptotic lower bound on a function.

⚫ The big-Oh notation gives an upper bound on the growth rate of a function
⚫ The statement “f(n) is O(g(n))” means that the growth rate of f(n) is no more than the
growth rate of g(n)
⚫ We can use the big-Oh notation to rank functions according to their growth rate
f n  a  a n  a n2 ... a nd
0 1 2 d

⚫ If is f(n) a polynomial of degree d, then f(n) is O(nd), i.e.,


1. Drop lower-order terms
2. Drop constant factors
⚫ Use the smallest possible class of functions
1. Say “2n is O(n)” instead of “2n is O(n2)”
⚫ Use the simplest expression of the class
Say “3n + 5 is O(n)” instead of “3n + 5 is O(3n)”
⚫ Following are the terms usually used in algorithm analysis:
1. Constant  1
2. Logarithmic  log n
3. Linear  n
4. N-Log-N  n log n
5. Quadratic  n2
6. Cubic  n3
7. Exponential  2n

Class P Problems:
P stands for deterministic polynomial time. A deterministic machine at each time executes an
instruction. Depending on instruction, it then goes to next state which is unique. Hence time
complexity of DTM is the maximum number of moves made by M is processing any input string of
length n, taken over all input of length n.
⚫ The class P consists of those problems that are solvable in polynomial time.
⚫ More specifically, they are problems that can be solved in time O(nk) for some constant k,
where n is the size of the input to the problem
⚫ The key is that n is the size of input
Def: A language L is said to be in class P if there exists a DTM M such that M is of time complexity
P(n) for some polynomial P and M accepts L.

SHREEDEVIPRAMOD,CSE,BRCE 128
Class NP Problems
Def: A language L is in class NP if there is a nondeterministic TM such that M is of time complexity
P(n) for some polynomial P and M accepts L.
⚫ NP is not the same as non-polynomial complexity/running time. NP does not stand for not
polynomial.
⚫ NP = Non-Deterministic polynomial time
⚫ NP means verifiable in polynomial time
⚫ Verifiable?
◦ If we are somehow given a ‘certificate’ of a solution we can verify the legitimacy in
polynomial time

⚫ Problem is in NP iff it is decidable by some non deterministic Turing machine in polynomial


time.
⚫ It is provable that a Non Deterministic Turing Machine is equivalent to a Deterministic Turing
Machine
⚫ Remember NFA to DFA conversion?
◦ Given an NFA with n states how many states does the equivalent DFA have?
◦ Worst case …. 2n
◦ The deterministic version of a polynomial time
⚫ non deterministic Turing machine will run in exponential time (worst case)
⚫ Since it takes polynomial time to run the program, just run the program and get a solution
⚫ But is NP a subset of P? It is not yet clear whether P = NP or not

Quantum Computers
⚫ Computers are physical objects, and computations are physical processes. What computers
can or cannot compute is determined by the law of physics alone, and not by pure
mathematics. Computation with coherent atomic-scale dynamics.
The behavior of a quantum computer is governed by the laws of quantum mechanics.

⚫ In 1982 Richard Feynmann, a Nobel laurite in physics suggested to build computer based on
uantum mechanics.
⚫ Quantum mechanics arose in the early 1920s, when classical physics could not explain
everything.
⚫ QM will provide tools to fill up the gulf between the small and the relatively complex
systems in physics.
⚫ Bit ( 0 or 1 ) is the fundamental concept of classical computation and information. Classical
computer built from electronic circuits containing wires and gates.
⚫ Quantum bit and quantum circuits which are analogous to bits and circuits. Two possible
states of a qubit (Dirac)are 0 1
⚫ Quantum bit is qubit described mathematically (where (alpha) is complex number)
0 0   1 1
⚫ Qubit can be in infinite number of state other than dirac |0> or |1>

⚫ he operations are induced by the apparatus linearly, that is, if

SHREEDEVIPRAMOD,CSE,BRCE 129
SHREEDEVIPRAMOD,CSE,BRCE 130
1. With One tape but Multiple heads: a K head TM has fixed k number of heads and
move of TM depends on the state and the symbol scanned by each head. ( head
can move left, right or stationary).
2. Multidimensional TM: It has finite control but the tape consists of a K-
dimensional array of cells infinite in all 2 k directions. Depending on the state and
symbol scanned , the device changes the state, prints a new symbol, and moves its
tape head in one of the 2 k directions, either positively of negatively along one of
the k axes.
3. Non deterministic TM: In the TM for a given state and tape symbol scanned by
the tape head, the machine has a finite number of choices for the next move. Each
choice consists of new state, a tape symbol to print and direction of head motion.

Linear Bounded Automata


LBA is a restricted form of a Non deterministic Turing machine. It is a multitrack turing
machine which has only one tape and this tape is exactly same length as that of input. It
accepts the string in the similar manner as that of TM. For LBA halting means accepting. In
LBA computation is restricted to an area bounded by length of the input. This is very much
similar to programming environment where size of the variable is bounded by its data type.
Lba is 7-tuple on Deterministic TM with

< a b a b a >
Left EM Right EM

M= (Q,∑,Ʈ, Delta,qaccept, qreject, q0 )

 Two extra symbols < and > are used left end marker and right
end marker.
 Input lies between these markers

SHREEDEVIPRAMOD,CSE,BRCE 131

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy