0% found this document useful (0 votes)
20 views

FLAT

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)
20 views

FLAT

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/ 85

What is a finite state machine :

A finite state machine has a set of states and two functions called the next-state and
output function.

 The set of states correspond to all the possible combinations of the internal
storage. If there are n bits of storage, there are 2n possible states.
 The next state function is a combinational logic function that, given the
inputs and the current state, determines the next state of the system.

The diagram given below explains the functioning of a finite state machine in
TOC.

The output function generates a set of outputs from the current state and the inputs.

Types

The two types of finite state machines are −

 Moore machine − In Moore machine, the output only depends on the


current state.
 Mealy machine − In Mealy machine, the output depends on both the current
state and the current input.

We mostly deal with the Moore machine. These two types are equivalent in
capabilities.

A Finite State Machine consists of the following −

 K states: S = {s1, s2, ... ,sk}, s1 is initial state


 N inputs: I = {h, i2, ... ,in}
 M outputs: O = {o1, o2, . ,om}
 Next-state function T(S, I): This is used for mapping each current state and
input to the next state.
 Output Function P(S): This function specifies the output.
Components

The components which exists in a finite state machine are explained below −

State − The states are usually drawn with circles and only one state can be active
at a time.

It is represented as follows −

Initial State − It is the starting point of our system. Initial states are usually drawn
with an arrow pointed to state, as shown below −

Final state − It is a subset of known states that indicates whether the input we
processed is valid or not. Accepting states are usually drawn as a double circle as
shown below −

Transitions − The machine moves from one state to another and is indicated as
transition. These are drawn as two states connected with a line, as shown below −
Finite Automaton can be classified into two types −

 Deterministic Finite Automaton (DFA)


 Non-deterministic Finite Automaton (NDFA / NFA)

Deterministic Finite Automaton (DFA)

In DFA, for each input symbol, one can determine the state to which the machine
will move. Hence, it is called Deterministic Automaton. As it has a finite number
of states, the machine is called Deterministic Finite Machine or Deterministic
Finite Automaton.

Formal Definition of a DFA

A DFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −

 Q is a finite set of states.


 ∑ is a finite set of symbols called the alphabet.
 δ is the transition function where δ: Q × ∑ → Q
 q0 is the initial state from where any input is processed (q0 ∈ Q).
 F is a set of final state/states of Q (F ⊆ Q).

Graphical Representation of a DFA

A DFA is represented by digraphs called state diagram.

 The vertices represent the states.


 The arcs labeled with an input alphabet show the transitions.
 The initial state is denoted by an empty single incoming arc.
 The final state is indicated by double circles.

Example

Let a deterministic finite automaton be →

 Q = {a, b, c},
 ∑ = {0, 1},
 q0 = {a},
 F = {c}, and

Transition function δ as shown by the following table −


Present State Next State for Input 0 Next State for Input 1

a a b

b c a

c b c

Its graphical representation would be as follows −

In NDFA, for a particular input symbol, the machine can move to any combination
of the states in the machine. In other words, the exact state to which the machine
moves cannot be determined. Hence, it is called Non-deterministic Automaton.
As it has finite number of states, the machine is called Non-deterministic Finite
Machine or Non-deterministic Finite Automaton.

Formal Definition of an NDFA

An NDFA can be represented by a 5-tuple (Q, ∑, δ, q0, F) where −

 Q is a finite set of states.


 ∑ is a finite set of symbols called the alphabets.
 δ is the transition function where δ: Q × ∑ → 2Q

(Here the power set of Q (2Q) has been taken because in case of NDFA, from
a state, transition can occur to any combination of Q states)
 q0 is the initial state from where any input is processed (q0 ∈ Q).
 F is a set of final state/states of Q (F ⊆ Q).

Graphical Representation of an NDFA: (same as DFA)

An NDFA is represented by digraphs called state diagram.

 The vertices represent the states.


 The arcs labeled with an input alphabet show the transitions.
 The initial state is denoted by an empty single incoming arc.
 The final state is indicated by double circles.

Example

Let a non-deterministic finite automaton be →

 Q = {a, b, c}
 ∑ = {0, 1}
 q0 = {a}
 F = {c}

The transition function δ as shown below −

Present State Next State for Input 0 Next State for Input 1

a a, b b

b c a, c

c b, c c

Its graphical representation would be as follows −


DFA vs NDFA

The following table lists the differences between DFA and NDFA.

DFA NDFA

The transition from a state can be to


The transition from a state is to a single
multiple next states for each input
particular next state for each input
symbol. Hence it is called non-
symbol. Hence it is called deterministic.
deterministic.

Empty string transitions are not seen in


NDFA permits empty string transitions.
DFA.

In NDFA, backtracking is not always


Backtracking is allowed in DFA
possible.

Requires more space. Requires less space.

A string is accepted by a NDFA, if at


A string is accepted by a DFA, if it
least one of all possible transitions ends
transits to a final state.
in a final state.

Finate Autometa with e moves:

The automaton may be allowed to change its state without reading the input
symbol 2.

In diagrams, such transitions are depicted by labeling the appropriate arcs with ε.

Note that this does not mean that E has become an input symbol. On the contrary,
we assume that the symbol E does not belong to any alphabet.

 ε -NFAs add a convenient feature but (in a sense) they bring us nothing new.
They do not extend the class of languages that can be represented.
 Both NFAs and E-NFAs recognize exactly the same languages.
Epsilon (ε) - closure

Epsilon closure for a given state X is a set of states which can be reached from the
states X with only (null) or E moves including the state X itself.

In other words, £-closure for a state can be obtained by union operation of the £-
closure of the states which can be reached from X with a single E move in a
recursive manner.

Example

Consider the following figure of NFA with ε move −

The transition state table for the above NFA is as follows −

State 0 1 epsilon

A B,C A B

B - B C

C C C -

For the above example, ε closure are as follows −

 E closure( A) : {A, B,C}


 E closure( B) :{B,C}
 E closure( C) : {C}

A Regular Expression can be recursively defined as follows −

 ε is a Regular Expression indicates the language containing an empty string.


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

Some RE Examples
Regular
Regular Set
Expressions

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

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

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

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

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

Set consisting of even number of 1’s including empty string,


(11)*
So L= {ε, 11, 1111, 111111, ……….}
Set of strings consisting of even number of a’s followed by
(aa)*(bb)*b odd number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab,
aaaabbb, …………..}

String of a’s and b’s of even length can be obtained by


(aa + ab + ba + bb)* concatenating any combination of the strings aa, ab, ba and bb
including null, so L = {aa, ab, ba, bb, aaab, aaba, …………..}

Finite automata may have outputs corresponding to each transition. There are two
types of finite state machines that generate output −

 Mealy Machine
 Moore machine

Mealy Machine

A Mealy Machine is an FSM whose output depends on the present state as well as
the present input.

It can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −

 Q is a finite set of states.


 ∑ is a finite set of symbols called the input alphabet.
 O is a finite set of symbols called the output alphabet.
 δ is the input transition function where δ: Q × ∑ → Q
 X is the output transition function where X: Q × ∑ → O
 q0 is the initial state from where any input is processed (q0 ∈ Q).

The state table of a Mealy Machine is shown below −

Next state

Present state input = 0 input = 1

State Output State Output


→a b x1 c x1

b b x2 d x3

c d x3 c x1

d d x3 d x2

The state diagram of the above Mealy Machine is −

Moore Machine

Moore machine is an FSM whose outputs depend on only the present state.

A Moore machine can be described by a 6 tuple (Q, ∑, O, δ, X, q0) where −

 Q is a finite set of states.


 ∑ is a finite set of symbols called the input alphabet.
 O is a finite set of symbols called the output alphabet.
 δ is the input transition function where δ: Q × ∑ → Q
 X is the output transition function where X: Q → O
 q0 is the initial state from where any input is processed (q0 ∈ Q).

The state table of a Moore Machine is shown below −

Present state Next State Output


Input = 0 Input = 1

→a b c x2

b b d x1

c c d x2

d d d x3

The state diagram of the above Moore Machine is −

Mealy Machine vs. Moore Machine

The following table highlights the points that differentiate a Mealy Machine from a
Moore Machine.

Mealy Machine Moore Machine

Output depends both upon the present Output depends only upon the present
state and the present input state.

Generally, it has fewer states than Generally, it has more states than Mealy
Moore Machine. Machine.

The value of the output function is a The value of the output function is a
function of the transitions and the function of the current state and the
changes, when the input logic on the changes at the clock edges, whenever state
present state is done. changes occur.

In Moore machines, more logic is required


Mealy machines react faster to inputs.
to decode the outputs resulting in more
They generally react in the same clock
circuit delays. They generally react one
cycle.
clock cycle later.

Moore Machine to Mealy Machine

Algorithm 4

Input − Moore Machine

Output − Mealy Machine

Step 1 − Take a blank Mealy Machine transition table format.

Step 2 − Copy all the Moore Machine transition states into this table format.

Step 3 − Check the present states and their corresponding outputs in the Moore
Machine state table; if for a state Qi output is m, copy it into the output columns of
the Mealy Machine state table wherever Qi appears in the next state.

Example

Let us consider the following Moore machine −

Next State
Present State Output
a=0a=1

→a d b 1

b a d 0

c c c 0
d b a 1

Now we apply Algorithm 4 to convert it to Mealy Machine.

Step 1 & 2 −

Next State

Present State a=0 a=1

State Output State Output

→a d b

b a d

c c c

d b a

Step 3 −

Next State

Present State a=0 a=1

State Output State Output

=> a d 1 b 0

b a 1 d 1

c c 0 c 0

d b 0 a 1
Mealy Machine to Moore Machine

Algorithm 5

Input − Mealy Machine

Output − Moore Machine

Step 1 − Calculate the number of different outputs for each state (Qi) that are
available in the state table of the Mealy machine.

Step 2 − If all the outputs of Qi are same, copy state Qi. If it has n distinct outputs,
break Qi into n states as Qin where n = 0, 1, 2.......

Step 3 − If the output of the initial state is 1, insert a new initial state at the
beginning which gives 0 output.

Example

Let us consider the following Mealy Machine −

Next State

Present State a=0 a=1

Next State Output Next State Output

→a d 0 B 1

b a 1 D 0

c c 1 C 0

d b 0 A 1

Here, states ‘a’ and ‘d’ give only 1 and 0 outputs respectively, so we retain states
‘a’ and ‘d’. But states ‘b’ and ‘c’ produce different outputs (1 and 0). So, we divide
b into b0, b1 and c into c0, c1.
Next State
Present State Output
a=0 a=1

→a d b1 1

b0 a d 0

b1 a d 1

c0 c1 C0 0

c1 c1 C0 1

d b0 a 0

Two-way deterministic finite automaton

A two-way deterministic finite automaton (2DFA) is an abstract machine, a


generalized version of the deterministic finite automaton (DFA) which can revisit
characters already processed. As in a DFA, there are a finite number of states with
transitions between them based on the current character, but each transition is also
labelled with a value indicating whether the machine will move its position in the
input to the left, right, or stay at the same position. Equivalently, 2DFAs can be
seen as read-only Turing machines with no work tape, only a read-only input tape.

2DFAs were introduced in a seminal 1959 paper by Rabin and Scott,[1] who proved
them to have equivalent power to one-way DFAs. That is, any formal language
which can be recognized by a 2DFA can be recognized by a DFA which only
examines and consumes each character in order. Since DFAs are obviously a
special case of 2DFAs, this implies that both kinds of machines recognize precisely
the class of regular languages. However, the equivalent DFA for a 2DFA may
require exponentially many states, making 2DFAs a much more practical
representation for algorithms for some common problems.

2DFAs are also equivalent to read-only Turing machines that use only a constant
amount of space on their work tape, since any constant amount of information can
be incorporated into the finite control state via a product construction (a state for
each combination of work tape state and control state).

Formal description

Formally, a two-way deterministic finite automaton can be described by the


following 8-tuple: where

 Q: is the finite, non-empty set of states


 sigma: is the finite, non-empty set of input symbols
 L: is the left endmarker
 R: is the right endmarker

 s: is the start state
 t: is the end state
 r; is the reject state

Applications of FSM(finate state machines):

Automaton is nothing but a machine which accepts the strings of a language L over
an input alphabet Σ.

There are four different types of Automata that are mostly used in the theory of
computation (TOC). These are as follows −

 Finite-state machine (FSM).


 Pushdown automata (PDA).
 Linear-bounded automata (LBA).
 Turing machine (TM).

When comparing these four types of automata, Finite-state machines are less
powerful whereas Turing machines are more powerful.

Note − Deterministic Finite Automata (DFA) and the Non-Deterministic Finite


Automata (NFA) have the same power because every DFA is converted into NFA
and every NFA is converted into DFA.

So far, we are familiar with the types of automata. Now, let us discuss the
expressive power of automata and further understand its applications.
Equivalence

Let’s see the equivalence of each automaton before going to applications of


automata.

Finite-state Machine is equivalent to the following −

 PDA with finite stack.


 Turing Machine with finite tape.
 Turing Machine with unidirectional tape.
 Turing Machine with read only tape.

Pushdown Automata is equivalent to the following −

 Finite Automata with stack

Turing Machine is equivalent to the following −

 PDA with additional stack.


 Finite Automata with two stacks.

Applications of different Automata

The applications of the different automata in Toc are explained below −

Finite Automata (FA)

The applications of Finite Automata are as follows −

 Design of the lexical analysis of a compiler.


 Recognize the pattern by using regular expressions.
 Use of the Mealy and Moore Machines for designing the combination and
sequential circuits.
 Helpful in text editors.
 Used for spell checkers.

Push Down Automata (PDA)

The applications of Pushdown automata are as follows −

 Used in the Syntax Analysis phase.


 Implementation of stack applications.
 Used in evaluations of the arithmetic expressions.
 Used for solving the Tower of Hanoi Problem.

Linear Bounded Automata (LBA)

The applications of linear bounded automata are as follows −

 Used in the genetic programming implementation.


 Construction of syntactic parse trees.

Turing Machine (TM)

The applications of Turing machine are as follows −

 Used to solve the recursively enumerable problem.


 Used for knowing complexity theory.
 Used for neural networks implementation.
 Used in Robotics Applications.
 Used in the implementation of artificial intelligence.

Unit -2

The theory of formal languages finds its applicability extensively in the fields of
Computer Science. Noam Chomsky gave a mathematical model of grammar in
1956 which is effective for writing computer languages.

Grammar

A grammar G can be formally written as a 4-tuple (N, T, S, P) where −

 N or VN is a set of variables or non-terminal symbols.


 T or ∑ is a set of Terminal symbols.
 S is a special variable called the Start symbol, S ∈ N
 P is Production rules for Terminals and Non-terminals. A production rule
has the form α → β, where α and β are strings on VN ∪ ∑ and least one
symbol of α belongs to VN.
Example

Grammar G1 −

({S, A, B}, {a, b}, S, {S → AB, A → a, B → b})

Here,

 S, A, and B are Non-terminal symbols;


 a and b are Terminal symbols
 S is the Start symbol, S ∈ N
 Productions, P : S → AB, A → a, B → b

Example

Grammar G2 −

(({S, A}, {a, b}, S,{S → aAb, aA → aaAb, A → ε } )

Here,

 S and A are Non-terminal symbols.


 a and b are Terminal symbols.
 ε is an empty string.
 S is the Start symbol, S ∈ N
 Production P : S → aAb, aA → aaAb, A → ε

Derivations from a Grammar

Strings may be derived from other strings using the productions in a grammar. If a
grammar G has a production α → β, we can say that x α y derives x β y in G. This
derivation is written as −

x α y ⇒G x β y

Example

Let us consider the grammar −

G2 = ({S, A}, {a, b}, S, {S → aAb, aA → aaAb, A → ε } )


Some of the strings that can be derived are −

S ⇒ aAb using production S → aAb

⇒ aaAbb using production aA → aAb

⇒ aaaAbbb using production aA → aaAb

⇒ aaabbb using production A → ε

The set of all strings that can be derived from a grammar is said to be the language
generated from that grammar. A language generated by a grammar G is a subset
formally defined by

L(G)={W|W ∈ ∑*, S ⇒G W}

If L(G1) = L(G2), the Grammar G1 is equivalent to the Grammar G2.

Example

If there is a grammar

G: N = {S, A, B} T = {a, b} P = {S → AB, A → a, B → b}

Here S produces AB, and we can replace A by a, and B by b. Here, the only
accepted string is ab, i.e.,

L(G) = {ab}

Example

Suppose we have the following grammar −

G: N = {S, A, B} T = {a, b} P = {S → AB, A → aA|a, B → bB|b}

The language generated by this grammar −

L(G) = {ab, a2b, ab2, a2b2, ………}

= {am bn | m ≥ 1 and n ≥ 1}
Construction of a Grammar Generating a Language

We’ll consider some languages and convert it into a grammar G which produces
those languages.

Example

Problem − Suppose, L (G) = {am bn | m ≥ 0 and n > 0}. We have to find out the
grammar G which produces L(G).

Solution

Since L(G) = {am bn | m ≥ 0 and n > 0}

the set of strings accepted can be rewritten as −

L(G) = {b, ab,bb, aab, abb, …….}

Here, the start symbol has to take at least one ‘b’ preceded by any number of ‘a’
including null.

To accept the string set {b, ab, bb, aab, abb, …….}, we have taken the productions

S → aS , S → B, B → b and B → bB

S → B → b (Accepted)

S → B → bB → bb (Accepted)

S → aS → aB → ab (Accepted)

S → aS → aaS → aaB → aab(Accepted)

S → aS → aB → abB → abb (Accepted)

Thus, we can prove every single string in L(G) is accepted by the language
generated by the production set.

Hence the grammar −

G: ({S, A, B}, {a, b}, S, { S → aS | B , B → b | bB })


Regular Sets:

Any set that represents the value of the Regular Expression is called a Regular
Set.

Properties of Regular Sets

Property 1. The union of two regular set is regular.

Proof −

Let us take two regular expressions

RE1 = a(aa)* and RE2 = (aa)*

So, L1 = {a, aaa, aaaaa,.....} (Strings of odd length excluding Null)

and L2 ={ ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)

L1 ∪ L2 = { ε, a, aa, aaa, aaaa, aaaaa, aaaaaa,.......}

(Strings of all possible lengths including Null)

RE (L1 ∪ L2) = a* (which is a regular expression itself)

Hence, proved.

Property 2. The intersection of two regular set is regular.

Proof −

Let us take two regular expressions

RE1 = a(a*) and RE2 = (aa)*

So, L1 = { a,aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)

L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)

L1 ∩ L2 = { aa, aaaa, aaaaaa,.......} (Strings of even length excluding Null)


RE (L1 ∩ L2) = aa(aa)* which is a regular expression itself.

Hence, proved.

Property 3. The complement of a regular set is regular.

Proof −

Let us take a regular expression −

RE = (aa)*

So, L = {ε, aa, aaaa, aaaaaa, .......} (Strings of even length including Null)

Complement of L is all the strings that is not in L.

So, L’ = {a, aaa, aaaaa, .....} (Strings of odd length excluding Null)

RE (L’) = a(aa)* which is a regular expression itself.

Hence, proved.

Property 4. The difference of two regular set is regular.

Proof −

Let us take two regular expressions −

RE1 = a (a*) and RE2 = (aa)*

So, L1 = {a, aa, aaa, aaaa, ....} (Strings of all possible lengths excluding Null)

L2 = { ε, aa, aaaa, aaaaaa,.......} (Strings of even length including Null)

L1 – L2 = {a, aaa, aaaaa, aaaaaaa, ....}

(Strings of all odd lengths excluding Null)

RE (L1 – L2) = a (aa)* which is a regular expression.

Hence, proved.
Property 5. The reversal of a regular set is regular.

Proof −

We have to prove LR is also regular if L is a regular set.

Let, L = {01, 10, 11, 10}

RE (L) = 01 + 10 + 11 + 10

LR = {10, 01, 11, 01}

RE (LR) = 01 + 10 + 11 + 10 which is regular

Hence, proved.

Property 6. The closure of a regular set is regular.

Proof −

If L = {a, aaa, aaaaa, .......} (Strings of odd length excluding Null)

i.e., RE (L) = a (aa)*

L* = {a, aa, aaa, aaaa , aaaaa,……………} (Strings of all lengths excluding Null)

RE (L*) = a (a)*

Hence, proved.

Property 7. The concatenation of two regular sets is regular.

Proof −

Let RE1 = (0+1)*0 and RE2 = 01(0+1)*

Here, L1 = {0, 00, 10, 000, 010, ......} (Set of strings ending in 0)

and L2 = {01, 010,011,.....} (Set of strings beginning with 01)

Then, L1 L2 = {001,0010,0011,0001,00010,00011,1001,10010,.............}
Set of strings containing 001 as a substring which can be represented by an RE −
(0 + 1)*001(0 + 1)*

Hence, proved.

Identities Related to Regular Expressions

Given R, P, L, Q as regular expressions, the following identities hold −

 ∅* = ε
 ε* = ε
 RR* = R*R
 R*R* = R*
 (R*)* = R*
 RR* = R*R
 (PQ)*P =P(QP)*
 (a+b)* = (a*b*)* = (a*+b*)* = (a+b*)* = a*(ba*)*
 R + ∅ = ∅ + R = R (The identity for union)
 R ε = ε R = R (The identity for concatenation)
 ∅ L = L ∅ = ∅ (The annihilator for concatenation)
 R + R = R (Idempotent law)
 L (M + N) = LM + LN (Left distributive law)
 (M + N) L = ML + NL (Right distributive law)
 ε + RR* = ε + R*R = R*

closure properties of Regular languages(sets)

n an automata theory, there are different closure properties for regular languages.
They are as follows −

 Union
 Intersection
 concatenation
 Kleene closure
 Complement

Let see one by one with an example


Union

If L1 and If L2 are two regular languages, their union L1 U L2 will also be regular.

Example

L1 = {an | n > O} and L2 = {bn | n > O}

L3 = L1 U L2 = {an U bn | n > O} is also regular.

Intersection

If L1 and If L2 are two regular languages, their intersection L1 ∩ L2 will also be


regular.

Example

L1= {am bn | n > 0 and m > O} and

L2= {am bn U bn am | n > 0 and m > O}

L3 = L1 ∩ L2 = {am bn | n > 0 and m > O} are also regular.

Concatenation

If L1 and If L2 are two regular languages, their concatenation L1.L2 will also be
regular.

Example

L1 = {an | n > 0} and L2 = {bn | n > O}

L3 = L1.L2 = {am . bn | m > 0 and n > O} is also regular.

Kleene Closure

If L1 is a regular language, its Kleene closure L1* will also be regular.

Example

L1 = (a U b )
L1* = (a U b)*

Complement

If L(G) is a regular language, its complement L'(G) will also be regular.


Complement of a language can be found by subtracting strings which are in L(G)
from all possible strings.

Example

L(G) = {an | n > 3} L'(G) = {an | n <= 3}

Note − Two regular expressions are equivalent, if languages generated by them are
the same. For example, (a+b*)* and (a+b)* generate the same language. Every
string which is generated by (a+b*)* is also generated by (a+b)* and vice versa.

Pumping Lemma For Regular Grammars

Theorem

Let L be a regular language. Then there exists a constant ‘c’ such that for every
string w in L −

|w| ≥ c

We can break w into three strings, w = xyz, such that −

 |y| > 0
 |xy| ≤ c
 For all k ≥ 0, the string xykz is also in L.

Applications of Pumping Lemma

Pumping Lemma is to be applied to show that certain languages are not regular. It
should never be used to show a language is regular.

 If L is regular, it satisfies Pumping Lemma.


 If L does not satisfy Pumping Lemma, it is non-regular.
Method to prove that a language L is not regular

 At first, we have to assume that L is regular.


 So, the pumping lemma should hold for L.
 Use the pumping lemma to obtain a contradiction −
o Select w such that |w| ≥ c
o Select y such that |y| ≥ 1
o Select x such that |xy| ≤ c
o Assign the remaining string to z.
o Select k such that the resulting string is not in L.

Hence L is not regular.

Problem

Prove that L = {aibi | i ≥ 0} is not regular.

Solution −

 At first, we assume that L is regular and n is the number of states.


 Let w = anbn. Thus |w| = 2n ≥ n.
 By pumping lemma, let w = xyz, where |xy| ≤ n.
 Let x = ap, y = aq, and z = arbn, where p + q + r = n, p ≠ 0, q ≠ 0, r ≠ 0. Thus
|y| ≠ 0.
 Let k = 2. Then xy2z = apa2qarbn.
 Number of as = (p + 2q + r) = (p + q + r) + q = n + q
 Hence, xy2z = an+q bn. Since q ≠ 0, xy2z is not of the form anbn.
 Thus, xy2z is not in L. Hence L is not regular.

Decision Algorithm(problem) in regular grammer:

A decision problem Q is a set of questions, each having either yes or no answer.


Let us consider the question is “is 10 a perfect square?” which is an example for a
decision problem. A decision problem generally consists of an infinite number of
related questions.
Example

The problem PSQ which determines whether an arbitrary natural number is a


perfect square or not having some doubtful questions like the following −

 q0: Is 0 a perfect square?


 q1: Is 1 a perfect square?
 q2: Is 2 a perfect square?

Solution

A solution to a decision problem Q is an algorithm which determines the


approximate answer to every question q ϵ Q.

An algorithm which solves a decision problem has to be the following −

 Complete.
 Mechanistic.
 Deterministic.

A procedure which satisfies the preceding properties is generally called effective.

A problem is called as a decidable if it has a representation in which the set of


accepted input strings from a recursive language.

Since computations of deterministic multi track and multi tape machines are
simulated on a standard Turing machine, a solution using these machines also
establishes the decidability of a problem.

The decision problem will be as follows −


Myhill-Nerode Theorem (minimization of finate autometa):

Minimization of DFA using Myhill-Nerode Theorem :


Minimization of DFA is Required to obtain the minimal and equivalent version of
any DFA which consists of minimum number of states possible. Myhill-Nerode
theorem can be used to convert a DFA to its equivalent DFA with minimum no of
states. This method of minimization is also called Table filling method. There is
also another method called Partitioning Method or Equivalence Method for the
minimization of DFA ( please visit https://www.geeksforgeeks.org/minimization-
of-dfa/ to know about Equivalence/Partitioning Method).

Steps for the Minimization of DFA :

1. Create the pairs of all the states involved in the given DFA.
2. Mark all the pairs (Qa,Qb) such a that Qa is Final state and Qb is Non-Final
State.
3. If there is any unmarked pair (Qa,Qb) such a that δ(Qa,x) and δ(Qb,x) is
marked, then mark (Qa,Qb). Here x is a input symbol. Repeat this step until
no more marking can be made.
4. Combine all the unmarked pairs and make them a single state in the
minimized DFA.

Example

Consider the following DFA,

Following is the transition table for the above DFA


Minimizing the above DFA using Myhill-Nerode Theorem :

Step-1: Create the pairs of all the states involved in DFA.

Step-2: Mark all the pairs (Qa,Qb) such a that Qa is Final state and Qb is Non-
Final State.
Step-3: If there is any unmarked pair (Qa,Qb) such a that δ(Qa,x) and δ(Qb,x) is
marked, then mark (Qa,Qb). Here x is a input symbol. Repeat this step until no
more marking can be made.

 Check for the unmarked pair Q2,Q1


o Check when x=0 : δ(Q2,0) = Q4 and δ(Q1,0) = Q3, check if the pair
Q4,Q3 is marked and no it is not marked.
o Check when x=1 : δ(Q2,1) = Q3 and δ(Q1,1) = Q4, check if the pair
Q4,Q3 is marked and no it is not marked.
o Hence we cannot mark the pair Q2,Q1.
 Check for the unmarked pair Q3,Q0
o Check when x=0 : δ(Q3,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and no it is not marked.
o Check when x=1 : δ(Q3,1) = Q5 and δ(Q0,1) = Q2, check if the pair
Q5,Q2 is marked and no it is not marked.
o Hence we cannot mark the pair Q3,Q0.
 Check for the unmarked pair Q4,Q0
o Check when x=0 : δ(Q4,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and no it is not marked.
o Check when x=1 : δ(Q4,1) = Q5 and δ(Q0,1) = Q2, check if the pair
Q5,Q2 is marked and no it is not marked.
o Hence we cannot mark the pair Q4,Q0.
 Check for the unmarked pair Q4,Q3
o Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of state
Q5,Q5 don’t exists.
o Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of state
Q5,Q5 don’t exists.
o Hence we cannot mark the pair Q4,Q3.
 Check for the unmarked pair Q5,Q1
o Check when x=0 : δ(Q5,0) = Q5 and δ(Q1,0) = Q3, check if the pair
Q5,Q3 is marked and yes it is marked.
o Hence we can mark the pair Q5,Q1.

 Check for the unmarked pair Q5,Q2


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

 Check for the unmarked pair Q4,Q0


o Check when x=0 : δ(Q4,0) = Q5 and δ(Q0,0) = Q1, check if the pair
Q5,Q1 is marked and yes it is marked.
o Hence we cannot mark the pair Q4,Q0.
 Check for the unmarked pair Q4,Q3
o Check when x=0 : δ(Q4,0) = Q5 and δ(Q3,0) = Q5, Such pair of state
Q5,Q5 don’t exists.
o Check when x=1 : δ(Q4,1) = Q5 and δ(Q3,1) = Q5, Such pair of state
Q5,Q5 don’t exists.
o Hence we cannot mark the pair Q4,Q3.
 Now even though we repeat the procedure we cannot mark the pairs
Q2,Q1(since Q4,Q3 is not marked) and Q4,Q3(since Q5,Q5 such pair of
states does not exists.). Hence we stop here.

Step-4: Combine all the unmarked pairs and make them as a single state in the
minimized DFA.

 The unmarked Pairs are Q2,Q1 and Q4,Q3 hence we combine them.

Following is the Minimized DFA with Q1Q2 and Q3Q4 as the combined states.

 Q0 remains as our starting state.


 Q1 and Q2 were our final states so even we combine them they will remain
as the combined final state.
 Q5 is the another final state we have.
 If we check the original Transition Table
o δ(Q0,0) was Q1 and δ(Q1,1) was Q2. As the states are combined, the
transition of Q0 on both the inputs 0 and 1 will be to the state Q1Q2.
o δ(Q1,0) was Q3, δ(Q1,1) was Q4 and δ(Q2,0) was Q4, δ(Q1,1) was
Q3. As the states are combined, the transition of Q1Q2 on both the
inputs 0 and 1 will be to the state Q3Q4.
o δ(Q3,0) was Q5, δ(Q3,1) was Q5 and δ(Q4,0) was Q5, δ(Q4,1) was
Q5. As the states are combined, the transition of Q3Q4 on both the
inputs 0 and 1 will be to the state Q5.
o δ(Q5,0) was Q5 and δ(Q5,1) was Q5. Hence the transition of state Q5
on both the inputs will be to the state Q5 itself.

Transition table for Minimized DFA

UNIT-3

Context-Free Grammar

Definition − A context-free grammar (CFG) consisting of a finite set of grammar


rules is a quadruple (N, T, P, S) where

 N is a set of non-terminal symbols.


 T is a set of terminals where N ∩ T = NULL.
 P is a set of rules, P: N → (N ∪ T)*, i.e., the left-hand side of the production
rule P does have any right context or left context.
 S is the start symbol.

Example
 The grammar ({A}, {a, b, c}, P, A), P : A → aA, A → abc.
 The grammar ({S, a, b}, {a, b}, P, S), P: S → aSa, S → bSb, S → ε
 The grammar ({S, F}, {0, 1}, P, S), P: S → 00S | 11F, F → 00F | ε

Generation of Derivation Tree

A derivation tree or parse tree is an ordered rooted tree that graphically represents
the semantic information a string derived from a context-free grammar.

Representation Technique

 Root vertex − Must be labeled by the start symbol.


 Vertex − Labeled by a non-terminal symbol.
 Leaves − Labeled by a terminal symbol or ε.

If S → x1x2 …… xn is a production rule in a CFG, then the parse tree / derivation


tree will be as follows −

There are two different approaches to draw a derivation tree −

Top-down Approach −

 Starts with the starting symbol S


 Goes down to tree leaves using productions

Bottom-up Approach −

 Starts from tree leaves


 Proceeds upward to the root which is the starting symbol S
Derivation or Yield of a Tree

The derivation or the yield of a parse tree is the final string obtained by
concatenating the labels of the leaves of the tree from left to right, ignoring the
Nulls. However, if all the leaves are Null, derivation is Null.

Example

Let a CFG {N,T,P,S} be

N = {S}, T = {a, b}, Starting symbol = S, P = S → SS | aSb | ε

One derivation from the above CFG is “abaabb”

S → SS → aSbS → abS → abaSb → abaaSbb → abaabb


Sentential Form and Partial Derivation Tree

A partial derivation tree is a sub-tree of a derivation tree/parse tree such that either
all of its children are in the sub-tree or none of them are in the sub-tree.

Example

If in any CFG the productions are −

S → AB, A → aaA | ε, B → Bb| ε

the partial derivation tree can be the following −

If a partial derivation tree contains the root S, it is called a sentential form. The
above sub-tree is also in sentential form.

Leftmost and Rightmost Derivation of a String

 Leftmost derivation − A leftmost derivation is obtained by applying


production to the leftmost variable in each step.
 Rightmost derivation − A rightmost derivation is obtained by applying
production to the rightmost variable in each step.

Example

Let any set of production rules in a CFG be

X → X+X | X*X |X| a

over an alphabet {a}.


The leftmost derivation for the string "a+a*a" may be −

X → X+X → a+X → a + X*X → a+a*X → a+a*a

The stepwise derivation of the above string is shown as below −

The rightmost derivation for the above string "a+a*a" may be −

X → X*X → X*a → X+X*a → X+a*a → a+a*a

The stepwise derivation of the above string is shown as below −


Left and Right Recursive Grammars

In a context-free grammar G, if there is a production in the form X → Xa where X


is a non-terminal and ‘a’ is a string of terminals, it is called a left recursive
production. The grammar having a left recursive production is called a left
recursive grammar.

And if in a context-free grammar G, if there is a production is in the form X → aX


where X is a non-terminal and ‘a’ is a string of terminals, it is called a right
recursive production. The grammar having a right recursive production is called a
right recursive grammar.

CFG Simplification
In a CFG, it may happen that all the production rules and symbols are not needed
for the derivation of strings. Besides, there may be some null productions and unit
productions. Elimination of these productions and symbols is called simplification
of CFGs. Simplification essentially comprises of the following steps −

 Reduction of CFG
 Removal of Unit Productions
 Removal of Null Productions

Reduction of CFG

CFGs are reduced in two phases −

Phase 1 − Derivation of an equivalent grammar, G’, from the CFG, G, such that
each variable derives some terminal string.

Derivation Procedure −

Step 1 − Include all symbols, W1, that derive some terminal and initialize i=1.

Step 2 − Include all symbols, Wi+1, that derive Wi.

Step 3 − Increment i and repeat Step 2, until Wi+1 = Wi.

Step 4 − Include all production rules that have Wi in it.

Phase 2 − Derivation of an equivalent grammar, G”, from the CFG, G’, such that
each symbol appears in a sentential form.

Derivation Procedure −

Step 1 − Include the start symbol in Y1 and initialize i = 1.

Step 2 − Include all symbols, Yi+1, that can be derived from Yi and include all
production rules that have been applied.

Step 3 − Increment i and repeat Step 2, until Yi+1 = Yi.


Problem

Find a reduced grammar equivalent to the grammar G, having production rules, P:


S → AC | B, A → a, C → c | BC, E → aA | e

Solution

Phase 1 −

T = { a, c, e }

W1 = { A, C, E } from rules A → a, C → c and E → aA

W2 = { A, C, E } U { S } from rule S → AC

W3 = { A, C, E, S } U ∅

Since W2 = W3, we can derive G’ as −

G’ = { { A, C, E, S }, { a, c, e }, P, {S}}

where P: S → AC, A → a, C → c , E → aA | e

Phase 2 −

Y1 = { S }

Y2 = { S, A, C } from rule S → AC

Y3 = { S, A, C, a, c } from rules A → a and C → c

Y4 = { S, A, C, a, c }

Since Y3 = Y4, we can derive G” as −

G” = { { A, C, S }, { a, c }, P, {S}}

where P: S → AC, A → a, C → c
Removal of Unit Productions

Any production rule in the form A → B where A, B ∈ Non-terminal is called unit


production..

Removal Procedure −

Step 1 − To remove A → B, add production A → x to the grammar rule whenever


B → x occurs in the grammar. [x ∈ Terminal, x can be Null]

Step 2 − Delete A → B from the grammar.

Step 3 − Repeat from step 1 until all unit productions are removed.

Problem

Remove unit production from the following −

S → XY, X → a, Y → Z | b, Z → M, M → N, N → a

Solution −

There are 3 unit productions in the grammar −

Y → Z, Z → M, and M → N

At first, we will remove M → N.

As N → a, we add M → a, and M → N is removed.

The production set becomes

S → XY, X → a, Y → Z | b, Z → M, M → a, N → a

Now we will remove Z → M.

As M → a, we add Z→ a, and Z → M is removed.

The production set becomes

S → XY, X → a, Y → Z | b, Z → a, M → a, N → a
Now we will remove Y → Z.

As Z → a, we add Y→ a, and Y → Z is removed.

The production set becomes

S → XY, X → a, Y → a | b, Z → a, M → a, N → a

Now Z, M, and N are unreachable, hence we can remove those.

The final CFG is unit production free −

S → XY, X → a, Y → a | b

Removal of Null Productions

In a CFG, a non-terminal symbol ‘A’ is a nullable variable if there is a production


A → ε or there is a derivation that starts at A and finally ends up with

ε: A → .......… → ε

Removal Procedure

Step 1 − Find out nullable non-terminal variables which derive ε.

Step 2 − For each production A → a, construct all productions A → x where x is


obtained from ‘a’ by removing one or multiple non-terminals from Step 1.

Step 3 − Combine the original productions with the result of step 2 and remove ε -
productions.

Problem

Remove null production from the following −

S → ASA | aB | b, A → B, B → b | ∈

Solution −

There are two nullable variables − A and B

At first, we will remove B → ε.


After removing B → ε, the production set becomes −

S→ASA | aB | b | a, A ε B| b | &epsilon, B → b

Now we will remove A → ε.

After removing A → ε, the production set becomes −

S→ASA | aB | b | a | SA | AS | S, A → B| b, B → b

This is the final production set without null transition.

normal forms:

1. Chomsky Normal Form


2. Greibach Normal Form

A CFG(context free grammar) is in CNF(Chomsky normal form) if all


production rules satisfy one of the following conditions:

 Start symbol generating ε. For example, A → ε.


 A non-terminal generating two non-terminals. For example, S → AB.
 A non-terminal generating a terminal. For example, S → a.

A CFG is in Chomsky Normal Form if the Productions are in the following forms

 A→a
 A → BC
 S→ε

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

Chomsky Normal Form:

Algorithm to Convert into Chomsky Normal Form −

Step 1 − If the start symbol S occurs on some right side, create a new start symbol
S’ and a new production S’→ S.
Step 2 − Remove Null productions. (Using the Null production removal algorithm
discussed earlier)

Step 3 − Remove unit productions. (Using the Unit production removal algorithm
discussed earlier)

Step 4 − Replace each production A → B1…Bn where n > 2 with A → B1C where
C → B2 …Bn. Repeat this step for all productions having two or more symbols in
the right side.

Step 5 − If the right side of any production is in the form A → aB where a is a


terminal and A, B are non-terminal, then the production is replaced by A → XB
and X → a. Repeat this step for every production which is in the form A → aB.

Problem

Convert the following CFG into CNF

S → ASA | aB, A → B | S, B → b | ε

Solution

(1) Since S appears in R.H.S, we add a new state S0 and S0→S is added to the
production set and it becomes −

S0→S, S→ ASA | aB, A → B | S, B → b | ∈

(2) Now we will remove the null productions −

B → ∈ and A → ∈

After removing B → ε, the production set becomes −

S0→S, S→ ASA | aB | a, A → B | S | ∈, B → b

After removing A → ∈, the production set becomes −

S0→S, S→ ASA | aB | a | AS | SA | S, A → B | S, B → b

(3) Now we will remove the unit productions.

After removing S → S, the production set becomes −


S0→S, S→ ASA | aB | a | AS | SA, A → B | S, B → b

After removing S0→ S, the production set becomes −

S0→ ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA

A → B | S, B → b

After removing A→ B, the production set becomes −

S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA

A→S|b

B→b

After removing A→ S, the production set becomes −

S0 → ASA | aB | a | AS | SA, S→ ASA | aB | a | AS | SA

A → b |ASA | aB | a | AS | SA, B → b

(4) Now we will find out more than two variables in the R.H.S

Here, S0→ ASA, S → ASA, A→ ASA violates two Non-terminals in R.H.S.

Hence we will apply step 4 and step 5 to get the following final production set
which is in CNF −

S0→ AX | aB | a | AS | SA

S→ AX | aB | a | AS | SA

A → b |AX | aB | a | AS | SA

B→b

X → SA

(5) We have to change the productions S0→ aB, S→ aB, A→ aB

And the final production set becomes −


S0→ AX | YB | a | AS | SA

S→ AX | YB | a | AS | SA

A → b A → b |AX | YB | a | AS | SA

B→b

X → SA

Y→a

A CFG is in Greibach Normal Form if the Productions are in the following forms −

A→b

A → bD1…Dn

S→ε

where A, D1,....,Dn are non-terminals and b is a terminal.

Greibach Normal Form:

Algorithm to Convert a CFG into Greibach Normal Form

Step 1 − If the start symbol S occurs on some right side, create a new start symbol
S’ and a new production S’ → S.

Step 2 − Remove Null productions. (Using the Null production removal algorithm
discussed earlier)

Step 3 − Remove unit productions. (Using the Unit production removal algorithm
discussed earlier)

Step 4 − Remove all direct and indirect left-recursion.

Step 5 − Do proper substitutions of productions to convert it into the proper form


of GNF.
Problem

Convert the following CFG into CNF

S → XY | Xn | p

X → mX | m

Y → Xn | o

Solution

Here, S does not appear on the right side of any production and there are no unit or
null productions in the production rule set. So, we can skip Step 1 to Step 3.

Step 4

Now after replacing

X in S → XY | Xo | p

with

mX | m

we obtain

S → mXY | mY | mXo | mo | p.

And after replacing

X in Y → Xn | o

with the right side of

X → mX | m

we obtain

Y → mXn | mn | o.
Two new productions O → o and P → p are added to the production set and then
we came to the final GNF as the following −

S → mXY | mY | mXC | mC | p

X → mX | m

Y → mXD | mD | o

O→o P→p

Pumping Lemma for CFG:

Lemma

If L is a context-free language, there is a pumping length p such that any string w


∈ L of length ≥ p can be written as w = uvxyz, where vy ≠ ε, |vxy| ≤ p, and for all i
≥ 0, uvixyiz ∈ L.

Applications of Pumping Lemma

Pumping lemma is used to check whether a grammar is context free or not. Let us
take an example and show how it is checked.

Problem

Find out whether the language L = {xnynzn | n ≥ 1} is context free or not.

Solution

Let L is context free. Then, L must satisfy pumping lemma.

At first, choose a number n of the pumping lemma. Then, take z as 0n1n2n.

Break z into uvwxy, where

|vwx| ≤ n and vx ≠ ε.

Hence vwx cannot involve both 0s and 2s, since the last 0 and the first 2 are at least
(n+1) positions apart. There are two cases −
Case 1 − vwx has no 2s. Then vx has only 0s and 1s. Then uwy, which would have
to be in L, has n 2s, but fewer than n 0s or 1s.

Case 2 − vwx has no 0s.

Here contradiction occurs.

Hence, L is not a context-free language.

closure properties for context free language(grammer)

The closure properties for context free language (CFG) are as follows −

Closed under Union Operation

n order to show that context-free language is closed under union operation,


consider two starting variables S1 and S2 for the two different languages L1 and
L2.

Grammar for union operation is as shown below −

S ->S1|S2

If both the languages belong to the context free language then union of both the
languages should belong to context free language.

By the above definition if a user generates S1 and S2 string or both then in that
case union of both the language is generated.

Hence, L1 U L2 ∈ CFL

So, context free language is closed under union operation.

Closed under Concatenation

In order to show that context free language is closed under concatenation, the
operation considers two starting variables S1 and for the two different languages
L1 and L2.

Grammar for union operation is as shown below −

S->S1S2−
If both the language belongs to the context free language then concatenate one of
both the language should belong to context free language.

∀L1L2∈CFL

{W1W2:W1∈L1∈ΛW2∈L2}∈CFL

By the above definition if a user generates S1 string for language L1 followed by


S2 string of language. Then, its concatenation of both languages is generated.

Hence, the result is as follows −

{W1W2:W1∈L1∈ΛW2∈L2}∈CFL

So, context free language is closed under concatenation operation.

Closed under Star operation

In order to show that context free language is closed under star operation. Consider
one start variable S1 for the languages L1

Grammar for union operation is as shown below −

S->S1S| ∈

If the language belongs to the context free language then the star of the language
should belong to the context free language.

∀L1∈CFL

By the above definition, if the user generates zero or many strings which is the
definition of the star. So, context free language is closed under star operation.

decision problem for CFG

A decision problem Q is a set of questions, each having either yes or no answer.


Let us consider the question is “is 10 a perfect square?” which is an example for a
decision problem. A decision problem generally consists of an infinite number of
related questions.
Example

The problem PSQ which determines whether an arbitrary natural number is a


perfect square or not having some doubtful questions like the following −

 q0: Is 0 a perfect square?


 q1: Is 1 a perfect square?
 q2: Is 2 a perfect square?

Solution

A solution to a decision problem Q is an algorithm which determines the


approximate answer to every question q ϵ Q.

An algorithm which solves a decision problem has to be the following −

 Complete.
 Mechanistic.
 Deterministic.

A procedure which satisfies the preceding properties is generally called effective.

A problem is called as a decidable if it has a representation in which the set of


accepted input strings from a recursive language.

Since computations of deterministic multi track and multi tape machines are
simulated on a standard Turing machine, a solution using these machines also
establishes the decidability of a problem.

The decision problem will be as follows −


UNIT_ 4

Pushdown Automata
Basic Structure of PDA

A pushdown automaton is a way to implement a context-free grammar in a similar


way we design DFA for a regular grammar. A DFA can remember a finite amount
of information, but a PDA can remember an infinite amount of information.

Basically a pushdown automaton is −

"Finite state machine" + "a stack"

A pushdown automaton has three components −

 an input tape,
 a control unit, and
 a stack with infinite size.

The stack head scans the top symbol of the stack.

A stack does two operations −

 Push − a new symbol is added at the top.


 Pop − the top symbol is read and removed.

A PDA may or may not read an input symbol, but it has to read the top of the stack
in every transition.
A PDA can be formally described as a 7-tuple (Q, ∑, S, δ, q0, I, F) −

 Q is the finite number of states


 ∑ is input alphabet
 S is stack symbols
 δ is the transition function: Q × (∑ ∪ {ε}) × S × Q × S*
 q0 is the initial state (q0 ∈ Q)
 I is the initial stack top symbol (I ∈ S)
 F is a set of accepting states (F ∈ Q)

The following diagram shows a transition in a PDA from a state q1 to state q2,
labeled as a,b → c −

This means at state q1, if we encounter an input string ‘a’ and top symbol of the
stack is ‘b’, then we pop ‘b’, push ‘c’ on top of the stack and move to state q2.

Terminologies Related to PDA

Instantaneous Description

The instantaneous description (ID) of a PDA is represented by a triplet (q, w, s)


where

 q is the state
 w is unconsumed input
 s is the stack contents
Turnstile Notation

The "turnstile" notation is used for connecting pairs of ID's that represent one or
many moves of a PDA. The process of transition is denoted by the turnstile symbol
"⊢".

Consider a PDA (Q, ∑, S, δ, q0, I, F). A transition can be mathematically


represented by the following turnstile notation −

(p, aw, Tβ) ⊢ (q, w, αb)

This implies that while taking a transition from state p to state q, the input symbol
‘a’ is consumed, and the top of the stack ‘T’ is replaced by a new string ‘α’.

Note − If we want zero or more moves of a PDA, we have to use the symbol (⊢*)
for it.

Instantaneous description of PDA

The Instantaneous description is called as an informal notation, and explains how a


Push down automata (PDA) computes the given input string and makes a decision
that the given string is accepted or rejected.

 The PDA involves both state and content of the stack.


 Stack is often one of the important parts of PDA at any time.
 So, we make a convenient notation for describing the successive
configurations of PDA for string processing.
 The factors of PDA notation by triple (q, w, γ) were
o q is the current state.
o w is the remaining input alphabet.
o γ is the current contents of the PDA stack.

Generally, the leftmost symbol indicates the top of the stack γ and the bottom at
the right end. This type of triple notation is called an instantaneous description or
ID of the pushdown automata.

A move from one instantaneous description to another is denoted by the symbol


‘⊢’

Therefore,
(q0, aw, z0) ⊢ (q1, w, yz0)

is possible if and only if

δ(q0, a, z0) ϵ (q1, yz0).


Example

Consider an example as given below −

Show the IDs or moves for input string w = “aabb” of PDA where,

M = ({q0, q1, q2}, {a, b}, {a, b, Z0}, δ, q0, Z0, {q2}),

Where δ is defined as follows −

δ(q0, a, Z0) = {(q0, aZ0)} Rule (1)


δ(q0, a, a) = {(q0, aa)} Rule (2)
δ(q0, b, a) = {(q1, λ)} Rule (3)
δ(q1, b, a) = {(q1, λ)} Rule (4)
δ(q1, λ, Z0) = {(q2, λ)} Rule (5)
δ(q0, λ, Z0) = {(q2, λ)} Rule (6)

And we need to find out whether string w is accepted by PDA or not.

Solution

The Instantaneous Description for the string w = “aabb”. It is explained below −

(q0, aabb, Z0)


|- (q0, abb, aZ0) based on Rule (1)
|- (q0, bb, aaZ0) based on Rule (2)
|- (q1, b, aZ0 based on Rule (3)
|- (q1, λ, Z0) based on Rule (3)
|- (q2, λ, λ) based on Rule (5)

Therefore, PDA reaches a configuration of (q2, λ, λ) i.e. PDA stack is empty and it
has reached a final state. So the string ‘w’ is accepted.

PDA & Context-Free Grammar


If a grammar G is context-free, we can build an equivalent nondeterministic PDA
which accepts the language that is produced by the context-free grammar G. A
parser can be built for the grammar G.

Also, if P is a pushdown automaton, an equivalent context-free grammar G can be


constructed where

L(G) = L(P)

In the next two topics, we will discuss how to convert from PDA to CFG and vice
versa.

Algorithm to find PDA corresponding to a given CFG

Input − A CFG, G = (V, T, P, S)

Output − Equivalent PDA, P = (Q, ∑, S, δ, q0, I, F)

Step 1 − Convert the productions of the CFG into GNF.

Step 2 − The PDA will have only one state {q}.

Step 3 − The start symbol of CFG will be the start symbol in the PDA.

Step 4 − All non-terminals of the CFG will be the stack symbols of the PDA and
all the terminals of the CFG will be the input symbols of the PDA.

Step 5 − For each production in the form A → aX where a is terminal and A, X are
combination of terminal and non-terminals, make a transition δ (q, a, A).

Problem

Construct a PDA from the following CFG.

G = ({S, X}, {a, b}, P, S)

where the productions are −

S → XS | ε , A → aXb | Ab | ab
Solution

Let the equivalent PDA,

P = ({q}, {a, b}, {a, b, X, S}, δ, q, S)

where δ −

δ(q, ε , S) = {(q, XS), (q, ε )}

δ(q, ε , X) = {(q, aXb), (q, Xb), (q, ab)}

δ(q, a, a) = {(q, ε )}

δ(q, 1, 1) = {(q, ε )}

Algorithm to find CFG corresponding to a given PDA

Input − A CFG, G = (V, T, P, S)

Output − Equivalent PDA, P = (Q, ∑, S, δ, q0, I, F) such that the non- terminals of
the grammar G will be {Xwx | w,x ∈ Q} and the start state will be Aq0,F.

Step 1 − For every w, x, y, z ∈ Q, m ∈ S and a, b ∈ ∑, if δ (w, a, ε) contains (y, m)


and (z, b, m) contains (x, ε), add the production rule Xwx → a Xyzb in grammar G.

Step 2 − For every w, x, y, z ∈ Q, add the production rule Xwx → XwyXyx in


grammar G.

Step 3 − For w ∈ Q, add the production rule Xww → ε in grammar G.

Parsing &Pushdown Automata

Parsing is used to derive a string using the production rules of a grammar. It is used
to check the acceptability of a string. Compiler is used to check whether or not a
string is syntactically correct. A parser takes the inputs and builds a parse tree.

A parser can be of two types −


 Top-Down Parser − Top-down parsing starts from the top with the start-
symbol and derives a string using a parse tree.
 Bottom-Up Parser − Bottom-up parsing starts from the bottom with the
string and comes to the start symbol using a parse tree.

Design of Top-Down Parser

For top-down parsing, a PDA has the following four types of transitions −

 Pop the non-terminal on the left hand side of the production at the top of the
stack and push its right-hand side string.
 If the top symbol of the stack matches with the input symbol being read, pop
it.
 Push the start symbol ‘S’ into the stack.
 If the input string is fully read and the stack is empty, go to the final state
‘F’.

Example

Design a top-down parser for the expression "x+y*z" for the grammar G with the
following production rules −

P: S → S+X | X, X → X*Y | Y, Y → (S) | id

Solution

If the PDA is (Q, ∑, S, δ, q0, I, F), then the top-down parsing is −

(x+y*z, I) ⊢(x +y*z, SI) ⊢ (x+y*z, S+XI) ⊢(x+y*z, X+XI)

⊢(x+y*z, Y+X I) ⊢(x+y*z, x+XI) ⊢(+y*z, +XI) ⊢ (y*z, XI)

⊢(y*z, X*YI) ⊢(y*z, y*YI) ⊢(*z,*YI) ⊢(z, YI) ⊢(z, zI) ⊢(ε, I)

Design of a Bottom-Up Parser

For bottom-up parsing, a PDA has the following four types of transitions −

 Push the current input symbol into the stack.


 Replace the right-hand side of a production at the top of the stack with its
left-hand side.
 If the top of the stack element matches with the current input symbol, pop it.
 If the input string is fully read and only if the start symbol ‘S’ remains in the
stack, pop it and go to the final state ‘F’.

Example

Design a top-down parser for the expression "x+y*z" for the grammar G with the
following production rules −

P: S → S+X | X, X → X*Y | Y, Y → (S) | id

Solution

If the PDA is (Q, ∑, S, δ, q0, I, F), then the bottom-up parsing is −

(x+y*z, I) ⊢ (+y*z, xI) ⊢ (+y*z, YI) ⊢ (+y*z, XI) ⊢ (+y*z, SI)

⊢(y*z, +SI) ⊢ (*z, y+SI) ⊢ (*z, Y+SI) ⊢ (*z, X+SI) ⊢ (z, *X+SI)

⊢ (ε, z*X+SI) ⊢ (ε, Y*X+SI) ⊢ (ε, X+SI) ⊢ (ε, SI)

UNIT -5

Turing Machine

A Turing Machine is an accepting device which accepts the languages (recursively


enumerable set) generated by type 0 grammars. It was invented in 1936 by Alan
Turing.

Definition

A Turing Machine (TM) is a mathematical model which consists of an infinite


length tape divided into cells on which input is given. It consists of a head which
reads the input tape. A state register stores the state of the Turing machine. After
reading an input symbol, it is replaced with another symbol, its internal state is
changed, and it moves from one cell to the right or left. If the TM reaches the final
state, the input string is accepted, otherwise rejected.

A TM can be formally described as a 7-tuple (Q, X, ∑, δ, q0, B, F) where −

 Q is a finite set of states


 X is the tape alphabet
 ∑ is the input alphabet
 δ is a transition function; δ : Q × X → Q × X × {Left_shift, Right_shift}.
 q0 is the initial state
 B is the blank symbol
 F is the set of final states

A Turing Machine is an accepting device which accepts the languages (recursively


enumerable set) generated by type 0 grammars. It was invented in 1936 by Alan
Turing.

Definition

A Turing Machine (TM) is a mathematical model which consists of an infinite


length tape divided into cells on which input is given. It consists of a head which
reads the input tape. A state register stores the state of the Turing machine. After
reading an input symbol, it is replaced with another symbol, its internal state is
changed, and it moves from one cell to the right or left. If the TM reaches the final
state, the input string is accepted, otherwise rejected.

A TM can be formally described as a 7-tuple (Q, X, ∑, δ, q0, B, F) where −

 Q is a finite set of states


 X is the tape alphabet
 ∑ is the input alphabet
 δ is a transition function; δ : Q × X → Q × X × {Left_shift, Right_shift}.
 q0 is the initial state
 B is the blank symbol
 F is the set of final states

Design & techniques for constructing Turing Machines:

Multi-tape Turing Machine

Multi-track Turing Machine

Non-Deterministic Turing Machine

Semi-Infinite Tape Turing Machine

1. Multi-tape Turing Machine


Multi-tape Turing Machines have multiple tapes where each tape is accessed with
a separate head. Each head can move independently of the other heads. Initially the
input is on tape 1 and others are blank. At first, the first tape is occupied by the
input and the other tapes are kept blank. Next, the machine reads consecutive
symbols under its heads and the TM prints a symbol on each tape and moves its
heads.

A Multi-tape Turing machine can be formally described as a 6-tuple (Q, X, B, δ,


q0, F) where −

 Q is a finite set of states


 X is the tape alphabet
 B is the blank symbol
 δ is a relation on states and symbols where

δ: Q × Xk → Q × (X × {Left_shift, Right_shift, No_shift })k

where there is k number of tapes

 q0 is the initial state


 F is the set of final states

Note − Every Multi-tape Turing machine has an equivalent single-tape Turing


machine

2. Multi-track Turing Machine


Multi-track Turing machines, a specific type of Multi-tape Turing machine,
contain multiple tracks but just one tape head reads and writes on all tracks. Here, a
single tape head reads n symbols from n tracks at one step. It accepts recursively
enumerable languages like a normal single-track single-tape Turing Machine
accepts.

A Multi-track Turing machine can be formally described as a 6-tuple (Q, X, ∑, δ,


q0, F) where −

 Q is a finite set of states


 X is the tape alphabet
 ∑ is the input alphabet
 δ is a relation on states and symbols where

δ(Qi, [a1, a2, a3,....]) = (Qj, [b1, b2, b3,....], Left_shift or Right_shift)

 q0 is the initial state


 F is the set of final states

Note − For every single-track Turing Machine S, there is an equivalent multi-track


Turing Machine M such that L(S) = L(M).

3. Non-Deterministic Turing Machine

In a Non-Deterministic Turing Machine, for every state and symbol, there are a
group of actions the TM can have. So, here the transitions are not deterministic.
The computation of a non-deterministic Turing Machine is a tree of configurations
that can be reached from the start configuration.

An input is accepted if there is at least one node of the tree which is an accept
configuration, otherwise it is not accepted. If all branches of the computational tree
halt on all inputs, the non-deterministic Turing Machine is called a Decider and if
for some input, all branches are rejected, the input is also rejected.

A non-deterministic Turing machine can be formally defined as a 6-tuple (Q, X, ∑,


δ, q0, B, F) where −

 Q is a finite set of states


 X is the tape alphabet
 ∑ is the input alphabet
 δ is a transition function;
δ : Q × X → P(Q × X × {Left_shift, Right_shift}).

 q0 is the initial state


 B is the blank symbol
 F is the set of final states

4. Semi-Infinite Tape Turing Machine

A Turing Machine with a semi-infinite tape has a left end but no right end. The left
end is limited with an end marker.

It is a two-track tape −

 Upper track − It represents the cells to the right of the initial head position.
 Lower track − It represents the cells to the left of the initial head position in
reverse order.

The infinite length input string is initially written on the tape in contiguous tape
cells.

The machine starts from the initial state q0 and the head scans from the left end
marker ‘End’. In each step, it reads the symbol on the tape under its head. It writes
a new symbol on that tape cell and then it moves the head either into left or right
one tape cell. A transition function determines the actions to be taken.

It has two special states called accept state and reject state. If at any point of time
it enters into the accepted state, the input is accepted and if it enters into the reject
state, the input is rejected by the TM. In some cases, it continues to run infinitely
without being accepted or rejected for some certain input symbols.

Note − Turing machines with semi-infinite tape are equivalent to standard Turing
machines.
Combining turing machines (Linear Bounded Automata)

A linear bounded automaton is a multi-track non-deterministic Turing machine


with a tape of some bounded finite length.

Length = function (Length of the initial input string, constant c)

Here,

Memory information ≤ c × Input information

The computation is restricted to the constant bounded area. The input alphabet
contains two special symbols which serve as left end markers and right end
markers which mean the transitions neither move to the left of the left end marker
nor to the right of the right end marker of the tape.

A linear bounded automaton can be defined as an 8-tuple (Q, X, ∑, q0, ML, MR, δ,
F) where −

 Q is a finite set of states


 X is the tape alphabet
 ∑ is the input alphabet
 q0 is the initial state
 ML is the left end marker
 MR is the right end marker where MR ≠ ML
 δ is a transition function which maps each pair (state, tape symbol) to (state,
tape symbol, Constant ‘c’) where c can be 0 or +1 or -1
 F is the set of final states

A deterministic linear bounded automaton is always context-sensitive and the


linear bounded automaton with empty language is undecidable..
UNIT -6

universal Turing machine

The Turing Machine (TM) is the machine level equivalent to a digital computer.

It was suggested by the mathematician Turing in the year 1930 and has become the
most widely used model of computation in computability and complexity theory.

The model consists of an input and output. The input is given in binary format
form on to the machine’s tape and the output consists of the contents of the tape
when the machine halts

The problem with the Turing machine is that a different machine must be
constructed for every new computation to be performed for every input output
relation.

This is the reason the Universal Turing machine was introduced which along with
input on the tape takes the description of a machine M.

The Universal Turing machine can go on then to simulate M on the rest of the
content of the input tape.

A Universal Turing machine can thus simulate any other machine.

The idea of connecting multiple Turing machine gave an idea to Turing −

 Can a Universal machine be created that can ‘simulate’ other machines?


 This machine is called as Universal Turing Machine

This machine would have three bits of information for the machine it is simulating

 A basic description of the machine.


 The contents of machine tape.
 The internal state of the machine.

The Universal machine would simulate the machine by looking at the input on the
tape and the state of the machine.
It would control the machine by changing its state based on the input. This leads to
the idea of a “computer running another computer”.

It would control the machine by changing its state based on the input. This leads to
the idea of a “computer running another computer”.

The schematic diagram of the Universal Turing Machine is as follows −

Halting Problem

Usually, programs consist of loops that are limited or unlimited in length.

The total work done by the program completely depends on the input given to the
program.

The program may consist of several different numbers of loops that may be in
linear or nested manner.

The Halting Problem is the problem of deciding or concluding based on a given


arbitrary computer program and its input, whether that program will stop executing
or run-in an infinite loop for the given input.

The Halting Problem tells that it is not easy to write a computer program that
executes in the limited time that is capable of deciding whether a program halts for
an input.
In addition to that the Halting Problem never says that it is not practicable to
determine whether a given random program is going to halt (stop).

Generally, it asks the question like “Given a random program and an input,
conclude whether the given random program is going to halt when that input is
given”.

Write a Halting Problem

An example of writing the Halting Problem is as follows −

INPUT − Program P and a string S.

OUTPUT − if P stops on S, it returns 1.

Otherwise, if P enters into an endless loop on S, it returns 0.

Let us consider the Halting Problem called H having the solution.

Now H takes the following two inputs −

 Program P
 Input S.

If P stops on S, then H results in “halt”, otherwise H gives the result “loop”.

The diagrammatic representation of H is as follows −

Example

ATM = {(M,w) | M is a TM and M halts at input w }.

We can build a universal Turing machine which can simulate any Turing machine
on any input.

Let’s consider TM which recognizing the Altering Turing Machine (ATM) −


Recognize-ATM (<M,w>)
Simulate M using UTM till it halts
If M halts and accept then
Accept
Else
Reject

Suppose, if M goes into an infinite loop on input w, then the TM Recognize-ATM


is going to run forever which means TM is only a recognizer, not a decider.

A decider for this problem would call a halt to simulations that loop forever.

Now the question is whether an ATM is TM decidable is equivalent to asking the


question whether we can tell if a TM M will halt on input w.

Because of this, both versions of this question are generally called the halting
problem.

Turing machine variations (Types)

Turing machines (TM) can also be deterministic or non-deterministic, but this does
not make them any more or less powerful.

However, if the tape is restricted so that you can only see use of the part of the tape
with the input, the TM becomes less powerful (linear bounded automata) and can
only recognise context sensitive languages.

Many other TM variations are equivalent to the original TM. This includes the
following −

 Multi-track
 Multi-tape
 Multi-head
 Multi-dimensional tape
 The off-line Turing machine

Multi-tape Turing Machine

A Turing machine with several tapes we call it a multi tape Turing machine.

Every tape’s have their own Read/Write head


For N-tape Turing Machine

M={( Q,X, ∑,δ,q0,B,F)}

We define

δ=QxXN ->Q x XN x {L,R}N

Example

If n=2 with current configuration δ(q0,a,e)=(q1,X,Y,L,R)

δ=QxXN ->Q x XN x {L,R}N

Non Deterministic Turing Machine

It is similar to DTM except that for any input and current state it has a number of
choices.

A string is accepted by a NDTM if there is a sequence of moves that leads to a


final state

The Transition function −

=Q x X ->2QxXx(L,R)

A NDTM is allowed to have more than one transition for a given tape symbol.
Multi-head Turing machine

It has a number of heads instead of one.

Each head independently reads/ writes symbols and moves left/right or keeps
stationery.

Off-line Turing Machine

An offline Turing machine has two tapes, which are as follows −

 One tape is read-only and contains the input.


 The other is read-write and is initially blank.
Restricted Turing Machines

The restricted Turing machines can be of the following types :

1. Halting Turing Machine :


A Turing Machine is said to be a halting Turing machine if it always halts for
every input string. It can accept the recursive language and is less powerful
than Turing machine.
2. Linear Bounded Automata :
It behaves as a Turing machine but the storage space of tape is restricted
only to the length of the input string. It is less powerful than a Turing
machine but more powerful than push down automata.
3. Unidirectional Turing Machine :
The head of this type of turing machine can move only in one direction. It
can accept the only regular language. It has the same power as finite
automata but less powerful than push down automata.
4. Read Only Turing Machine :
It is equivalent to finite automata. It contains a read head only which
doesn’t have written capability. It accepts only regular languages.
5. Read Only-Unidirectional Turing Machine :
It is similar to finite automata. It contains a read-only head and can move
only in one direction. It accepts a regular language.

Decidable and undecidable problems:

Decidable Language

A language L is called decidable if there is a decider M such that L( M) = L.

 Given a decider M, you can learn whether or not a string w ∈ L(M).


o Run M on w.
o Although it might take a long time, M will accept or reject w.
 The set R is the set of all decidable languages.
o L ∈ R if L is decidable.

Undecidable Language

A decision problem P is undecidable if the language L of all yes instances to P is


not decidable.
An undecidable language may be partially decidable but not decidable. Suppose, if
a language is not even partially decidable, then there is no Turing machine that
exists for the respective language.

Problem

Find whether the problem given below is decidable or undecidable.

“Let the given input be some Turing Machine M and some string w. The problem
is to determine whether the machine M, executed on the input string w, ever moves
its readhead to the left for three delta rules in a row.”

Solution

Define M' is a Turing machine that takes a pair (M,w) as input, where M is a
Turing machine recognized by M' and w is the input to M.

Whenever the head of simulated machine M moves to left while processing input
w , M' stops and accepts (M,w)

For a particular input to M' (M,w), construction the Turing machine P is −

 P executes M' on (M,w)


 P stops and accepts any input if M' accepts (M,w)

We have tried to reduce the Universal Turing Machine U to P, because we know


that L(U) is not decidable, and also we conclude that L(P) is not decidable.
Consequently, M' is not decidable.

The proof is by contradiction.

Let us assume that this problem is decidable, and then we have to show that the
altering turing machine (ATM) is also decidable −

ATM = { M is a TM and M accepts w}.

Let R be a Turing machine which decides the leftmost problem.

That is, R decides the language


leftmost = { M on input w ever attempts to move its head left when it's head is on
the left-most tape cell }.

Now, the idea is to construct a Turing machine S which decides ATM in such a
way that it uses R.

On input, S first modifies machine M to M´, so that M´ moves its head to the left
from the left-most cell only when M accepts its input.

To ensure that during its computation M´ does not move the head left from the left-
most position,

First machine M´ shifts the input w one position to the right, and places a special
symbol on the left-most tape cell. The computation of M´ starts with the head on
the second tape cell.

During its computation M´ ever attempts to move its head to the left-most tape cell,
M´ finds out by reading the special symbol and puts the head back to the second
cell, and continues its execution. If M enters an accept state, then M´ enters a loop
that forces the head to always move to the left.

After S has constructed M´ it runs the decider R on input < M´; w>.

If R accepts then S accepts, otherwise if R rejects then S rejects.

Therefore, ATM is decidable, which is a contradiction.

UNIT – 7

Chomsky hierarchy:

Chomsky Hierarchy represents the class of languages that are accepted by the
different machines.

Chomsky hierarchy

Hierarchy of grammars according to Chomsky is explained below as per the


grammar types −

Type 0 − It is an Unrestricted grammars


Unrestricted grammar − an unrestricted grammar is a 4-tuple (T,N,P,S), which
consisting of −

T = set of terminals

N = set of nonterminal

P =as set of productions, of the form −

v->w

where v and w are strings consisting of nonterminal and terminals.

S = is called the start symbol.

Example − Turing Machine (TM)

Type 1 − Context-sensitive grammars

All productions are of form,

v -> w where |v| < |w|

uAv -> uwv with w != epsilon,

I.e., A -> w but only in the context of u _ v.

A context-sensitive grammar is equivalent to a linear bounded and context-


sensitive language.

Example − Linear Bounded Automaton (LBA)

Type 3 − Context-free grammars −

All productions are of the form −

A -> x − where A is nonterminal, x is a string of nonterminal and terminals, A


context-free grammar is equivalent to a pushdown automaton (PDA) and to
context free languages.

Example − Pushdown Automaton (PDA)


Type 3 − Regular grammars

All productions are of the form −

A -> xB

A -> x where A, B are nonterminal, x is a string of terminals.

Regular grammars are equivalent to regular sets and to finite automata.

Example − Finite Automaton (FA)

Grammar
Grammar accepted Language accepted Automaton
type

unrestricted recursively enumerable


Type 0 Turing Machine
grammar language

context-sensitive context-sensitive linear-bounded


Type 1
grammar language automata

Context-free Push down


Type 2 Context-free language
grammar automata

finite state
Type 3 regular grammar regular language
automaton

The Chomsky hierarchy is depicted in the diagram given below −


Regular grammer or language:

A Regular Expression can be recursively defined as follows −

 ε is a Regular Expression indicates the language containing an empty string.


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

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

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

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

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

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

Set consisting of even number of 1’s including empty string,


(11)*
So L= {ε, 11, 1111, 111111, ……….}

Set of strings consisting of even number of a’s followed by


(aa)*(bb)*b odd number of b’s , so L = {b, aab, aabbb, aabbbbb, aaaab,
aaaabbb, …………..}

String of a’s and b’s of even length can be obtained by


(aa + ab + ba + bb)* concatenating any combination of the strings aa, ab, ba and
bb including null, so L = {aa, ab, ba, bb, aaab, aaba, …………..}

Context-Sensitive Grammar

A context-sensitive grammar whose productions are of the form

αAβ → αγβ

Where α, β ∈ (N ∪ T)*, A ∈ N; γ ∈ (N ∪ T)+ and a rule of the form S → λ is


allowed if the start symbol S do not appear on the right hand side of any rule.

The language generated by such a grammar is called a context-sensitive language.


 Every context-free grammar is also context-sensitive =⇒ the context-free
languages are a subset of the context-sensitive languages (see Chomsky
Normal Form).
 But, not every context-sensitive language is context-free.

Example

The language {anbncn, n > 1} is context-sensitive but not context free.

A grammar for this language is given by:


S → aSBC | aBC,
CB → HB,
HB → HC,
HC → BC,
aB → ab,
bB → bb,
bC → bc,
cC → cc
A derivation e.g. aabbcc, using this grammar is:
S ⇒ aSBC
⇒ aaBCBC (using S → aBC)
⇒ aabCBC (using aB → ab)
⇒ aabHBC (using CB → HB)
⇒ aabHCC (using HB → HC)
⇒ aabBCC (using HC → BC)
⇒ aabbCC (using bB → bb)
⇒ aabbcC (using bC → bc)
⇒ aabbcc (using cC → cc)

The context-sensitive languages can also be generated by a monotonic grammar,


any production is allowed, as long as there are no rules for making strings shorter
(except one S → ∧)!

unrestricted grammar:
In automata theory, the class of unrestricted grammars (also called semi-Thue,
type-0 or phrase structure grammars) is the most general class of grammars in
the Chomsky hierarchy. No restrictions are made on the productions of an
unrestricted grammar, other than each of their left-hand sides being non-empty.

*******************************

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