TOC Unit 3 PDF
TOC Unit 3 PDF
in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 1
Grammar
A grammar is a mechanism used for describing languages. This is one of the most simple but yet powerful
mechanism. There are other notions to do the same, of course.
be
.in
In everyday language, like English, we have a set of symbols (alphabet), a set of words constructed from these
symbols, and a set of rules using which we can group the words to construct meaningful sentences. The
grammar for English tells us what are the words in it and the rules to construct sentences. It also tells us
whether a particular sentence is well-formed (as per the grammar) or not. But even if one follows the rules of
the english grammar it may lead to some sentences which are not meaningful at all, because of impreciseness
and ambiguities involved in the language. In english grammar we use many other higher level constructs like
noun-phrase, verb-phrase, article, noun, predicate, verb etc. A typical rule can be defined as
< noun-phrase > < predicate >
tu
meaning that "a sentence can be constructed using a 'noun-phrase' followed by a predicate".
se
.c
If we take {a, an, the} to be <article>; cow, bird, boy, Ram, pen to be examples of <noun>; and eats, runs,
swims, walks, are associated with <verb>, then we can construct the sentence- a cow runs, the boy eats, an
pen walks- using the above rules. Even though all sentences are well-formed, the last one is not meaningful.
We observe that we start with the higher level construct <sentence> and then reduce it to <noun-phrase>,
<article>, <noun>, <verb> successively, eventually leading to a group of words associated with these
constructs.
These concepts are generalized in formal language leading to formal grammars. The word 'formal' here refers
to the fact that the specified rules for the language are explicitly stated in terms of what strings or symbols can
occur. There can be no ambiguity in it.
Formal definitions of a Grammar
A grammar G is defined as a quadruple.
www.csetube.in
www.csetube.in
is a non-empty finite set of terminal symbols such that
is a
, i.e.
iff
and
, where
.in
Production rules:
The production rules specify how the grammar transforms one string to another. Given a string
(in
) to
. We say that
derives
to rewrite
and is denoted as
be
the
, we say
if
can be
se
We write
derived from
tu
Successive strings are dervied by applying the productions rules of the grammar in any arbitrary order. A
particular rule can be used if it is applicable, and it can be applied as many times as described.
.c
By applying the production rules in arbitrary order, any given grammar can generate many strings of terminal
symbols starting with the special start symbol, S, of the grammar. The set of all such terminal strings is called
the language generated (or defined) by the grammar.
That is
If
iff
, denoted as a
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
Period: 2
, where N = {S},
ab, S
of
aSb}
ab
aSb
aabb
aSb
aaSbb
be
.in
Some terminal strings generated by this grammar together with their derivation is given below.
aaabbb
se
tu
. When the non-terminal is replaced by ab (which is then only possibility for generating a
form
.c
There is no general rule for finding a grammar for a given language. For many languages we can devise
grammars and there are many languages for which we cannot find any grammar.
It is possible to find a grammar for L by modifying the previous grammar since we need to generate an extra b
at the end of the string
generates
where, N = { S, B }, P = { S
Bb, B
ab, B
www.csetube.in
aBb }
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 3
.in
Parse Trees:
.c
se
tu
be
www.csetube.in
.c
se
tu
be
.in
www.csetube.in
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 4
be
.in
.c
se
tu
www.csetube.in
.c
se
tu
be
.in
www.csetube.in
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 5
be
.in
It is observed that FA have limited capability. (in the sense that the class of languages accepted or
characterized by them is small). This is due to the "finite memory" (number of states) and "no external memory"
involved with them. A PDA is simply an NFA augmented with an "external stack memory". The addition of a
stack provides the PDA with a last-in, first-out memory management cpapability. This "Stack" or "pushdown
store" can be used to record a potentially unbounded information. It is due to this memory management
capability with the help of the stack that a PDA can overcome the memory limitations that prevents a FA to
.c
se
tu
As shown in figure, a PDA has three components: an input tape with read only head, a finite control and a
pushdown store.
The input head is read-only and may only move from left to right, one symbol (or cell) at a time. In each step,
the PDA pops the top symbol off the stack; based on this symbol, the input symbol it is currently reading, and
its present state, it can push a sequence of symbols onto the stack, move its read-only head one cell (or
symbol) to the right, and enter a new state, as defined by the transition rules of the PDA.
PDA are nondeterministic, by default. That is, - transitions are also allowed in which the PDA can pop and
push, and change state without reading the next input symbol or moving its read-only head. Besides this, there
may be multiple options for possible next moves.
www.csetube.in
www.csetube.in
Formal Definitions : Formally, a PDA M is a 7-tuple M =
where,
to subset of
.in
be
go to state
pop z off the stack
push
, then
se
, then
.c
If a =
tu
state q with z on the top of the stack regardless of the current input symbol, it can nondeterministically for any
i,
go to state
push
onto the stack, and
leave its read-only head where it is.
State transition diagram : A PDA can also be depicted by a state transition diagram. The labels on the arcs
indicate both the input and the stack operation. The transition
for
and
is depicted by
Final states are indicated by double circles and the start state is indicated by an arrow to it from nowhere.
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 5
A configuration or an instantaneous description (ID) of PDA at any moment during its computation is an
is
with its read head pointing to the leftmost input symbol and the stack containing only the
be
start state,
.in
element of
describing the current state, the portion of the input remaining to be read (i.e.
under and to the right of the read head), and the current stack contents. Only these three elements can
affect the computation from that point on and, hence, are parts of the ID.
tu
The "next move relation" one figure describes how the PDA can move from one configuration to another
in one step.
relations
J if
J if
define as follows
such that I
and
Let I,
or an input symbol.
.c
iff
'a' may be
se
Formally,
such that I
K and K
J.
That is,
is the reflexive, transitive closure of
zero or more moves.
. We say that I
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 6
.in
be
for some
and
tu
se
2. Acceptance by empty stack (or Null stack) : The PDA M accepts its input
.c
, it ever empties the stack w/o pushing anything back on after reading the entire
start configuration on input
input. Formally, we define N(M), the language accepted by empty stack, to be
for some
Note that the set of final states, F is irrelevant in this case and we usually let the F to be the empty set i.e. F =
Q.
, and
www.csetube.in
www.csetube.in
be
.in
tu
if it sees anymore a, it simply pushes it onto the stack. Note that when M is on state
se
if it sees the first b with a on the top of the stack, then it needs to
top of the stack can only be a. On state
start comparison of numbers of a's and b's, since all the a's at the begining of the input have already been
pushed onto the stack. It start this process by popping off the a from the top of the stack and enters in state q3
(to remember that the comparison process has begun). On state
, it expects only b's in the input (if it sees
any more a in the input thus the input will not be in the proper form of anbn). Hence there is no more on input a
.c
. On state
it pops off an a from the top of the stack for every b in the input. When it
when it is in state
sees the last b on state q3 (i.e. when the input is exaushted), then the last a from the stack will be popped off
and the start symbol z is exposed. This is the only possible case when the input (i.e. on -input ) the PDA M
Let the input be aabb. we start with the start configuration and proceed to the subsequent IDs using the
transition function defined
( using transition 1 )
( using transition 2 )
( using transition 3 )
( using transition 4 ),
string aabb is rightly accepted by M
( using transition 5 ) ,
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 6
we can show the computation of the PDA on a given input using the IDs and next move relations. For example,
following are the computation on two input strings.
tu
Hence the PDA gets stuck and the string aabab is not accepted.
be
.in
se
Example 2 : We give an example of a PDA M that accepts the set of balanced strings of parentheses [] by
empty stack.
The PDA M is given below.
is defined as
.c
where
Informally, whenever it sees a [, it will push the ] onto the stack. (first two transitions), and whenever it sees a ]
and the top of the stack symbol is [, it will pop the symbol [ off the stack. (The third transition). The fourth
transition is used when the input is exhausted in order to pop z off the stack ( to empty the stack) and accept.
Note that there is only one state and no final state. The following is a sequence of configurations leading to the
acceptance of the string [ [ ] [ ] ] [ ].
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 7
.in
It turns out that the two definitions of acceptance of a language by a PDA - accpetance by final state and empty
stack- are equivalent in the sense that if a language can be accepted by empty stack by some PDA, it can also
be accepted by final state by some other PDA and vice versa. Hence it doesn't matter which one we use, since
each kind of machine can simulate the other.Given any arbitrary PDA M that accpets the language L by final
state or empty stack, we can always construct an equivalent PDA M with a single final state that accpets
exactly the same language L. The construction process of M' from M and the proof of equivalence of M & M'
are given below.
be
where
contains
L(M) . Then
for some
Then
accepts
i.e.
L(
), then
for
Conversely, let
accepts
Thus
and
.c
Let
and
se
L(M) = L(
tu
inherits all other moves except the last one from M. Hence
Thus M accepts
for some
simulate all the moves of M and enters in its own final state
accepts a string
www.csetube.in
iff M
www.csetube.in
and X
where
Let
transition of
and
and
Transitions 1 causes
marker X which is below the symbols of M's stack. From this point onward
are equivalent.
. Then
for some
. But then
( Since
.c
( by transition rule 2 )
. Conversely, let
accepts
also accepts
se
( by transition rule 1)
Hence,
be
Let M accepts
tu
At this point,
when simulating M will empty its stack except the symbol X at the bottom.
.in
Then
for some
Hence, M starting with its initial configuration will eventually empty its stack and accept the input i.e.
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 8
where
and
.in
be
From the given CFG G we now construct an equivalent PDA M that accepts by empty stack. Note that there is
only one state in M. Let
tu
, where
se
.c
iff
, then by definition of L(G), there must be a leftmost derivation starting with S and deriving w.
If
i.e.
Again if
iff
But we will prove a more general result as given in the following lemma. Replacing A by S (the start symbol)
and
by
www.csetube.in
www.csetube.in
and
iff
and
.in
i.e.
iff
be
iff
Induction Step :
se
and
where
.c
and
for some
tu
via a leftmost derivation. Let the last production applied in their derivation is
.............................................................................(1)
since
and
, we get
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
, then
That is, if
of
Period: 8
.in
and let
se
and
tu
we get
.c
i.e.
and
be
where
aAB
a / aA
a / bB
The one state PDA M equivalent to G is shown below. For convenience, a production of G and the
corresponding transition in M are marked by the same encircled number.
www.csetube.in
].
www.csetube.in
(1) S
(2) A
(3) A
(4) B
(5) B
aAB
a
aA
a
bB
. We have used the same construction discussed earlier
1. (q, aaaba, s)
( q, aba, AB )
3.
( q, ba, B )
4.
( q, a, B )
5.
( q,
tu
be
2.
.in
( q, aaba, AB )
se
Note : encircled numbers here shows the transitions rule applied at every step.
aAB
S
1
aaaB
aaabB
aaaba
Steps
aaAB
.c
Now consider the derivation of the same string under grammar G. Once again, the production used at every
step is shown with encircled number.
Observations:
There is an one-to-one correspondence of the sequence of moves of the PDA M and the derivation
sequence under the CFG G for the same input string in the sense that - number of steps in both the
cases are same and transition rule corresponding to the same production is used at every step (as
shown by encircled number).
considering the moves of the PDA and derivation under G together, it is also observed that at every
step the input read so far and the stack content together is exactly identical to the corresponding
sentential form i.e.
<what is Read><stack> = <sentential form>
Say, at step 2, Read so far = a
stack = AB
Sentential form = aAB From this property we claim that
and we get
. If the claim is
iff
iff
or
iff
Thus N(M) = L(G) as desired. Note that we have already proved a more general version of the claim
www.csetube.in
( by
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 9
N(M)
It can be show that this reverse construction works only for single state PDAs.
That is, for every one-state PDA M there is CFG G such that L(G) = N(M). For every move of the
PDA M
be
.in
we first see whether the "reverse of the construction" that was used in part (i) can be used here to construct an
equivalent CFG from any PDA M.
we introduce a production
where N = T and
tu
grammar
in the
we can now apply the proof in part (i) in the reverse direction to show that L(G) = N(M).
.c
se
But the reverse construction does not work for PDAs with more than one state. For example, consider the PDA
).
( Note
Corresponding Production in G
Transitions in M
www.csetube.in
www.csetube.in
But under this grammar we can also derive some strings which are not in the language. e.g
and
. But
Therefore, to complete the proof of part (ii) we need to prove the following claim also.
such that
It is quite possible to prove the above claim. But here we will adopt a different approach. We start with any
arbitrary PDA M that accepts by empty stack and directly construct an equivalent CFG G.
.in
PDA to CFG
We want to construct a CFG G to simulate any arbitrary PDA M with one or more states. Without loss of
generality we can assume that the PDA M accepts by empty stack.
The idea is to use nonterminal of the form <PAq> whenever PDA M in state P with A on top of the stack goes
be
to state
. That is, for example, for a given transition of the PDA corresponding production in the grammar as
shown below,
se
tu
Where
.c
Let
1.
2.
and
If
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 9
for some
as production in G. Therefore,
we have
iff
i.e.
be
iff
to get
.in
iff
tu
Now, to show that the above construction of CFG G from any PDA M works, we need to prove the proposed
claim.
) in
se
Note: At this point, the justification for introduction of the first type of production (of the form
the CFG G, is quite clear. This helps use deriving a string from the start symbol of the grammar.
.c
The proof consists of two parts: ' if ' part and ' only if ' part. First, consider the ' if ' part
then
If
Basis is n =1
Then
a production of G.
Then
Inductive Hypothesis :
www.csetube.in
. Hence, by construction
is
www.csetube.in
Inductive Step :
and
general transition
, where
Let
the stack. Then there must exist a sequence of states in M (as per construction)
(with
.in
), such that
tu
be
se
[ Note: Each step takes less than or equal to n -1 moves because the total number of moves required assumed
to be n-1.]
That is, in general
.c
We can show the computation of the PDA on a given input using the IDs and next move relations. For example,
following are the computation on two input strings.
i) Let the input be aabb. we start with the start configuration and proceed to the subsequent IDs using the
transition function defined
( using transition 1 ) ,
( using transition 3 ),
( using transition 4 )
( using transition 5 ) ,
www.csetube.in
( using transition 2 )
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 9
be
.in
tu
.c
se
It turns out that the two definitions of acceptance of a language by a PDA - accpetance by final state and empty
stack- are equivalent in the sense that if a language can be accepted by empty stack by some PDA, it can also
be accepted by final state by some other PDA and vice versa. Hence it doesn't matter which one we use, since
each kind of machine can simulate the other.Given any arbitrary PDA M that accpets the language L by final
state or empty stack, we can always construct an equivalent PDA M with a single final state that accpets
exactly the same language L. The construction process of M' from M and the proof of equivalence of M & M'
are given below
There are two cases to be considered.
. Let
where
www.csetube.in
www.csetube.in
contains
and
for some
. Then
Let
Then
and
Conversely, let
.
accepts
for some
, then
i.e.
inherits all other moves except the last one from M. Hence
Thus M accepts
simulate all the moves of M and enters in its own final state
where
and
and
iff M
contains all
the transition of
.c
Let
se
accepts a string
tu
we will construct
for some
be
.in
accepts
Thus
and
Let M accepts
when simulating M will empty its stack except the symbol X at the bottom.
are equivalent.
www.csetube.in
www.csetube.in
R/TP/02
Lecture Plan
Code & Name of Subject: CS2303, Theory of Computation
Issue: C Rev.01
Page
Unit No: 3
of
Period: 9
Then
for some
. But then,
.in
( by transition rule 1 )
( by transition rule 2 )
also accepts
.Conversely, let
accepts
tu
Hence,
be
( since
for some Q .
Then
se
.c
Hence, M starting with its initial configuration will eventually empty its stack and accept the input i.e.
Deterministic PDA:
Regular Languages and DPDAs The DPDAs accepts a class of languages that is in between the regular
languages and CFLs.
www.csetube.in
be
.in
www.csetube.in
tu
se
Pushdown automata that we have already defined and discussed are nondeterministic by default, that is , there may be two or
more moves involving the same combinations of state, input symbol, and top of the stock, and again, for some state and
top of the stock the machine may either read and input symbol or make an - transition (without consuming any input).
.c
In deterministic PDA , there is never a choice of move in any situation. This is handled by preventing the above mentioned two
cases as described in the definition below.
Defnition : Let
satisfied.
be a PDA . Then M is deterministic if and only if both the following conditions are
1.
2.
If
any combination of
and
and
for every
(This condition prevents the possibility of a choice between a move with or without an input symbol).
www.csetube.in