PDA and CFG conversions CSCI 3130 Formal Languages and Automata Theory Siu On CHAN Fall 2018 Chinese University of Hong Kong 1/12
CFGs and PDAs L has a context-free grammar if and only if it is accepted by some pushdown automaton. context-free grammar pushdown automaton Will fjrst convert CFG to PDA 2/12
Convention A sequence of transitions like q 0 q 1 will be abbreviated as q 0 q 1 3/12 x , a / b ε, ε/ c ε, ε/ d x , a / bcd replace a by bcd on stack
replace by production in reverse Converting a CFG to a PDA 00#11 . . . 0#11 $11 A 0 A 1 A 0 0#11 $1 A 0 0 pop terminal and match 00#11 $1 A 0 replace by production in reverse $ A Idea: Use PDA to simulate derivations 3. Pop top terminal if it matches input Example: Rules: 1. Write the start symbol A onto the stack production 2. Rewrite variable on top of stack (in reverse) according to PDA control stack input write start variable 4/12 A → 0 A 1 A → B A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 00 B 11 ⇒ 00#11 B → # ε, ε/ A ε, A / 1 A 0
Converting a CFG to a PDA Idea: Use PDA to simulate derivations . . . 0#11 $11 A 0 0#11 $1 A pop terminal and match 00#11 $1 A 0 replace by production in reverse 00#11 $ A write start variable production Example: Rules: 2. Rewrite variable on top of stack (in reverse) according to 1. Write the start symbol A onto the stack 3. Pop top terminal if it matches input PDA control stack input 4/12 A → 0 A 1 A → B A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 00 B 11 ⇒ 00#11 B → # ε, ε/ A ε, A / 1 A 0 0 , 0 /ε ε, A / 1 A 0 replace by production in reverse
Converting a CFG to a PDA $11 B 00#11 $1 A 00#11 $11 A 0 00#11 $11 A 00#11 00#11 CFG $11# 00#11 $11 00#11 $1 00#11 $ $1 A 0 00#11 $ A 00#11 q 0 q 1 q 2 5/12 stack input ε, ε/ $ A ε, $ /ε A → 0 A 1 ε, A / 1 A 0 0 , 0 /ε A → B ε, A / B 1 , 1 /ε ε, B / # # , # /ε B → # A ⇒ 0 A 1 ⇒ 00 A 11 ⇒ 00 B 11 ⇒ 00#11
General CFG to PDA conversion q 0 q 1 q 2 for every terminal a for every production 6/12 a , a /ε ε, A /α k . . . α 1 A → α 1 . . . α k ε, ε/ $ A ε, $ /ε
From PDAs to CFGs context-free grammar pushdown automaton simplifjed PDA Simplifjed pushdown automaton: • Empties its stack before accepting • Each transition is either a push, or a pop, but not both 7/12 • Has a single accepting state
Simplifying the PDA q 3 0 q 0 q 0 Empties its stack before accepting Single accepting state 8/12 q 2 q 1 q 3 q 2 q 1 ε, ε/ε ε, ε/ε = ⇒ ε, ε/ε ε, a /ε for every stack symbol a = ⇒ q ′ ε, ε/ε
Simplifying the PDA q 1 q 1 01 q 0 q 1 q 0 Each transition either pushes or pops, but not both 9/12 01 q 0 q 1 q 0 a , b /ε ε, ε/ c a , b / c = q ′ ⇒ a , ε/ b ε, b /ε a , ε/ε = q ′ ⇒
Simplifjed PDA to CFG Intention: (with empty stack both at q and at r ) 10/12 For every pair ( q , r ) of states in PDA, introduce variable A qr in CFG A qr generates all strings that allow the PDA to go from q to r
Simplifjed PDA to CFG p allowed s r PDA q r q p q CFG 11/12 A qq → ε A pr → A pq A qr a , ε/ x A ps → a A qr b a = ε or b = ε b , x /ε Start variable: A pq (initial state p , accepting state q )
A 00 A 11 A 22 A 01 A 02 A 12 Example: Simplifjed PDA to CFG 1 q 1 q 1 q 1 q 1 q 1 q 1 q 1 q 1 0 0 1 1 $ 0 A 22 $ 0 $ 0 0 $ 0 $ $ 1 $ q 0 , A 11 , A 01 A 11 q 1 q 2 variables: start variable: A 02 productions: A 02 q 0 A 01 A 01 A 12 A 12 1 A 11 0 A 00 A 11 A 11 A 12 A 02 12/12 A 11 0 A 11 1 A 11 A 11 A 11 A 11 ε, ε/ $ ε, $ /ε 1 , ε/ 1 0, ε / 0 0 , 1 /ε 1 , 0 /ε
Example: Simplifjed PDA to CFG $ q 1 q 1 q 1 q 1 0 0 1 1 1 0 $ q 1 0 $ 0 0 $ 0 $ $ 1 $ q 1 q 1 q 0 q 1 q 1 q 2 start variable: A 02 productions: 12/12 q 0 ε, ε/ $ ε, $ /ε variables: A 00 , A 11 , A 22 , A 01 , A 02 , A 12 1 , ε/ 1 0, ε / 0 0 , 1 /ε 1 , 0 /ε A 02 → A 01 A 12 A 01 → A 01 A 11 A 12 → A 11 A 12 A 11 → A 11 A 11 A 11 → 0 A 11 1 A 11 → 1 A 11 0 A 02 → A 11 A 00 → ε , A 11 → ε , A 22 → ε
Example: Simplifjed PDA to CFG $ q 1 q 1 q 1 q 1 0 0 1 1 1 0 $ q 0 0 $ 0 0 $ 0 $ $ 1 $ q 1 q 1 q 1 q 1 q 1 q 2 start variable: A 02 productions: 12/12 q 0 ε, ε/ $ ε, $ /ε variables: A 00 , A 11 , A 22 , A 01 , A 02 , A 12 1 , ε/ 1 0, ε / 0 0 , 1 /ε 1 , 0 /ε A 02 → A 01 A 12 A 01 → A 01 A 11 A 12 → A 11 A 12 A 11 → A 11 A 11 A 11 → 0 A 11 1 A 11 → 1 A 11 0 ε ε A 02 → A 11 A 00 → ε , A 11 → ε , A 22 → ε
Recommend
More recommend