Plan for Today Equivalence between PDAs and CFGs
Plan for Today • Equivalence between PDAs and CFGs • More Examples of PDAs
What actually happened today • Reviewed definitions of context free grammar (CFG) and push down automata (PDA). • Showed how context free grammars can be recognized by pushdown automata (PDA).
• • • Context Free Grammar • 4 -tuple, (V, Σ, R, S) V: variables Σ: alphabet R: rules, V (V �Σ)* aka. A w for some • w ∈ (V �Σ)* S ∈ V: starting variable We say that x ∈ L if there is a sequence of variable substitutions that ends up with x In internet terminology: can you generate w by repeatedly replacing variables by strings in the given rules
Examples of CFGs • A = {0 n#02 n| n ≥ 0} (aside: check this is not regular) • s # | 0 s 00 • Heuristic for checking whether a language is context free (warning: this does not always work): it counts one numbers
Formal Definition of PDA • • • 6 -tuple, (Q, Σ, Γ, δ, q 0, F) Q: states, Σ: alphabet Γ: stack symbols δ: Q × Σε × Γε P(Q × Γε) transition function q 0: starting state F: accepting states
PDA accepts a string w if w=w 0 w 1…wm (some wi may be ε) m: #steps stacks s 0 s 1. . sm, states: r 0…rm Start r 0= q 0, final state accepting rm ∈ F s 0=ε For every i, exists popi and pushi and tempi s. t • si = tempi popi • si+1 = tempi pushi • The transition of going from ri to ri+1 by popping popi (possibly ε) and pushing pushi (possibly also ε) is valid: (ri+1, pushi) ∈ δ(ri, wi, popi)
CFGs are recognized by PDAs Left-to-right traversal of parse tree Stack of PDA stack when traversing tree Resulting PDA has two major components: Apply rule A w: • Pop A off of stack • Push w. R onto stack Cancel matches w. the input string
CFGs are recognized by PDAs S # | 0 S 00 S #: can remove S from stack, push # Apply S 0 S 00: can remove S from rules stack, push 0, 0, S, 0 iteratively ε, S # ε, S 0 S 00 q 0 #, # ε 0, 0 ε 1, 1 ε Cancel stack with tape
Multi push/pops q 1 Push/pop multiple q 2 ε, S 0 S 00 q 0 ε, ε 0 Note: because stack is flipped, push the symbols in reverse order (this is the notation in the text book) ε, ε S q 3
CFGs are recognized by PDAs ε, S # ε, ε S q 0 ε, S 0 S 00 q 1 #, # ε Example string 0, 0 ε 1, 1 ε 00#0000 ε, ε State: q. F Stack:
CFGs are recognized by PDAs ε, S # ε, ε S q 0 ε, S 0 S 00 q 1 #, # ε 0, 0 ε 1, 1 ε ε, ε q. F Sequence of input|state|stack when ran on the input 00#0000: • 00#0000|q 0| ε • 00#0000|q 1| S • 00#0000|q 1| 00 S 0 • 0#0000|q 1| 00 S • 0#0000|q 1| 0000 S 0 • #0000|q 1| 0000 S • #0000|q 1| 0000#
CFGs are recognized by PDAs ε, S # ε, ε S q 0 ε, S 0 S 00 q 1 #, # ε 0, 0 ε 1, 1 ε ε, ε q. F Sequence of input|state|stack when ran on the input 00#0000: • #0000|q 1| 0000# • 0000|q 1| 0000 • 000|q 1| 000 • 00|q 1| 00 • 0|q 1| 0 • |q 1| • |q. F|
Admin Items • This conversion is actually not that useful: it’s usually easier to just build CFG/PDAs when you need them • HW 5 now due Thu Apr 2 • Test 3 Thu Apr 9, 11: 00 am - 1: 30 pm via Grade. Scope. • If test is in an awkward time zone, please email for alternate arrangements. • Office hours (schedule on website): https: //gatech. bluejeans. com/242024735
Languages recognized by PDAs are CFGs General rule for conversion • What goes up must come down • One rule for what happens between each symbol going on the stack, and coming off of the stack Create a rule for: I went from state p to state s, starting w. empty stack, ending w. empty stack
Simplifying PDA Each transition either push or pop, but not both a, b c a, b ε ε, ε c
Converting PDAs to CFG variable for all pairs of states: Aps generates all strings that get me from p to s, and leaves the stack empty
Possible ways of moving from p to s First step: • Pushed nothing: we went to q, via (p, c, ε) (q, ε) then we went from q to s starting with /ending at empty stacks • Pushed x, 2 more possibilities: • x was popped at some intermediate state q: break down into p q, q s • x was popped just before we got to s: Intermediate states (after p, before s) also start/end with empty stack: x was never removed
Case 1: pushed nothing on first step c, ε ε p q ? ? ? Corresponding CFG rule: Aps c. Aqs s
Case 2 a: x popped at some intermediate state q Stack symbol x ∈ Γ p ? ? ? q ? ? ? Corresponding CFG rule: Aps Apq. Aqs s
Case 2 b: x was never popped until right before s Stack symbol x ∈ Γ q p c 1, ε x ? ? ? Corresponding CFG rule: Aps c 1 Aqrc 2 r s c 2, x ε
Creation of CFG rules Case 1: for all rules of the form (p, c, ε) (q, ε) Case 2 a: for all triples p, q, s Case 2 b: for all triples p, q, r, s and rules: (p, c, ε) (q, ε)
CFG / PDA for a more complicated language L = {ww. R |w ∈ {0, 1}*} w. R: the reversal of w
CFG / PDA for a more complicated language L = {aibjck |i, j, k ≥ 0 , i=j or j=k} Next class
All languages Turing decidable (hw 6) Context free ? ? ? (hw 6) Regular
- Slides: 24