pushdown automata (pda) intro ma/csse 474 theory of computation
TRANSCRIPT
![Page 1: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/1.jpg)
Pushdown Automata (PDA) Intro
MA/CSSE 474Theory of Computation
![Page 2: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/2.jpg)
Your Questions?• Previous class days'
material• Reading Assignments
• HW10 problems
• Anything else
![Page 3: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/3.jpg)
Recap: Normal Forms for Grammars
Chomsky Normal Form, in which all rules are of one of the following two forms:
● X a, where a , or● X BC, where B and C are elements of V - .
Advantages:
● Parsers can use binary trees.● Exact length of derivations is known:
S
A B
A A B B
a a b B B
b b
![Page 4: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/4.jpg)
Recap: Normal Forms for Grammars
Greibach Normal Form, in which all rules are of the following form:
● X a , where a and (V - )*.
Advantages:
● Every derivation of a string s contains |s| rule applications.
● Greibach normal form grammars can easily be converted to pushdown automata with no - transitions. This is useful because such PDAs are guaranteed to halt.
![Page 5: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/5.jpg)
Theorems: Normal Forms ExistTheorem: Given a CFG G, there exists an equivalent Chomsky normal form grammar GC such that:
L(GC) = L(G) – {}.
Proof: The proof is by construction.
Theorem: Given a CFG G, there exists an equivalent Greibach normal form grammar GG such that:
L(GG) = L(G) – {}.
Proof: The proof is also by construction.
Details of Chomsky conversion are complex but straightforward; I leave them for you to read in Chapter 11 and/or in the next 16 slides.
Details of Greibach conversion are more complex but still straightforward; I leave them for you to read in Appendix D if you wish (not req'd).
![Page 6: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/6.jpg)
The Price of Normal Forms
E E + EE (E)E id
Converting to Chomsky normal form:
E E EE P EE L EE E RE id L (R )P +
Conversion doesn’t change weak generative capacity but it may change strong generative capacity.
![Page 7: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/7.jpg)
Pushdown Automata
![Page 8: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/8.jpg)
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
![Page 9: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/9.jpg)
Definition of a Pushdown Automaton
M = (K, , , , s, A), where: K is a finite set of states is the input alphabet is the stack alphabet s K is the initial state A K is the set of accepting states, and is the transition relation. It is a finite subset of
(K ( {}) *) (K *)
state input or string of state string ofsymbols
symbolsto pop to
pushfrom top on
top of stack of
stack
and are not necessarily disjoint
![Page 10: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/10.jpg)
Definition of a Pushdown Automaton
A configuration of M is an element of K * *.
The initial configuration of M is (s, w, ), where w is the input string.
![Page 11: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/11.jpg)
Manipulating the Stackc will be written as cab
a
b
If c1c2…cn is pushed onto the stack:
c1
c2
cn
cab
c1c2…cncab
![Page 12: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/12.jpg)
Yields
Let c be any element of {}, Let 1, 2 and be any elements of *, and Let w be any element of *.
Then:(q1, cw, 1) |-M (q2, w, 2) iff ((q1, c, 1), (q2, 2)) .
Let |-M* be the reflexive, transitive closure of |-M.
C1 yields configuration C2 iff C1 |-M* C2
![Page 13: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/13.jpg)
Computations
A computation by M is a finite sequence of configurations C0, C1, …, Cn for some n 0 such that:
● C0 is an initial configuration, ● Cn is of the form (q, , ), for some state q KM and some string in *, and ● C0 |-M C1 |-M C2 |-M … |-M Cn.
![Page 14: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/14.jpg)
Nondeterminism
If M is in some configuration (q1, s, ) it is possible that:
● contains exactly one transition that matches.
● contains more than one transition that matches.
● contains no transition that matches.
![Page 15: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/15.jpg)
Accepting A computation C of M is an accepting computation iff:
● C = (s, w, ) |-M* (q, , ), and ● q A. M accepts a string w iff at least one of its computations accepts. Other paths may: ● Read all the input and halt in a nonaccepting state, ● Read all the input and halt in an accepting state with the stack not empty, ● Loop forever and never finish reading the input, or ● Reach a dead end where no more input can be read.
The language accepted by M, denoted L(M), is the set of all strings accepted by M.
![Page 16: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/16.jpg)
Rejecting
A computation C of M is a rejecting computation iff:
● C = (s, w, ) |-M* (q, w, ), ● C is not an accepting computation, and ● M has no moves that it can make from (q, , ).
M rejects a string w iff all of its computations reject.
Note that it is possible that, on input w, M neither accepts nor rejects.
![Page 17: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/17.jpg)
A PDA for Bal
M = (K, , , , s, A), where:K = {s} the states = {(, )} the input alphabet = {(} the stack alphabetA = {s} contains:
((s, (, ), (s, ( )) **((s, ), ( ), (s, ))
**Important: This does not mean that the stack is empty
![Page 18: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/18.jpg)
A PDA for AnBn = {anbn: n 0}
![Page 19: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/19.jpg)
M = (K, , , , s, A), where: K = {s, f} the states = {a, b, c} the input alphabet = {a, b} the stack alphabet A = {f} the accepting states contains: ((s, a, ), (s, a))
((s, b, ), (s, b))((s, c, ), (f, ))((f, a, a), (f, ))((f, b, b), (f, ))
A PDA for {wcwR: w {a, b}*}
![Page 20: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/20.jpg)
A PDA for {anb2n: n 0}
![Page 21: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/21.jpg)
A PDA for PalEven ={wwR: w {a, b}*}
S S aSaS bSb
A PDA:
This one is nondeterministic
![Page 22: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/22.jpg)
A PDA for {w {a, b}* : #a(w) = #b(w)}
![Page 23: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/23.jpg)
More on NondeterminismAccepting Mismatches
L = {ambn : m n; m, n > 0}
Start with the case where n = m:
a//a
b/a/
b/a/
1 2
![Page 24: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/24.jpg)
More on NondeterminismAccepting Mismatches
L = {ambn : m n; m, n > 0}
Start with the case where n = m:
a//a
b/a/
b/a/
● If stack and input are empty, halt and reject.
● If input is empty but stack is not (m > n) (accept):
● If stack is empty but input is not (m < n) (accept):
1 2
![Page 25: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/25.jpg)
More on NondeterminismAccepting Mismatches
L = {ambn : m n; m, n > 0}
a//a
b/a/
b/a/
● If input is empty but stack is not (m < n) (accept):
a//a
b/a/
b/a/
/a/
/a/
1 2
21 3
![Page 26: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/26.jpg)
More on NondeterminismAccepting Mismatches
L = {ambn : m n; m, n > 0}
a//a
b/a/
b/a/
● If stack is empty but input is not (m > n) (accept):
a//a
b/a/
b/a/
1 2
21 4 b//
b//
![Page 27: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/27.jpg)
Putting It TogetherL = {ambn : m n; m, n > 0}
● Jumping to the input clearing state 4: Need to detect bottom of stack.
● Jumping to the stack clearing state 3: Need to detect end of input.
![Page 28: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/28.jpg)
The Power of Nondeterminism
Consider AnBnCn = {anbncn: n 0}.
PDA for it?
![Page 29: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/29.jpg)
The Power of Nondeterminism
Consider AnBnCn = {anbncn: n 0}.
Now consider L = AnBnCn. L is the union of two languages:
1. {w {a, b, c}* : the letters are out of order}, and
2. {aibjck: i, j, k 0 and (i j or j k)} (in other words, unequal numbers of a’s, b’s, and c’s).
![Page 30: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/30.jpg)
A PDA for L = AnBnCn
![Page 31: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/31.jpg)
Are the Context-Free Languages Closed Under Complement?
AnBnCn is context free.
If the CF languages were closed under complement, then
AnBnCn = AnBnCn
would also be context-free.
But we will prove that it is not.
![Page 32: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/32.jpg)
L = {anbmcp: n, m, p 0 and n m or m p}
S NC /* n m, then arbitrary c'sS QP /* arbitrary a's, then p mN A /* more a's than b'sN B /* more b's than a'sA aA aAA aAbB bB BbB aBbC | cC /* add any number of c'sP B' /* more b's than c'sP C' /* more c's than b'sB' b B' bB'B' bB'cC' c | C'cC' C'cC' bC'cQ | aQ /* prefix with any number of a's
![Page 33: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/33.jpg)
Reducing Nondeterminism
● Jumping to the input clearing state 4: Need to detect bottom of stack, so push # onto the stack before we start.● Jumping to the stack clearing state 3: Need to detect end of input. Add to L a termination character (e.g., $)
![Page 34: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/34.jpg)
Reducing Nondeterminism
● Jumping to the input clearing state 4:
![Page 35: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/35.jpg)
Reducing Nondeterminism
● Jumping to the stack clearing state 3:
![Page 36: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/36.jpg)
More on PDAs A PDA for {wwR : w {a, b}*}:
What about a PDA to accept {ww : w {a, b}*}?
![Page 37: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/37.jpg)
PDAs and Context-Free Grammars
Theorem: The class of languages accepted by PDAs is exactly the class of context-free languages.
Recall: context-free languages are languages that can be defined with context-free grammars.
Restate theorem:
Can describe with context-free grammar
Can accept by PDA
![Page 38: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/38.jpg)
Going One Way
Lemma: Each context-free language is accepted by some PDA.
Proof (by construction):
The idea: Let the stack do the work.
Two approaches:
• Top down
• Bottom up
![Page 39: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/39.jpg)
Top Down The idea: Let the stack keep track of expectations.
Example: Arithmetic expressions
E E + TE TT T FT FF (E)F id
(1) (q, , E), (q, E+T) (7) (q, id, id), (q, )(2) (q, , E), (q, T) (8) (q, (, ( ), (q, )(3) (q, , T), (q, T*F) (9) (q, ), ) ), (q, )(4) (q, , T), (q, F) (10) (q, +, +), (q, )(5) (q, , F), (q, (E) ) (11) (q, , ), (q, )(6) (q, , F), (q, id)
![Page 40: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/40.jpg)
A Top-Down Parser
The outline of M is:
M = ({p, q}, , V, , p, {q}), where contains: ● The start-up transition ((p, , ), (q, S)).
● For each rule X s1s2…sn. in R, the transition: ((q, , X), (q, s1s2…sn)).
● For each character c , the transition: ((q, c, c), (q, )).
![Page 41: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/41.jpg)
Example of the Construction L = {anb*an}
0 (p, , ), (q, S) (1) S * 1 (q, , S), (q, )(2) S B 2 (q, , S), (q, B)(3) S aSa 3 (q, , S), (q, aSa)(4) B 4 (q, , B), (q, )(5) B bB 5 (q, , B), (q, bB)
6 (q, a, a), (q, )input = a a b b a a 7 (q, b, b), (q, )trans state unread input stack
p a a b b a a 0 q a a b b a a S3 q a a b b a a aSa6 q a b b a a Sa3 q a b b a a aSaa6 q b b a a Saa2 q b b a a Baa5 q b b a a bBaa7 q b a a Baa5 q b a a bBaa7 q a a Baa4 q a a aa6 q a a6 q
![Page 42: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/42.jpg)
Another ExampleL = {anbmcpdq : m + n = p + q}
(1)S aSd(2)S T(3)S U(4)T aTc(5)T V(6)U bUd(7)U V(8)V bVc(9)V
input = a a b c d d
![Page 43: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/43.jpg)
Another Example L = {anbmcpdq : m + n = p + q}
0 (p, , ), (q, S)(1) S aSd 1 (q, , S), (q, aSd)(2) S T 2 (q, , S), (q, T)(3) S U 3 (q, , S), (q, U)(4) T aTc 4 (q, , T), (q, aTc)(5) T V 5 (q, , T), (q, V)(6) U bUd 6 (q, , U), (q, bUd)(7) U V 7 (q, , U), (q, V)(8) V bVc 8 (q, , V), (q, bVc)(9) V 9 (q, , V), (q, )
10 (q, a, a), (q, )11 (q, b, b), (q, )
input = a a b c d d 12 (q, c, c), (q, )13 (q, d, d), (q, )
trans state unread input stack
![Page 44: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/44.jpg)
The Other Way to Build a PDA - Directly
L = {anbmcpdq : m + n = p + q}
(1) S aSd (6) U bUd(2) S T (7) U V(3) S U (8) V bVc(4) T aTc (9) V (5) T V
input = a a b c d d
![Page 45: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/45.jpg)
The Other Way to Build a PDA - DirectlyL = {anbmcpdq : m + n = p + q}
(1) S aSd (6) U bUd(2) S T (7) U V(3) S U (8) V bVc(4) T aTc (9) V (5) T V
input = a a b c d d
1 2 3 4
a//a b//a c/a/ d/a/
b//a c/a/ d/a/
c/a/ d/a/
d/a/
![Page 46: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/46.jpg)
Notice Nondeterminism Machines constructed with the algorithm are often nondeterministic,
even when they needn't be. This happens even with trivial languages.
Example: AnBn = {anbn: n 0}
A grammar for AnBn is: A PDA M for AnBn is:
(0) ((p, , ), (q, S))[1] S aSb (1) ((q, , S), (q, aSb))[2] S (2) ((q, , S), (q, ))
(3) ((q, a, a), (q, )) (4) ((q, b, b), (q, ))
But transitions 1 and 2 make M nondeterministic.
A directly constructed machine for AnBn:
![Page 47: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/47.jpg)
Bottom-Up
(1) E E + T(2) E T(3) T T F (4) T F(5) F (E)(6) F id
Reduce Transitions:(1) (p, , T + E), (p, E)(2) (p, , T), (p, E)(3) (p, , F T), (p, T)(4) (p, , F), (p, T)(5) (p, , )E( ), (p, F)(6) (p, , id), (p, F)
Shift Transitions(7) (p, id, ), (p, id) (8) (p, (, ), (p, () (9) (p, ), ), (p, )) (10) (p, +, ), (p, +) (11) (p, , ), (p, )
The idea: Let the stack keep track of what has been found.
![Page 48: Pushdown Automata (PDA) Intro MA/CSSE 474 Theory of Computation](https://reader036.vdocuments.us/reader036/viewer/2022062422/56649eab5503460f94bb1a0f/html5/thumbnails/48.jpg)
A Bottom-Up Parser The outline of M is:
M = ({p, q}, , V, , p, {q}), where contains:
● The shift transitions: ((p, c, ), (p, c)), for each c .
● The reduce transitions: ((p, , (s1s2…sn.)R), (p, X)), for each rule X s1s2…sn. in G.
● The finish up transition: ((p, , S), (q, )).