0% found this document useful (0 votes)
85 views60 pages

Lecture 14-15 - PDA

Uploaded by

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

Lecture 14-15 - PDA

Uploaded by

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

Computation Theory

Push Down Automata


Push Down Automata (PDA) Theory
A new format for FAs
 A class of machines (FAs) has been discussed accepting the regular
language i.e. corresponding to a regular language there is a machine
in this class, accepting that language and corresponding to a
machine of this class there is a regular language accepted by this
machine. It has also been discussed that there is a CFG
corresponding to regular language and CFGs also define some
nonregular languages, as well
 There is a question whether there is a class of machines accepting
the CFLs? The answer is yes. The new machines which are to be
defined are more powerful and can be constructed with the help of
FAs with new format.
 To define the new format of an FA, the following are to be defined
Push Down Automata (PDA) Theory
Input TAPE
 The part of an FA, where the input string is placed before it is run, is
called the input TAPE..∆∆aabbaCell i Cell ii Cell iii
 The input TAPE is supposed to accommodate all possible strings.
The input TAPE is partitioned with cells, so that each letter of the
input string can be placed in each cell. The input string abbaa is
shown in the following input TAPE.
Cell i Cell ii Cell iii
a b b a a ∆ ∆ .

 The character ∆ indicates a blank in the TAPE. The input string is


read from the TAPE starting from the cell (i). It is assumed that
when first ∆ is read, the rest of the TAPE is supposed to be blank.
Push Down Automata (PDA) Theory
The START state START
This state is like initial state of an FA & is
represented by
An ACCEPT state
This state is like a final state of an FA and
is expressed by ACCEPT

A REJECT state
This state is like dead-end non final state
REJECT
and is expressed by
Note: It may be noted that the ACCEPT and REJECT states are called
the halt states.
Push Down Automata (PDA) Theory
A READ state
 This state is to read an input letter a
READ b
and lead to some other state. The

READ state is expressed by
Example
 Before some other states are
defined consider the following b a
a
example of an FA along with its x- y+
b
new format
Push Down Automata (PDA) Theory
 Obviously the above FA accepts the language of strings, expressed
by (a+b)*a. Following is the new format of the above FA
START

a ∆
READ READ
b

∆ a

REJECT ACCEPT
Note
 The ∆ edge should not be confused with -labeled edge. The
∆-edges start only from READ boxes and lead to halt states.
Push Down Automata (PDA) Theory
Example a a,b
b
The above FA accepts the language 1 b +
-
expressed by (a+b)*bb(a+b)* a

START
a a,b

a READ b READ b READ

∆ ∆ ∆
REJECT REJECT ACCEPT
Push Down Automata (PDA) Theory

Push Down Automata (PDA)


Push Down Automata (PDA) Theory
PUSHDOWN STACK or PUSHDOWN STORE
 PUSHDOWN STACK is a place where the input letters
can be placed until these letters are referred again. It can
store as many letters as one can in a long column.
 Initially the STACK is supposed to be empty i.e. each of
its storage location contains a blank.
Push Down Automata (PDA) Theory
PUSH
 A PUSH operator adds a new letter at the top of STACK,
for e.g. if the letters a, b, c and d are pushed to the STACK
that was initially blank, the STACK can be shown as
The PUSH state is expressed by
PUSH a

 When a letter is pushed, it replaces the existing letter and


pushes it one position below
Push Down Automata (PDA) Theory
POP and STACK
 POP is an operation that takes out a letter from the top of
the STACK. The rest of the letters are moved one location
up. POP state is expressed as

b
POP a


Push Down Automata (PDA) Theory
Note
 It may be noted that popping an empty STACK is like
reading an empty TAPE, i.e. popping a blank character ∆.
 It may also be noted that when the new format of an FA
contains PUSH and POP states, it is called PUSHDOWN
Automata or PDAs. It may be observed that if the
PUSHDOWN STACK (the memory structure) is added to
an FA then its language recognizing capabilities are
increased considerably. Following is an example of PDA
Push Down Automata (PDA) Theory
Example
 Consider the following PDA
START

PUSH a a READ1 ∆

b
b

POP1 a READ2 ∆ POP2


b, ∆ a,b

REJECT ACCEPT
REJECT
Push Down Automata (PDA) Theory
 The string aaabbb is to be run on this machine. Before the
string is processed, the string is supposed to be placed on
the TAPE and the STACK is supposed to be empty as
shown below

a a a b b b ∆ ∆  


Push Down Automata (PDA) Theory
 Reading first a from the TAPE we move from READ1State to
PUSH a state, it causes the letter a deleted from the TAPE and added
to the top of the STACK, as shown below

STACK
a a a b b b ∆ ∆ 
TAPE a


Push Down Automata (PDA) Theory
 Reading next two a’s successively, will delete further two
a’s from the TAPE and add these letters to the top of the
STACK, as shown below
STACK
a a a b b b ∆ ∆ 
TAPE a
a
a


Push Down Automata (PDA) Theory
 Then reading the next letter which is b from the TAPE will
lead to the POP1 state. The top letter at the STACK is a,
which is popped out and READ2 state is entered. Situation
of TAPE and STACK is shown below
STACK

a
TAPE a a a b b b ∆ ∆  a
a



Push Down Automata (PDA) Theory
 Reading the next two b’s successively will delete two b’s from the
TAPE, will lead to the POP1 state and these b’s will be removed
from the STACK as shown below

a a a b b b ∆ ∆ 

 Now there is only blank character ∆ left, to be read from 
the TAPE, which leads to POP2 state. While the only 
blank characters is left in the STACK to be popped out 
and the ACCEPT state is entered, which shows that the
string aaabbb is accepted by this PDA. It may be observed that the
above PDA accepts the language
{anbn: n = 0,1,2, … }.
Push Down Automata (PDA) Theory
 Since the null string is like a blank character, so to
determine how the null string is accepted, it can be placed
in the TAPE as shown below

TAPE ∆ ∆ ∆ 

 Reading ∆ at state READ1 leads to POP2 state and POP2


state contains only ∆, hence it leads to ACCEPT state and
the null string is accepted.
 Note: The process of running the string aaabbb can also be
expressed in the table given in the next lecture
Push Down Automata (PDA) Theory
Note: The process of running the string aaabbb can also be
expressed in the following table.
STATE STACK TAPE STATE STACK TAPE
START ∆… aaabbb∆ … READ2 aa∆ … aaabbb∆ …
READ1 ∆… aaabbb∆ …
POP1 a∆ … aaabbb∆ …
PUSH a a∆ … aaabbb∆ …
READ2 a∆ … aaabbb∆ …
READ1 a∆ … aaabbb∆ …
PUSH a aa∆ … aaabbb∆ … POP1 ∆… aaabbb∆ …
READ1 aa∆ … aaabbb∆ … READ2 ∆… aaabbb∆ …
PUSH a aaa∆ … aaabbb∆ … POP2 ∆… aaabbb∆ …
READ1 aaa∆ … aaabbb∆ …
ACCEPT ∆… aaabbb∆ …
POP1 aa∆ … aaabbb∆ …
Push Down Automata (PDA) Theory
Note: It may be noted that the TAPE alphabet Σ and STACK
alphabet Γ , may be different in general and hence the PDA
equivalent to that accepting {anbn: n=0,1,2,3…}
START


PUSH X a READ1

b
b

∆ X POP2
POP1 X READ2


a ∆
REJECT ACCEPT
Push Down Automata (PDA) Theory
Example:
 Consider the following FA corresponding to the EVEN-
EVEN language
a

a

b b b b

a
 The corresponding PDA will be
Push Down Automata (PDA) Theory
ACCEPT REJECT
∆ a ∆

START

a
b b b b
a


a ∆
REJECT
REJECT
Push Down Automata (PDA) Theory
Nondeterministic PDA
 Like TGs and NFAs, if in a PDA there are more than one
outgoing edges at READ or POP states with one label, then it
creates nondeterminism and the PDA is called nondeterministic
PDA.
 In nondeterministic PDA no edge is labeled by string of
terminals or nonterminals, like that can be observed in TGs.
Also if there is no edge for any letter to be read from the
TAPE, the machine crashes and the string is rejected.
 In nondeterministic PDA a string may trace more than one
paths. If there exists at least one path traced by a string leading
to ACCEPT state, then the string is supposed to be accepted,
otherwise rejected
Push Down Automata (PDA) Theory
START POP1 a

a a
b
PUSH a a READ1
b
POP2 b READ2
PUSH b ∆
b

POP3

ACCEPT
Push Down Automata (PDA) Theory
 Here the nondeterminism can be observed at state READ1.
It can be observed that the above PDA accepts the
language
 EVENPALINDROME={w reverse(w): wE{a, b}*}
 ={, aa, bb, aaaa, abba, baab, bbbb, …}
 Now the definition of PDA including the possibility of
nondeterminism may be given as follows
Push Down Automata (PDA)
 An alphabet Σ of input letters.
 An input TAPE with infinite many locations in one direction. Initially the
input string is placed in it starting from first cell, the remaining part of the
TAPE is empty.
 An alphabet Γ of STACK characters.
 A pushdown STACK which is initially empty, with infinite many locations
in one direction. Initially the STACK contains blanks.
 One START state with only one out-edge and no in-edge.
 Two halt states i.e. ACCEPT and REJECT states, with in-edges and no
out-edges.
 A PUSH state that introduces characters onto the top of the STACK.
 A POP state that reads the top character of the STACK, (may contain more
than one out-edges with same label).
 A READ state that reads the next unused letter from the TAPE, (may
contain more than one out-edges with same label).
Push Down Automata (PDA)
Example: Consider the CFG S  S+S|S*S|4
Following is the PDA accepting the corresponding CFL
4 + *
READ1 READ3
READ2
ACCEPT
START +
S * ∆
PUSH1 S POP ∆ READ4
S S PUSH5 S
PUSH2 S

PUSH3 + PUSH6 *

PUSH4 S PUSH7 S
Push Down Automata (PDA)
The string 4 + 4 * 4 traces the path shown in the following table
STATE STACK TAPE STATE STACK TAPE
READ2 S 4*4
START ∆ 4+4*4
POP ∆ 4*4
PUSH1 S S 4+4*4 PUSH5 S S 4*4
PUSH6 * *S 4*4
POP ∆ 4+4*4 PUSH7 S S*S 4*4
PUSH2 S S 4+4*4 POP *S 4*4
READ1 *S *4
PUSH3 + +S 4+4*4 POP S *4
PUSH4 S S+S 4+4*4 READ3 S 4
POP ∆ 4
POP +S 4+4*4
READ1 ∆ ∆
READ1 +S +4*4 POP ∆ ∆
READ4 ∆ ∆
POP S +4*4
ACCEPT ∆ ∆
Push Down Automata (PDA)
Note
 It may be noted that the letters are deleted from the TAPE
instead of underlined.
 It may also be noted that the choice of path at POP state
can be determined by the left most deviation of the string
belonging to the CFL.
Chapter # 15

PDA corresponding to CFG


PDA corresponding to CFG
PDA corresponding to CFG
Theorem
 Corresponding to any CFG there exists a PDA accepting
the language generated by the CFG.
 Since an algorithm has already been discussed to convert
the CFG in CNF, so the PDA can be constructed
corresponding to the CFG. As the CFG in CNF generates
all the nonnull words of the corresponding CFL, so
accepting the null string (if it is contained in the CFL), can
be managed separately.
PDA corresponding to CFG
Example: Consider the following CFG which is in CNF and
does not generate the null string
S  SB|AB
A  CC
Bb
Ca
The corresponding PDA will be
PDA corresponding to CFG
a b
READ1
READ2
ACCEPT
START
C B ∆
POP ∆ READ3
PUSH S
S A
S

PUSH B PUSH B PUSH C

PUSH S PUSH A PUSH C


PDA corresponding to CFG
 Here the STACK alphabet Γ = {S, A, B, C}, where the
TAPE alphabet Σ={a, b}.
Note: It may be noted that when the POP state is entered
either a nonterminal is replaced by two nonterminals at the
top of the STACK accommodating a production, or a
nonterminal is popped out from the top of the stack and a
READ state is entered to read a specified letter from the
TAPE or else the machine crashes.
 The choice of path taken at POP state to accommodate the
word belonging to the CFL can be determined by the left
most derivation of the word
PDA corresponding to CFG
Consider the word aab with its left most derivation, as follows:
Working-String Generation Production Used
S =>AB S  AB step 1
=>CCB A  CC step 2
=>aCB Ca step 3
=>aaB Ca step 4
=>aab Bb step 5
PDA corresponding to CFG
 First of all the START state is entered STACK TAPE
∆… aab∆…

 The PUSH S state is entered


STACK TAPE
S aab∆…

 The POP state is entered and to accommodate the production S


 AB, PUSH B and PUSH A states are entered.
STACK TAPE
AB aab∆…
PDA corresponding to CFG
 Then the POP state is entered and to accommodate the
production A  CC, PUSH C, PUSH C states are entered
STACK TAPE
CCB aab

 The POP state is entered and to accommodate the production C


 a, READ1 is entered and the letter a is read from the TAPE
STACK TAPE
CB aab
PDA corresponding to CFG
 The POP state is entered and to accommodate the production C 
a, READ1 state is entered and the letter a is read from the TAPE

STACK TAPE
B aab
 The POP state is entered and to accommodate the production B 
b, READ2 state is entered and the letter b is read from the TAPE

STACK TAPE
aab
 The ∆ shown in the ∆STACK indicates that there are no
nonterminals in the working string and ∆ is read from the STACK
which leads to READ3 state where the ∆ is read from the TAPE
and the ACCEPT state is entered which shows that the word aab
is accepted by the PDA.
PDA corresponding to CFG
Following is the table showing all the observations discussed above, for
Left most STATE STACK TAPE
the word aab derivation
START ∆ aab
S PUSH S S aab
POP ∆ aab
PUSH B B aab
AB PUSH A AB aab
POP B aab
PUSH C CB aab
CCB PUSH C CCB aab
POP CB aab
aCB READ1 CB aab
POP B aab
aaB READ1 B aab
POP ∆ aab
aab READ2 ∆ aab
POP ∆ aab
READ3 ∆ aab
ACCEPT ∆ aab
PDA corresponding to CFG
Following is an example of building the PDA corresponding
to the given CFG:
Example:
Consider the following CFG
S  XY
X  aX | bX |a
Y  Ya | Yb | a
First of all, converting the CFG to be in CNF, introduce the nonterminals
A and B as
Aa
Bb
PDA corresponding to CFG
The following CFG is in CNF
S  XY
X  AX | BX |a
Y  YA | YB | a
Aa
Bb
The PDA corresponding to the above CFG will be
PDA corresponding to CFG
a a a b
READ1 READ3 READ4
READ2
B
START A ACCEPT
X Y

PUSH S POP ∆ READ5

S
X Y
X
Y
PUSH Y PUSH X PUSH X PUSH A PUSH B

PUSH X PUSH A PUSH B PUSH Y PUSH Y


PDA corresponding to CFG
The word aaab can be generated as
Working-String Generation Production Used
S =>XY S  XY step 1
=>AXY X  AX step 2
=>aXY Aa step 3
=>aaY Xa step 4
=>aaYB Y  YB step 5
=>aaaB Ya step 6
=>aaab Bb step 7
PDA corresponding to CFG
STACK TAPE STACK TAPE
(ST) ∆ aaab (PP) Y aaab

(PH S) S aaab (RD1) Y aaab

(PP) ∆ aaab (PP) ∆ aabb

(PH Y) Y aaab (PH B) B aabb

(PH X) XY aaab (PH Y)YB aabb

(PP) Y aaab (PP) B aaab

(PH X)XY aaab (RD2) B aaab

(PHA)AXY aaab (PP) ∆ aaab

(PP) XY aaab (RD4) ∆ aaab

(RD3)XY aaab (PP) ∆ ∆


PDA corresponding to CFG
Example
Consider the following CFG
S  XY
X  aX | bX |a
Y  Ya | Yb | a
First of all, converting the CFG to be in CNF, introduce the nonterminals A and B as
Aa
Bb
The following CFG is in CNF
S  XY
X  AX | BX |a
Y  YA | YB | a
Aa
Bb
PDA corresponding to CFG
a a a b
READ1 READ3 READ4
READ2
B
START A ACCEPT
X Y

PUSH S POP ∆ READ5

S
X Y
X
Y
PUSH Y PUSH X PUSH X PUSH A PUSH B

PUSH X PUSH A PUSH B PUSH Y PUSH Y


PDA corresponding to CFG
Theorem
 Given a PDA that accepts the language L, there exists a
CFG that generates exactly L.
 Before the CFG corresponding to the given PDA is
determined, the PDA is converted into the standard form
which is called the conversion form.
 Before the PDA is converted into conversion form a new
state HERE is defined which is placed in the middle of any
edge.
PDA corresponding to CFG
Like READ and POP states, HERE states are also numbered
e.g.
READ7 a READ9 b

becomes

READ7 a HERE3 READ9 b


PDA corresponding to CFG
Conversion form of PDA
DefinitionA PDA is in conversion form if it fulfills the following
conditions:
1. There is only one ACCEPT state.
2. There are no REJECT states.
3. Every READ or HERE is followed immediately by a POP i.e.
every edge leading out of any READ or HERE state goes directly
into a POP state.
4. No two POPs exist in a row on the same path without a READ or
HERE between them whether or not there are any intervening PUSH
states (i.e. the POP states must be separated by READs or HEREs).
PDA corresponding to CFG
5. All branching, deterministic or nondeterministic occurs at
READ or HERE states, none at POP states and every edge
has only one label.
6. Even before we get to START, a “bottom of STACK”
symbol $ is placed on the STACK. If this symbol is ever
popped in the processing it must be replaced immediately.
The STACK is never popped beneath this symbol. Right
before entering ACCEPT this symbol is popped out and
left.
7. The PDA must begin with the sequence
$
START POP PUSH $ HERE
PDA corresponding to CFG
8. The entire input string must be read before the machine
can accept the word.
 Different situations of a PDA to be converted into
conversion form are discussed as follows
 To satisfy condition 3,

READ7 a READ8 b

b
PDA corresponding to CFG
Becomes PUSH a
a
READ7 a POP b PUSH b READ7 b
$
b PUSH $
To satisfy condition 4,
POP4 a POP5 b

becomes
a b
POP4 HERE POP5
PDA corresponding to CFG
To satisfy condition 5 READ1 a
POP1
b READ2
a
READ3
becomes
POP1 a
PUSH a
a
READ1 RD7 READ2
a b
POP2 PUSH b

To satisfy condition 6, it is supposed that the STACK


$
STACK is initially in the position shown below ∆
PDA corresponding to CFG
Example: Consider the following PDA accepting the
language {a2nbn : n = 1,2,3, …}
START

READ1 b POP1 a POP2 a READ2 ∆

a b

PUSH a
ACCEPT $ POP3

Which may be converted to


PDA corresponding to CFG
$
PUSH $ POP4 START

b a
READ1 POP1 HERE POP2

a a
a b
READ2
POP5 POP6

a $ $
PUSH a PUSH $ ACCEPT POP3

PUSH a PUSH a The above PDA accepts


exactly the same language
PDA corresponding to CFG
Note: It may be noted that any PDA which is in conversion form can be
considered to be the collection of path segments, where each path
segment is of the following form
FROM TO READ POP PUSH
START READ ONE or Exactly one Any string
or READ or HERE no input letter STACK onto the
or HERE or ACCEPT character STACK

 START, READ, HERE and ACCEPT states are called the joints of
the machine. Between two consecutive joints on a path exactly one
character is popped and any number of characters can be pushed.
 The PDA which is in the conversion form can be supposed to be
the set of joints with path segments in between, similar to a TG
PDA corresponding to CFG
START READ1 HERE

READ2

ACCEPT

The above entire machine can be described as a list of all


joint-to-joint path segments, called summary table.
PDA corresponding to CFG
The PDA converted to the conversion form has the following
summary table
FROM TO READ POP PUSH ROW
Where Where What What What Number
START READ1  $ $ 1
READ1 READ1 a $ a$ 2
READ1 READ1 a a aa 3
READ1 HERE b a -- 4
HERE READ2  a -- 5
READ2 HERE b a -- 6
READ2 AT ∆ $ -- 7
End of Lecture

You might also like