09 PushdownAutomata
09 PushdownAutomata
Objectives
• A New Format for FAs
2
A new format for FA
• Regular languages can be generated by CFGs, and so can some
non-regular languages.
• In other words, the set of CFLs is larger than the set of regular
languages.
3
INPUT TAPE
• The INPUT TAPE is part of the machine where the input
string is placed.
• It is infinitely long to accommodate any possible input.
• The locations into which we put the input letters are
called cells. We name cells with lowercase Roman
numerals.
• The character ∆ is used to indicate a blank.
• We read one letter at a time, from left to right, and never
go back to a cell that was read before.
• When we reach the first blank cell, we stop.
• We always presume that once the first blank is
encountered, the rest of the TAPE is also blank.
4
Other Symbols
5
• The REJECT state is a dead-end state that is
not final, such as
8
Example
9
Adding a Pushdown Stack
• We would like to add a PUSHDOWN STACK to our
machine.
• A PUSHDOWN STACK is a place where input letters
can be stored.
• Before the machine begins to process an input string,
the STACK is presumed to be empty.
• The operation PUSH adds a new letter to the top of the
STACK.
• All the other letters are pushed down accordingly.
• The operation POP takes a letter out of the STACK.
• The rest of the letters are moved up one location each
accordingly.
10
• For example, if we perform operations PUSH a,
PUSH b, and PUSH c, then the stack will look
like the following:
11
• We can add a PUSHDOWN STACK and the operations PUSH and
POP to our machine by including the following symbols:
• The edges coming out of a POP state are labeled in the same way
as the edges coming out of a READ state.
• Branching can occur at POP states but not at PUSH states: We can
leave PUSH states only by the one indicated route, although we can
enter a PUSH state from any direction.
12
• When FAs are equipped with a STACK and POP
and PUSH states, we call them pushdown
automata or PDAs.
13
• Consider this PDA:
15
• Similarly, after reading the last b, STACK pops the last a, and we
have
TAPE = ∆, ∆, … STACK = ∆, ∆, …
• When we encounter the blank character ∆ from the INPUT TAPE,
the STACK pops a blank , and we are lead to the ACCEPT state.
• Hence, the string aaabbb is accepted by this machine.
16
• 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
17
• The process of running the string aaabbb can also be
expressed in the following table
18
Remarks
• We need not restrict ourselves to using the same
alphabet for input strings and for the STACK.
• In the example above, we could have read an a form the
TAPE and then pushed an X into the STACK; that is, we
let the X’s count the number of a’s.
19
Remarks
• Following is an example of PDA corresponding to an FA
20
Deterministic VS Non-Deterministic PDA
• Since our goal is to produce a new type of machine that can
recognize all CFLs (just as FAs can recognize all regular
languages), the addition of a simple STACK may not be enough. We
shall see that the new PDAs will have to be nondeterministic.
21
• Nondeterminism allows for the possibility of too few (even zero) as well as
too many edges leading out of a branch state (READ or POP).
• For example, we can have more than one edge with the same label leading
out of a branch state. We can also have no edge, say no b-edge, leading
out of a particular READ state. If a b is read at that state, processing cannot
continue, the machine crashes, and the input is rejected.
• Recall that for FAs, nondeterminism (e.g., NFAs and TG) does not increase
the power of the machine to accept new languages. For PDAs, this is
different:
22
Example
• Let’s consider what kind of PDA could accept the language of
ODDPALINDROME. This is the language of all strings of a’s and b’s
that are palindrome and have an odd number of letters. For
instance, a word of this language is w = abbabba.
• Possible solution: When we process the front part of the word (abb),
we also PUSH the input letters onto the STACK. When we process
the back part of the string (bba), we can POP the STACK in order to
compare the letters being read with those from the STACK.
• However, the problem here is that the middle letter does not stand
out. It is therefore impossible for the machine to recognize where the
front part ends and where the back part begins: A PDA, just like an
FA, reads the input string from left to right and has no idea at any
stage how many letters remain to be read.
23
• The PDA therefore should be nondeterministic:
We need to make a choice to follow the right
edge at the right time (i.e. when reading the
middle letter.)
24
• Try the input string aba on this machine.
25
Defining the PDA
A pushdown automaton, PDA, is a collection of eight
things:
1. An alphabet ∑ of input letters.
2. An input TAPE (infinite in one direction). Initially, the string of
input letters is placed on the TAPE starting in cell i. The rest of
the TAPE is blank.
3. An alphabet Γ of STACK characters.
4. A pushdown STACK (infinite in one direction). Initially, the
STACK is empty (contains all blanks).
5. One START state that has only out-edges, and no in-edges:
26
Defining the PDA (contd.)
6. Halt states of two kinds: some ACCEPT and
some REJECT. They have in-edges and no
out-edges:
30