Presentation is loading. Please wait.

Presentation is loading. Please wait.

Pushdown automata The Chinese University of Hong Kong Fall 2011

Similar presentations


Presentation on theme: "Pushdown automata The Chinese University of Hong Kong Fall 2011"— Presentation transcript:

1 Pushdown automata The Chinese University of Hong Kong Fall 2011
CSCI 3130: Formal languages and automata theory Pushdown automata Andrej Bogdanov

2 Motivation regular expression NFA DFA syntactic computational
is more powerful than CFG pushdown automaton syntactic computational

3 Pushdown automata versus NFA
state control input 1 NFA

4 Pushdown automata pushdown automaton (PDA)
state control input 1 stack pushdown automaton (PDA) A PDA is like an NFA with but with an infinite stack

5 pushdown automaton (PDA)
Pushdown automata state control input 1 $ 1 1 stack pushdown automaton (PDA) As the PDA is reading the input, it can push / pop symbols from the top of the stack

6 Building a PDA L = {0n1n: n ≥ 1} state control We remember each 0
pop $ push $ We remember each 0 by pushing x onto the stack read 0 push x read 1 pop x When we see a 1, we pop an x from the stack read 1 pop x We want to accept when we hit the stack bottom We’ll use t$ mark bottom

7 A PDA in action L = {0n1n: n ≥ 1} state control input 1 1 1 … x x x
push $ input read 0 1 1 1 push x read 1 pop x $ x x x read 1 stack pop x pop $

8 Notation for PDAs read, pop / push push $ e, e / $ read 0 0, e / x
1, x / e 0, e / x e, $ / e e, e / $ q0 q1 q2 q3 read 1 pop x read 0 push x pop $ push $ read, pop / push

9 Definition of a PDA A pushdown automaton is (Q, , , , q0, F) where:
Q is a finite set of states;  is the input alphabet;  is the stack alphabet q0 in Q is the initial state; F  Q is a set of final states;  is the transition function d: Q  (  {})  (  {}) → subsets of Q  (G  {}) state input symbol pop symbol state push symbol

10 Example S = {0, 1} G = {$, x} d(q0, e, e) = {(q1, $)} d(q0, e, $) = ∅
1, x / e 0, e / x e, $ / e e, e / $ q0 q1 q2 q3 S = {0, 1} G = {$, x} d(q0, e, e) = {(q1, $)} d(q0, e, $) = ∅ d(q0, e, x) = ∅ d(q0, 0, e) = ∅ ... d: Q  (  {})  (  {}) → subsets of Q  (G  {}) state input symbol pop symbol state push symbol

11 The language of a PDA A PDA is nondeterministic Multiple transitions on same pop/input allowed Transitions may but do not have to push or pop The language of a PDA is the set of all strings in S* that can lead the PDA to an accepting state

12 Example 1 L = {w#wR: w  {0, 1}*} S = {0, 1, #} G = {0, 1}
e, e / $ q0 q3 e, $ / e q2 #, e / e 0, e / 0 q1 1, e / 1 0, 0 / e 1, 1 / e write w on stack read wR from stack

13 Example 2 L = {wwR: w  S*} S = {0, 1} e, 00, 0110  L
e, e / $ q0 q3 e, $ / e e, e / e 0, e / 0 q1 1, e / 1 0, 0 / e 1, 1 / e q2 guess middle of string

14 Example 3 L = {w: w = wR, w  S*} S = {0, 1} e, 1, 00, 010, 0110  L
or x xR 011 ∉ L e, e / e 0, e / e 1, e / e e, e / $ q0 q3 e, $ / e 0, e / 0 q1 1, e / 1 0, 0 / e 1, 1 / e q2 middle symbol can be e, 0, or 1

15 Example 4 L = {0n1m0m1n | n  0, m  0} S = {0, 1} input: 0n 1m 0m 1n
q1 e, e / e 1, e / 1 q2 e, e / $ q0 e, $ / e q5 input: 0n 1m 0m 1n e, e / e q3 0, 1 / e stack: 0n 1m e, e / e q4 1, 0/ e

16 Example 5 L = {w: w has same number 0s and 1s} S = {0, 1} Strategy:
Stack keeps track of excess of 0s or 1s If at the end, stack is empty, number is equal e, e / $ q0 q3 e, $ / e q1 0, e / 0 1, e / 1 0, 1 / e 1, 0 / e

17 Example 5 L = {w: w has same number 0s and 1s} S = {0, 1} Invariant:
In every execution of the PDA: #1 - #0 on stack = #1 - #0 in input so far 0, e / 0 e, e / $ q0 q1 1, e / 1 q3 e, $ / e 0, 1 / e 1, 0 / e If w is not in L, it must be rejected

18 Example 5 L = {w: w has same number 0s and 1s} S = {0, 1} Property:
In some execution of the PDA: stack consists only of 0s or only of 1s (or e) 0, e / 0 e, e / $ q0 q1 1, e / 1 q3 e, $ / e 0, 1 / e 1, 0 / e If w is in L, some execution will accept

19 Example 5 L = {w: w has same number 0s and 1s} S = {0, 1} read stack
e, e / $ q0 q1 1, e / 1 q3 e, $ / e 0, 1 / e 1, 0 / e read stack w = $0 $00 1 $0 1 $ 1 $1 $

20 Example 6 L = {w: w has two 0-blocks with same number of 0s Strategy:
01011, , , 01111 allowed not allowed Strategy: Detect start of first 0-block Push 0s on stack Detect start of second 0-block Pop 0s from stack

21 Example 6 1 Detect start of first 0-block 2 Push 0s on stack
q0 q1 0, 1 1 e 0, e / 0 1 Detect start of first 0-block 2 Push 0s on stack 1 0, 1 0, 0 / e 3 Detect start of second 0-block 4 Pop 0s from stack

22 Example 6 L = {w: w has two 0-blocks with same number of 0s 0, e / e
q0 q1 1, e / e 1 2 q2 0, e / 0 q3 3 1, e / e q4 0, e / e 1, e / e q6 0, e / e q7 e, $ / e 4 q5 0, 0 / e

23 CFG ↔ PDA conversions

24 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

25 A convention When we have a sequence of transitions like:
We will abbreviate it like this: e, e / c q0 q1 x, a / b e, e / d pop a, then push b, c, and d x, a / bcd q0 q1 replace a by bcd on stack

26 Converting a CFG to a PDA
Idea: Use PDA to simulate derivations A → 0A1 A → B B → # A  0A1  00A11  00B11  00#11 PDA control: stack: input: write start variable e, e / A $A 00#11 replace production in reverse e, A / 1A0 $1A0 00#11 pop terminal and match 0, 0 / e $1A 0#11 replace production in reverse e, A / 1A0 $11A0 0#11 pop terminal and match 0, 0 / e $11A #11 replace production in reverse e, A / B $11B #11

27 Converting a CFG to a PDA
q0 e, e / $A e, $ / e q1 q2 A → 0A1 A → B B → # e, A / 1A0 e, A / B e, B / # 0, 0 / e 1, 1 / e #, # / e CFG input stack $A 00#11 $1A0 00#11 $1A 00#11 $11A0 00#11 $11A 00#11 $11B 00#11 $11# 00#11 $11 00#11 $1 00#11 $ 00#11 A  0A1  00A11  00B11  00#11

28 General PDA to CFG conversion
a, a / e e, A / ak...a1 for every terminal a for every production A → a1...ak e, e / $S e, $ / e q0 q1 q2


Download ppt "Pushdown automata The Chinese University of Hong Kong Fall 2011"

Similar presentations


Ads by Google