1 / 33

CSC 3130: Automata theory and formal languages

Fall 2008. The Chinese University of Hong Kong. CSC 3130: Automata theory and formal languages. Pushdown automata. Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130. Motivation. We had two ways to describe regular languages How about context-free languages?. regular expression.

alaina
Download Presentation

CSC 3130: Automata theory and formal languages

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

  2. Motivation • We had two ways to describe regular languages • How about context-free languages? regularexpression NFA DFA syntactic computational CFG pushdown automaton syntactic computational

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

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

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

  6. Rules for pushdown automata • Multiple and e-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 state control L = {0n#1n: n ≥ 0} push $ input read 0 0 0 0 # 1 1 1 push a read # … read 1 $ a a a pop a stack pop $

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

  9. Formal definition A pushdown automaton is (Q, ,, , q0, F): • Q is a finite set of states; •  is the input alphabet; •  is the stack alphabet • q0in 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, #} q0 G = {$, a} e, e / $ q1 0, e / a d(q1, 0, e) = {(q1, a)} #, e / e d(q1, 1, e) = ∅ q2 1, a / e d(q1, #, e) = {(q1, e)} d(q1, 0, a) = ∅ e, $ / e ... q3 d: Q  (  {})  (  {}) → subsets of Q  (G  {})

  11. Example 1 • L = {wwR: w ∈ S*} • S = {0, 1} e, 00, 0110 ∈L 1, 011, 010 ∉L e, e / $ e, e / e e, $/ e q1 q3 q2 q0 0, e / 0 0, 0 / e 1, e / 1 1, 1 / e

  12. Example 2 • L = {w: w = wR, w ∈ S*} • S = {0, 1} e, 1, 00, 010, 0110 ∈L 0110110110 or 011010110 011 ∉L x xR x xR e, e / e 0, e / e 1, e / e e, e / $ e, $/ e q1 q3 q2 q0 0, e / 0 0, 0 / e 1, e / 1 1, 1 / e

  13. Example 3 L = {0n1m0m1n | n 0, m 0} • S = {0, 1} 0, e / 0 1, e / 1 e, e / e e, e / $ q1 q2 q0 e, e / e q4 q3 q5 e, e / e e, $/ e 0, 1 / e 1, 0/ e

  14. Example 4 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 / $ e, $/ e q1 q3 q0 1, e / 1 0, e / 0 0, 1 / e 1, 0 / e

  15. Example 4 L = {w: w has same number 0s and 1s} • S = {0, 1} e, e / $ e, $/ e q1 q3 q0 read stack w = 001110 1, e / 1 0, e / 0 0 $0 0 $00 0, 1 / e 1, 0 / e 1 $0 1 $ 1 $1 0 $

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

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

  18. Example 5 L = {w: whas two 0-blocks with same number of 0s 0, e / e 1, e / e 0, e / 0 0, e / 0 e, e / $ 1, e / e q1 q0 q2 q3 1, e / e e, e / $ 0, e / e 0, e / e 1, e / e q4 1, e / e 1, e / e 1, e / e 1, $/ e q5 q7 q6 e, $/ e 0, 0/ e

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

  20. A convention • Sometimes we denote “transitions” by: • This will mean: q0 q1 a, b/ c1c2c3 q0 q1 a, b/ c1 e, e/ c2 e, e/ c3 intermediatestates • pop b, then push c1, c2, and c3

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

  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 → a and substitute A with a • If the top of the stack is a terminal a:Read next input symbol and compare to aIf they don’t match, reject (die) • If top of stack is $, go to accept state

  24. Description of PDA for CFGs a, a / e e, A / ak...a1 for every terminal a for every production A → a1...ak e, e / $S e, $ / e q2 q1 q0

  25. From PDAs to CFGs • First, we simplify the PDA: • It has a single accept stateqf • It empties its stack before accepting • Each transition is either a push, or a pop, but not both ✓ context-free grammar pushdown automaton

  26. Simplifying the PDA 0, e / a 1, b / e • First, we simplify the PDA: • It has a single accept stateqf • It empties its stack before accepting • Each transition is either a push, or a pop, but not both e, e / $ q0 q1 q2 1, a / b e, e / e e, e / e qf e, $ / e q3 e, a / e e, b / e

  27. Simplifying the PDA 0, e / a 1, b / e • First, we simplify the PDA: • It has a single accept stateqf • It empties its stack before accepting • Each transition is either a push, or a pop, but not both e, e / $ q0 q1 q2 1, a / b e, e / e e, e / e q3 qf e, $ / e e, a / e e, b / e

  28. Simplifying the PDA 0, e / a 1, b / e • First, we simplify the PDA: • It has a single accept stateqf • It empties its stack before accepting • Each transition is either a push, or a pop, but not both e, e / $ 1, a / e e, e / b q0 q12 q1 q2 e, e / x e, e/ x q23 q13 e, x / e e, x / e q3 e, $ / e e, a / e qf e, b / e

  29. From PDAs to CFGs • We look at the stack in an accepting computation: input 0 1 0 e 1 1 e 0 1 e e 0 q0 q1 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf state a b a c c c a a a a a a a a a stack $ $ $ $ $ $ $ $ $ $ $ portions that preserve the stack A03 = {x: x leads from q0 to q3 and preserves stack}

  30. From PDAs to CFGs input 0 1 0 e 1 1 e 0 1 e e 0 q0 q1 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf state a b a c c c a a a a a a a a a stack $ $ $ $ $ $ $ $ $ $ $ A11 0 e A03 → 0A11e A03

  31. From PDAs to CFGs input 0 1 0 e 1 1 e 0 1 e e 0 q0 q1 q1 q3 q1 q7 q0 q1 q2 q1 q3 q7 qf state a b a c c c a a a a a a a a a stack $ $ $ $ $ $ $ $ $ $ $ A10 A03 A13 A13 → A10A03

  32. From PDAs to CFGs Aij variables: qi’ a, e / t A0f start variable: qi qj b, t /e Aij → aAi’j’b qj’ Aik → AijAjk qi qj qk Aii → e qi

  33. Example 0, e / a 1, a / e q0 q2 q3 q1 e, e / $ e, $/ e e, a /e start variable: A03 productions: A00 → e A12 → 0A121 A03 → A12 A00 → A00A00 A11 → e A12 → 0A11 A00 → A01A10 A22 → e A00 → A03A30 A33 → e A01 → A01A11 A01 → A02A21 ...

More Related