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

Pushdown Automata: Recognizing Context-Free Languages

Pushdown automata are a type of automaton that extends finite state machines by adding a stack. This allows pushdown automata to recognize context-free languages. A pushdown automaton has a finite set of states, input alphabet, stack alphabet, initial state, set of final states, and transition relation. The transition relation specifies the current state, top of stack symbol being read, next state, and symbol to push on the stack. Examples show pushdown automata that recognize languages such as balanced brackets, anbn, and wcwR.

Uploaded by

Singham
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)
34 views

Pushdown Automata: Recognizing Context-Free Languages

Pushdown automata are a type of automaton that extends finite state machines by adding a stack. This allows pushdown automata to recognize context-free languages. A pushdown automaton has a finite set of states, input alphabet, stack alphabet, initial state, set of final states, and transition relation. The transition relation specifies the current state, top of stack symbol being read, next state, and symbol to push on the stack. Examples show pushdown automata that recognize languages such as balanced brackets, anbn, and wcwR.

Uploaded by

Singham
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/ 8

Pushdown Automata

Read K & S 3.3.


Read Supplementary Materials: Context-Free Languages and Pushdown Automata: Designing Pushdown Automata.
Do Homework 13.

Recognizing Context-Free Languages

Two notions of recognition:


(1) Say yes or no, just like with FSMs
(2) Say yes or no, AND
if yes, describe the structure

a + b * c
Just Recognizing

We need a device similar to an FSM except that it needs more power.

The insight: Precisely what it needs is a stack, which gives it an unlimited amount of memory with a restricted structure.

( ( ( ( ( ) ) ) ) ( ) ( ( ) )

( Finite
( State
( Controller
(
( (

Definition of a Pushdown Automaton

M = (K, Σ, Γ, ∆, s, F), where:


K is a finite set of states
Σ is the input alphabet
Γ is the stack alphabet
s ∈ K is the initial state
F ⊆ K is the set of final states, and
∆ is the transition relation. It is a finite subset of

(K × (Σ ∪ {ε}) × Γ* ) × ( K × Γ* )

state input or ε string of symbols to pop state string of symbols to


from top of stack push on top of stack

M accepts a string w iff


(s, w, ε) |-M* (p, ε, ε) for some state p ∈ F

Lecture Notes 14 Pushdown Automata 1


A PDA for Balanced Brackets

[//[
s
]/[/

M = (K, Σ, Γ, ∆, s, F), where:


K = {s} the states
Σ = {[, ]} the input alphabet
Γ = {[} the stack alphabet
F = {s}
∆ contains:

((s, [, ε), (s, [ ))


((s, ], [ ), (s, ε))

Important:
This does not mean that the stack is empty.
An Example of Accepting

[//[
s
]/[/

∆ contains:
[1] ((s, [, ε), (s, [ ))
[2] ((s, ], [ ), (s, ε))
input = [ [ [ ] [ ] ] ]

trans state unread input stack


s [[[][]]] ε
1 s [[][]]] [
1 s [][]]] [[
1 s ][]]] [[[
2 s []]] [[
1 s ]]] [[[
2 s ]] [[
2 s ] [
2 s ε ε

An Example of Rejecting

[//[
s
]/[/
∆ contains:
[1] ((s, [, ε), (s, [ ))
[2] ((s, ], [ ), (s, ε))
input = [ [ ] ] ]

trans state unread input stack


s [[]]] ε
1 s []]] [
1 s ]]] [[
2 s ]] [
2 s ] ε
none! s ] ε
We're in s, a final state, but we cannot accept because the input string is not empty. So we reject.

Lecture Notes 14 Pushdown Automata 2


A PDA for anbn

First we notice:
• We'll use the stack to count the a's.
• This time, all strings in L have two regions. So we need two states so that a's can't follow b's. Note the similarity to the
regular language a*b*.

A PDA for wcwR

A PDA to accept strings of the form wcwR:

a//a a/a/
c//
s f

b//b b/b/

M = (K, Σ, Γ, ∆, s, F), where:


K = {s, f} the states
Σ = {a, b, c} the input alphabet
Γ = {a, b} the stack alphabet
F = {f} the final states
∆ contains:
((s, a, ε), (s, a))
((s, b, ε), (s, b))
((s, c, ε), (f, ε))
((f, a, a), (f, ε))
((f, b, b), (f, ε))
An Example of Accepting

a//a a/a/
c//
s f

b//b b/b/

∆ contains:
[1] ((s, a, ε), (s, a))
[2] ((s, b, ε), (s, b))
[3] ((s, c, ε), (f, ε))
[4] ((f, a, a), (f, ε))
[5] ((f, b, b), (f, ε))

input = b a c a b
trans state unread input stack
s bacab ε
2 s acab b
1 s cab ab
3 f ab ab
5 f b b
6 f ε ε

Lecture Notes 14 Pushdown Automata 3


A Nondeterministic PDA
L = wwR
S→ε
S → aSa
S → bSb
A PDA to accept strings of the form wwR:

a//a a/a/
ε//
s f

b//b b/b/

M = (K, Σ, Γ, ∆, s, F), where:


K = {s, f} the states
Σ = {a, b, c} the input alphabet
Γ = {a, b} the stack alphabet
F = {f} the final states
∆ contains:
((s, a, ε), (s, a))
((s, b, ε), (s, b))
((s, ε, ε), (f, ε))
((f, a, a), (f, ε))
((f, b, b), (f, ε))
An Example of Accepting

a//a a/a/
ε//
s f

b//b b/b/

[1] ((s, a, ε), (s, a)) [4] ((f, a, a), (f, ε))
[2] ((s, b, ε), (s, b)) [5] ((f, b, b), (f, ε))
[3] ((s, ε, ε), (f, ε))
input: a a b b a a

trans state unread input stack


s aabbaa ε
1 s abbaa a
3 f abbaa a
4 f bbaa ε
none

trans state unread input stack


s aabbaa ε
1 s abbaa a
1 s bbaa aa
2 s baa baa
3 f baa baa
5 f aa aa
4 f a a
4 f ε ε

Lecture Notes 14 Pushdown Automata 4


L = {ambn : m ≤ n}
A context-free grammar for L:
S→ε
S → Sb /* more b's
S → aSb
A PDA to accept L:

a//a b/a/
b/a/ b/ε/
1 2
b/ε/

Accepting Mismatches

L = {ambn m ≠ n; m, n >0}

a//a b/a/
b/a/
1 2

• If stack and input are empty, halt and reject.

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

a//a b/a/ ε/a/


b/a/ ε/a/
1 2 3

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

a//a b/a/ ε/a/


b/a/ ε/a/
1 2 3

b//
4 b//

Lecture Notes 14 Pushdown Automata 5


Eliminating Nondeterminism

A PDA is deterministic if, for each input and state, there is at most one possible transition. Determinism implies uniquely
defined machine behavior.

a//a b/a/ ε/a/


b/a/ ε/a/
1 2 3

b//
4 b//

• Jumping to the input clearing state 4:


Need to detect bottom of stack, so push Z onto the stack before we start.

a//a b/a/ ε/a/


ε//Z b/a/ ε/a/
0 1 2 3 ε/Z/

b/Z/
4 b//

• Jumping to the stack clearing state 3:


Need to detect end of input. To do that, we actually need to modify the definition of L to add a termination character
(e.g., $)

L = {anbmcp : n,m,p ≥ 0 and (n ≠ m or m ≠ p)}

S → NC /* n ≠ m, then arbitrary c's C → ε | cC /* add any number of c's


S → QP /* arbitrary a's, then p ≠ m P → B' /* more b's than c's
N→A /* more a's than b's P → C' /* more c's than b's
N→B /* more b's than a's B' → b
A→a B' → bB'
A → aA B' → bB'c
A → aAb C' → c | C'c
B→b C' → C'c
B → Bb C' → bC'c
B → aBb Q → ε | aQ /* prefix with any number of a's

L = {anbmcp : n,m,p ≥ 0 and (n ≠ m or m ≠ p)}

ε//Z a//a
S S' machine for N

a// b,c

clear and accept


machine for P

Lecture Notes 14 Pushdown Automata 6


Another Deterministic CFL

L = {anbn} ∪ {bn an}

A CFG for L: A NDPDA for L:

S→A
S→B
A→ε
A → aAb
B→ε
B → bBa

A DPDA for L:

More on PDAs

What about a PDA to accept strings of the form ww?

Every FSM is (Trivially) a PDA

Given an FSM M = (K, Σ, ∆, s, F)


and elements of ∆ of the form
( p, i, q )
old state, input, new state

We construct a PDA M' = (K, Σ, Γ, ∆, s, F)


where Γ = ∅ /* stack alphabet
and
each transition (p, i, q) becomes
( ( p, i, ε ), ( q, ε ) )
old state, input, don't look at stack new state don't push on stack

In other words, we just don't use the stack.

Alternative (but Equivalent) Definitions of a NDPDA

Example: Accept by final state at end of string (i.e., we don't care about the stack being empty)
We can easily convert from one of our machines to one of these:
1. Add a new state at the beginning that pushes # onto the stack.
2. Add a new final state and a transition to it that can be taken if the input string is empty and the top of the stack is #.
Converting the balanced parentheses machine:

(//( ε//# (//(


S S S'
)/(/ )/(/
ε/#/

The new machine is nondeterministic:


( ) ( )
Ý
The stack will be: #

Lecture Notes 14 Pushdown Automata 7


What About PDA's for Interesting Languages?

E→E+T Arithmetic Expressions


E→T
T→T*F ε/ε/E
T→F 1 2
F → (E)
F → id

(1) (2, ε, E), (2, E+T) Example:


(2) (2, ε, E), (2, T) a+b*c
(3) (2, ε, T), (2, T*F)
(4) (2, ε, T), (2, F)
(5) (2, ε, F), (2, (E) )
(6) (2, ε, F), (2, id)
(7) (2, id, id), (2, ε)
(8) (2, (, ( ), (2, ε)
(9) (2, ), ) ), (2, ε)
(10) (2, +, +), (2, ε)
(11) (2, *, *), (2, ε)

But what we really want to do with languages like this is to extract structure.

Comparing Regular and Context-Free Languages

Regular Languages Context-Free Languages

• regular expressions • context-free grammars


- or -
• regular grammars
• recognize • parse
• = DFSAs • = NDPDAs

Lecture Notes 14 Pushdown Automata 8

You might also like