Upload
tranquynh
View
215
Download
0
Embed Size (px)
Citation preview
1
CS 301 - Lecture 11 Nondeterministic Pushdown
Automata Fall 2008
Review • Languages and Grammars
– Alphabets, strings, languages • Regular Languages
– Deterministic Finite and Nondeterministic Automata – Equivalence of NFA and DFA – Regular Expressions – Regular Grammars – Properties of Regular Languages – Languages that are not regular and the pumping lemma
• Context Free Languages – Context Free Grammars – Derivations: leftmost, rightmost and derivation trees – Parsing and ambiguity – Simplifying Grammars and Normal Forms
• Today: – Nondeterministic Pushdown Automata
Pushdown Automata PDAs
Pushdown Automaton -- PDA Input String
Stack
States
2
The States
q1 q2a, b→ c
Input symbol
Pop symbol
Push symbol
q1 q2a, b→ c
a
b top
input
stack
a
Replace eh
$eh
$
c
q1 q2a, λ → c
a a
Push b
eh
$eh
$
bc
top
input
stack
q1 q2a, b→ λ
a a
Pop b
eh
$eh
$
top
input
stack
3
q1 q2a, λ → λ
a a
No Change b
eh
$eh
$
btop
input
stack
q1 q2λ→$,a
a a
Pop $ top
input
stack
A Possible Transition
empty
q1 q2cba →,
a
input
A Bad Transition
The automaton Halts in state and Rejects the input string
Empty stack
q1
HALT
q1 q2ca →λ,
a
input
A Bad Transition
The automaton Halts in state and Rejects the input string
Empty stack
q1
HALT
4
q1 q2zyx →,
No transition is allowed to be followed When the stack is empty
Empty stack
q1 q2ba →$,
a a
Pop $ top
input
stack
A Good Transition
b
Non-Determinism
q1
q2a, b→ c
q3a, b→ c
q1 q2λ, b→ c
transition−λ
These are allowed transitions in a Non-deterministic PDA (NPDA)
NPDA: Non-Deterministic PDA
Example:
λ, λ → λ
a, λ → a
b, a→ λq0 q1 q2 q3
b, a→ λ
λ, $→ $
5
a, λ → a
b, a→ λ0q q1 q2 q3
Execution Example: Input
a a a b b b
current state
b, a→ λ
Time 0
λ, λ → λ λ, $→ $
Stack $
a, λ → a
b, a→ λq0 q1 q2 q3
Input
a a a b b b
b, a→ λ
Time 1
λ, λ → λ λ, $→ $
Stack $
a, λ → a
b, a→ λq0 q1 q2 q3
Input
Stack
a a a b b b$a
b, a→ λ
Time 2
λ, λ → λ λ, $→ $
a, λ → a
b, a→ λq0 q1 q2 q3
Input
Stack
a a a b b b$aa
b, a→ λ
Time 3
λ, λ → λ λ, $→ $
6
a, λ → a
b, a→ λq0 q1 q2 q3
Input
Stack
a a a b b b
$aaa
b, a→ λ
Time 4
λ, λ → λ λ, $→ $
a, λ → a
b, a→ λq0 q1 q2 q3
Input
a a a b b b
Stack
$aaa
b, a→ λ
Time 5
λ, λ → λ λ, $→ $
a, λ → a
b, a→ λq0 q1 q2 q3
Input
a a a b b b$a
Stack
b, a→ λ
Time 6
λ, λ → λ λ, $→ $
a
a, λ → a
b, a→ λq0 q1 q2 q3
Input
a a a b b b$
Stack
b, a→ λ
Time 7
λ, λ → λ λ, $→ $
a
7
a, λ → a
b, a→ λq0 q1 q2 q3
Input
a a a b b b
b, a→ λ
Time 8
accept λ, λ → λ λ, $→ $
$Stack
A string is accepted if there is a computation such that:
All the input is consumed AND The last state is a final state
At the end of the computation, we do not care about the stack contents
The input string is accepted by the NPDA:
aaabbb
a, λ → a
b, a→ λq0 q1 q2 q3
b, a→ λ
λ, λ → λ λ, $→ $
}0:{ ≥= nbaL nn
is the language accepted by the NPDA:
a, λ → a
b, a→ λq0 q1 q2 q3
b, a→ λ
In general,
λ, λ → λ λ, $→ $
8
Another NPDA example
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
NPDA M
}{)( RwwML =
Execution Example:
Input Time 0
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
a ab b
Input
a ab
Time 1
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
ab
Input Time 2
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
aa ab bb
9
Input Time 3
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
aa ab bb
Guess the middle of string
Input Time 4
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
aa ab bb
Input Time 5
Stack
$
λ, $→ $1q q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
a ab b a
Input Time 6
Stack
$
λ, $→ $q1
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
a ab b
accept q2
10
Rejection Example:
Input Time 0
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
a b b b
Input Time 1
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
aa b b b
Input Time 2
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
ab
a b b b
Input Time 3
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
ab
Guess the middle of string
a b b b
11
Input Time 4
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
ab
a b b b
Input Time 5
Stack
$
λ, $→ $1q q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
aa b b b
There is no possible transition.
Input is not consumed
Another computation on same string:
Input Time 0
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
a b b b
Input Time 1
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
aa b b b
12
Input Time 2
Stack
$
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
ab
a b b b
Input Time 3
Stack
$ab
a b b b
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
b
Input Time 4
Stack
a b b b
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
$abbb
Input Time 5
Stack
a b b b
$abbb
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
No final state is reached
13
λ, $→ $q1 q2
bbaa
→
→
λ
λ
,,
λ, λ → λq0
λ
λ
→
→
bbaa,,
There is no computation that accepts string abbb
)(MLabbb∉
A string is rejected if there is no computation such that:
All the input is consumed AND The last state is a final state
At the end of the computation, we do not care about the stack contents
In other words, a string is rejected if in every computation with this string:
The input cannot be consumed OR
The input is consumed and the last state is not a final state
OR The stack head moves below the bottom of the stack
Another NPDA example
q0
NPDA M}1:{)( −≥= mnbaML mn
λ
λ
λ
→
→
→
$,,,
bab
aa
This NPDA generates:
and for each substring u
14
Execution Example:
Input Time 0
Stack
a a b
λ
λ
λ
→
→
→
$,,,
bab
aa
q0
$
Input Time 1
Stack
a a b
q0
a$
λ
λ
λ
→
→
→
$,,,
bab
aa
Input Time 2
Stack
a a b
q0
aa
$
λ
λ
λ
→
→
→
$,,,
bab
aa
Input Time 3
Stack
a a b
q0
a
accept
$
λ
λ
λ
→
→
→
$,,,
bab
aa
15
Rejection example:
Input Time 0
Stack
a b b
q0
b$
Input Time 1
Stack
q0
aa b b b$
λ
λ
λ
→
→
→
$,,,
bab
aa
Input Time 2
Stack
q0
a b b b$
λ
λ
λ
→
→
→
$,,,
bab
aa
Input Time 3
Stack
q0
a b b b
λ
λ
λ
→
→
→
$,,,
bab
aa
16
Input Time 4
Stack
q0Halt and Reject
a b b b
λ
λ
λ
→
→
→
$,,,
bab
aa
Pushing Strings
q1 q2a, b→ w
Input symbol
Pop symbol
Push string
q1 q2a, b→ cdf
a
b top
input
stack
a
Push eh h
e
cdf
pushed string
Example:
$ $
What’s Next • Read
– Linz Chapter 1,2.1, 2.2, 2.3, (skip 2.4), 3, 4, 5, 6.1, 6.2, (skip 6.3), 7.1, and 7.2
– JFLAP Chapter 1, 2.1, (skip 2.2), 3, 4, 5, 6, 7 • Next Lecture Topics from Chapter 7.2
– Pushdown Automata and Context Free Grammars • Quiz 2 in Recitation on Wednesday 10/1
– Covers Linz 3, 4 and JFLAP 3, 4 – Closed book, but you may bring one sheet of 8.5 x 11 inch paper with any
notes you like. – Quiz will take the full hour
• Homework – Homework Due Thursday