Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown.

Similar presentations


Presentation on theme: "CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown."— Presentation transcript:

1 CSC 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130 The Chinese University of Hong Kong Pushdown automata Fall 2008

2 Motivation We had two ways to describe regular languages How about context-free languages? regular expression DFANFA syntactic computational CFGpushdown automaton syntactic computational

3 Pushdown automata versus NFA Since context-free is more powerful than regular, pushdown automata must generalize NFAs state control 0100 input NFA

4 Pushdown automata A pushdown automaton has access to a stack, which is a potentially infinite supply of memory state control 0100 input pushdown automaton (PDA) … stack

5 Pushdown automata As the PDA is reading the input, it can push / pop symbols in / out of the stack state control 0100 input pushdown automaton (PDA) Z0Z0 01 stack … 1

6 Rules for pushdown automata Multiple and  -transitions are allowed Stack is always accessed from the top Each transition can pop and/or push into stack Transitions depend on input symbol and on last symbol popped from stack Automaton accepts if after reading whole input, it can reach an accepting state

7 Example L = {0 n #1 n : n ≥ 0} state control 000# input $ aa stack a … 111 read 0 push a read # read 1 pop a pop $ push $

8 Shorthand notation read, pop / push read 0 push a read # read 1 pop a pop $ push $ 0,  / a #,  /  1, a /  , $ /  ,  / $ convention: we use $ for “end of stack”

9 Formal definition A pushdown automaton is (Q, , , , q 0, F) : –Q is a finite set of states; –  is the input alphabet; –  is the stack alphabet –q 0 in Q is the initial state; –F  Q is a set of final states; –  is the transition function  : Q  (   {  })  (   {  }) → subsets of Q  (   {  }) stateinput symbolpop symbolstatepush symbol

10 Example  : Q  (   {  })  (   {  }) → subsets of Q  (   {  })  (q 1, 0,  ) = {(q 1, a)}  (q 1, 1,  ) = ∅  (q 1, #,  ) = {(q 1,  )}  (q 1, 0,  ) = ∅... 0,  / a #,  /  1, a /  , $ /  ,  / $ q0q0 q1q1 q2q2 q3q3  = {0, 1, #}  = {$, a}

11 Example 1 L = {ww R : w  ∈  *}  = {0, 1} , 00, 0110 ∈ L 1, 011, 010 ∉ L 0,  / 0 ,  / $ q0q0 q1q1 1,  / 1 0, 0 /  q2q2 1, 1 /  q3q3 , $  / ,  / 

12 Example 2 L = {w: w = w R, w  ∈  *}  = {0, 1} , 1, 00, 010, 0110 ∈ L 011 ∉ L 0,  / 0 ,  / $ q0q0 q1q1 1,  / 1 0, 0 /  q2q2 1, 1 /  q3q3 , $  /  ,  /  0,  /  1,  /  0110110110011010110 or xxxRxR xRxR

13 Example 3 L = {0 n 1 m 0 m 1 n | n  0, m  0}  = {0, 1} 0,  / 0 ,  / $ q0q0 q1q1 1,  / 1 q2q2 , $  /  ,  /  q5q5 q4q4 q3q3 0, 1 /  1, 0/ 

14 Example 4 L = {w: w has same number 0s and 1s}  = {0, 1} Strategy:Stack keeps track of excess of 0 s or 1 s If at the end, stack is empty, number is equal 0,  / 0 ,  / $ q0q0 q1q1 1,  / 1 q3q3 , $  /  0, 1 /  1, 0 / 

15 Example 4 L = {w: w has same number 0s and 1s}  = {0, 1} 0,  / 0 ,  / $ q0q0 q1q1 1,  / 1 q3q3 , $  /  0, 1 /  1, 0 /  w = 001110 readstack 0$0 0$00 1$0 1$ 1$1 0$

16 Example 5 L = {w: w has two 0-blocks with same number of 0s Strategy:Guess where first block starts Record 0 s on stack 01011, 001011001, 10010101001 01001000, 01111 allowednot allowed Guess where second block starts Match 0 s from stack

17 Example 5 L = {w: w has two 0-blocks with same number of 0s Strategy:Guess where first block starts Record 0 s on stack Guess where second block starts Match 0 s from stack It either starts at the beginning, or after some 1 After that we must see at least one 0 Then you see a 1, or a pattern 1(0+1)*1 Accept if stack is empty either after next 1, or if you reach end of string

18 Example 5 L = {w: w has two 0-blocks with same number of 0s 0,  /  ,  / $ q0q0 q1q1 q2q2 1,  /  0,  / 0 q3q3 ,  / $ q5q5 1,  /  q4q4 0,  /  1,  /  0, 0  /  1, $  /  q6q6 0,  /  1,  /  q7q7 , $  / 

19 Main theorem A language L is context-free if and only if it is accepted by some pushdown automaton. context-free grammarpushdown automaton

20 A convention Sometimes we denote “transitions” by: This will mean: a, b  / c 1 c 2 c 3 q0q0 q1q1 ,  / c 2 q0q0 q1q1 a, b  / c 1 ,  / c 3 intermediate states pop b, then push c 1, c 2, and c 3

21 From CFGs to PDAs Idea: Use PDA to simulate derivations A → 0A1 A → B B → # A  0A1  00A11  00B11  00#11 PDA control: CFG: write start variable stack: $A replace production in reverse $1A0 pop terminals and match $1A e, e / A 0, 0 / e e, A / 1A0 input: 00#11 0#11 replace production in reverse $11A0e, A / 1A0 0#11 pop terminals and match $11A0, 0 / e #11 replace production in reverse $11Be, A / B #11

22 From CFGs to PDAs If, after reading whole input, PDA ends up with an empty stack, derivation must be valid Conversely, if there is no valid derivation, PDA will get stuck somewhere –Either unable to match next input symbol, –Or match whole input but stack non empty

23 Description of PDA for CFGs Push $ onto stack Repeat the following steps: –If the top of the stack is a variable A : Choose a rule A →  and substitute A with  –If the top of the stack is a terminal a : Read next input symbol and compare to a If they don’t match, reject (die) –If top of stack is $, go to accept state

24 Description of PDA for CFGs q0q0 q1q1 q2q2 ,  / $S a, a /  for every terminal a , A /  k...  1 for every production A →  1...  k , $ / 

25 From PDAs to CFGs First, we simplify the PDA: –It has a single accept state q f –It empties its stack before accepting –Each transition is either a push, or a pop, but not both context-free grammarpushdown automaton ✓

26 Simplifying the PDA First, we simplify the PDA: –It has a single accept state q f –It empties its stack before accepting –Each transition is either a push, or a pop, but not both 0,  / a 1, a / b 1, b /  q1q1 q2q2 q3q3 ,  /  q0q0 ,  / $ qfqf , b /  , a /  , $ / 

27 Simplifying the PDA First, we simplify the PDA: –It has a single accept state q f –It empties its stack before accepting –Each transition is either a push, or a pop, but not both 0,  / a 1, a / b 1, b /  q1q1 q2q2 q3q3 ,  /  q0q0 ,  / $ qfqf , b /  , a /  , $ / 

28 Simplifying the PDA First, we simplify the PDA: –It has a single accept state q f –It empties its stack before accepting –Each transition is either a push, or a pop, but not both 0,  / a 1, a /  1, b /  q1q1 q2q2 q3q3 ,  / x q0q0 ,  / $ qfqf , b /  , a /  , $ /  q 13 , x /  ,  / x q 23 , x /  q 12 ,  / b

29 From PDAs to CFGs We look at the stack in an accepting computation: a $$ a $ a $ a $ a $ a $ a $ a $ a $$ baccc a portions that preserve the stack q3q3 q1q1 q7q7 q0q0 q1q1 q2q2 q1q1 q3q3 q7q7 A 03 = {x: x leads from q 0 to q 3 and preserves stack}  11  01  0 0 input state stack qfqf q0q0 q1q1 0 q1q1 1

30 From PDAs to CFGs aaaaaaaaa baccc a q0q0 q1q1 q3q3 q1q1 q7q7 q1q1 q2q2 q1q1 q7q7  11  01  0 00 input state stack A 11 A 03 0  A 03 → 0A 11  q0q0 q3q3 qfqf $$$$$$$$$$$ q1q1 1

31 From PDAs to CFGs aaaaaaaaa baccc a q3q3 q1q1 q7q7 q1q1 q2q2 q1q1 q7q7  11  01  0 0 input state stack A 03 A 13 A 13 → A 10 A 03 q0q0 q3q3 A 10 qfqf q0q0 q1q1 0 q1q1 1 $$$$$$$$$$$

32 From PDAs to CFGs qiqi qjqj a,  / t b, t /  q i’ q j’ A ij → aA i’j’ b qiqi qjqj qkqk A ik → A ij A jk qiqi A ii →  variables: A ij start variable: A0fA0f

33 Example start variable: A 03 productions: A 00 → A 00 A 00 A 00 → A 01 A 10 A 00 → A 03 A 30 A 01 → A 01 A 11 A 01 → A 02 A 21 A 00 → ... A 11 →  A 22 →  A 12 → 0A 12 1 A 12 → 0A 11 A 33 →  0,  / a ,  / $ , $ /  1, a /  q0q0 q2q2 q3q3 q1q1 , a /  A 03 → A 12


Download ppt "CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Pushdown."

Similar presentations


Ads by Google