Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
CS 133 : Automata Theory and Computability Context-Free Grammars and Languages Henry N. Adorna Nestine Hope S. Hernandez Algorithms and Complexity Laboratory Department of Computer Science University of the Philippines, Diliman {hnadorna,nshernandez}@dcs.upd.edu.ph
LEC 6
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Context-Free Grammars and Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Context-Free Grammars and Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Schematic of a finite automaton:
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Schematic of a finite automaton:
Schematic of a pushdown automaton:
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Definition A pushdown automaton(PDA) is a 6-tuple (Q, Σ, Γ, δ, q0 , F ), where Q, Σ, Γ and F are all finite sets, and 1. Q is the set of states, 2. Σ is the input alphabet, 3. Γ is the stack alphabet, 4. δ : Q × Σε × Γε → ℘(Q × Γε ) is the transition function, 5. q0 ∈ Q is the start state, and 6. F ⊆ Q is the set of accept states.
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Computation by a PDA Accept input w if w can be written as w = w1 w2 · · · wm , where each wi ∈ Σε and sequences of states r0 , r1 , · · · , rm ∈ Q and strings s0 , s1 , · · · , sm ∈ Γ∗ exist that satisfy the ff: 1. r0 = q0 and s0 = ε 2. For i = 0, · · · , m − 1, we have (ri+1 , b) ∈ δ(ri , wi+1 , a), where si = at and si+1 = bt for some a, b ∈ Γε and t ∈ Γ∗ 3. rm ∈ F
Pushdown Automata
Equivalence of PDAs and CFGs
Example ({0n 1n |n ≼ 0})
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Example ({0n 1n |n ≥ 0}) Let M1 = (Q, Σ, Γ, δ, q1 , F ) where • Q = {q1 , q2 , q3 , q4 }, • Σ = {0, 1}, • Γ = {0, $}, • F = {q1 , q4 } and • δ is given by the following table
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Example ({0n 1n |n ≥ 0}) Let M1 = (Q, Σ, Γ, δ, q1 , F ) where • Q = {q1 , q2 , q3 , q4 }, • Σ = {0, 1}, • Γ = {0, $}, • F = {q1 , q4 } and • δ is given by the following table Input: Stack: q1 q2 q3 q4
0
0 | $
|
ε
{(q2 , 0)}
0 {(q3 , ε)} {(q3 , ε)}
1 | $
|
ε
0
ε | $
{(q4 , ε)}
| ε {(q2 , $)}
Pushdown Automata
Input: Stack: q1 q2 q3 q4
0
Equivalence of PDAs and CFGs
0 | $
|
ε
{(q2 , 0)}
0 {(q3 , ε)} {(q3 , ε)}
Closure Properties of Context-Free Languages
1 | $
|
ε
0
ε | $
{(q4 , ε)}
| ε {(q2 , $)}
Pushdown Automata
Input: Stack: q1 q2 q3 q4
start
0
Equivalence of PDAs and CFGs
0 | $
|
ε
{(q2 , 0)}
q1
ε, ε → $
Closure Properties of Context-Free Languages
1 | $
0 {(q3 , ε)} {(q3 , ε)}
q2
ε, $ → ε
q3
ε
0
ε | $
{(q4 , ε)}
0, ε → 0
1, 0 → ε
q4
|
1, 0 → ε
| ε {(q2 , $)}
Pushdown Automata
Equivalence of PDAs and CFGs
Example {ai bj ck |i, j, k ≼ 0 and i = j or i = k}
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Example {ai bj ck |i, j, k ≥ 0 and i = j or i = k}
c, ε → ε
b, a → ε
start
q1
ε, ε → ε
q3
ε, $ → ε
q4
ε, ε → $ q2 a, ε → a
ε, ε → ε
q5
b, ε → ε
ε, ε → ε
q6 c, a → ε
ε, $ → ε
q7
Pushdown Automata
Equivalence of PDAs and CFGs
Example {wwR |w ∈ {0, 1}∗ }
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Example {wwR |w ∈ {0, 1}∗ }
0, ε → 0
start
q1
ε, ε → $
q2
1, ε → 1
ε, ε → ε
q4
ε, $ → ε
q3
0, 0 → ε
1, 1 → ε
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Context-Free Grammars and Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Theorem A language is context free if and only if some pushdown automaton recognizes it.
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
From CFG to PDA 1. Place the marker symbol $ and the start variable on the stack. 2. Repeat the following steps forever. a. If the top of stack is a variable symbol A, nondeterministically select one of the rules for A and substitute A by the string on the right-hand side of the rule. b. If the top of the stack is a terminal symbol a, read the next symbol from the input and compare it to a. If they match, repeat. If they do not match, reject on this branch of the nondeterminism. c. If the top of the stack is the symbol $, enter the accept state. Doing so accepts the input if it has all been read.
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
From CFG to PDA 1. Place the marker symbol $ and the start variable on the stack. 2. Repeat the following steps forever. a. If the top of stack is a variable symbol A, nondeterministically select one of the rules for A and substitute A by the string on the right-hand side of the rule. b. If the top of the stack is a terminal symbol a, read the next symbol from the input and compare it to a. If they match, repeat. If they do not match, reject on this branch of the nondeterminism. c. If the top of the stack is the symbol $, enter the accept state. Doing so accepts the input if it has all been read.
Pushdown Automata
Equivalence of PDAs and CFGs
Example Construct a PDA from the following CFG: S → aT b | b T → Ta | ε
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Example Construct a PDA from the following CFG: S → aT b | b T → Ta | ε
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
From PDA to CFG First, we slightly modify P to give it the following features: 1. It has a single accept state, qaccept . 2. It empties its stack before accepting. 3. Each transition either pushes a symbol onto the stack or pops one off the stack, but it does not do both at the same time.
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
From PDA to CFG First, we slightly modify P to give it the following features: 1. It has a single accept state, qaccept . 2. It empties its stack before accepting. 3. Each transition either pushes a symbol onto the stack or pops one off the stack, but it does not do both at the same time. Let the PDA P = (Q, Σ, Γ, δ, q0 , {qaccept }). We construct the CFG G as follows: The variables of G are {Apq | p, q ∈ Q}. The start variable is Aq0 ,qaccept . The rules of G are described as follows: • For each p, q, r, s ∈ Q, t ∈ Γ, and a, b ∈ Σε , if δ(p, a, ε) contains
r, t and δ(s, b, t) contains (q, ε), put the rule Apq → aArs b in G. • For each p, q, r, ∈ Q, put the rule Apq → Apr Arq in G. • Finally, for each p ∈ Q, put the rule App → ε in G.
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Context-Free Grammars and Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
The context-free languages are closed under the following operations:
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
The context-free languages are closed under the following operations: 1. union
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
The context-free languages are closed under the following operations: 1. union 2. concatenation
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
The context-free languages are closed under the following operations: 1. union 2. concatenation 3. star
Pushdown Automata
Equivalence of PDAs and CFGs
Closure Properties of Context-Free Languages
Questions? See you next meeting!