regular expressions finite state automata
play

Regular Expressions / Finite State Automata UW CSE 490u Quiz - PowerPoint PPT Presentation

Regular Expressions / Finite State Automata UW CSE 490u Quiz Section, Feb 8, 2017 Sam Thomson Denotations of REs = = { } c = {c} for c = { x y | x , y


  1. Regular Expressions / Finite State Automata UW CSE 490u Quiz Section, Feb 8, 2017 Sam Thomson

  2. Denotations of REs • 〚∅〛 = ∅ • 〚 ε 〛 = { ε } • 〚 c 〛 = {c} for c ∈ Σ • 〚 α β 〛 = { x y | x ∈ 〚 α 〛 , y ∈ 〚 β 〛 } • 〚 α | β 〛 = 〚 α 〛∪〚 β 〛 • 〚 α * 〛 = { x 0 ... x n | x i ∈ 〚 α 〛 , n ≥ 0 }

  3. FSA Diagrams a,b ab b a,b a a b

  4. FSA Diagrams a,b a,b a|b a,b

  5. FSA Diagrams a a,b a*b b a,b

  6. Pumping Lemma

  7. Intuition (FSA) • A regular language L has an FSA F that accepts it. • F has p states (for some p ) • Any word w ∈ L with |w| > p will go through the same state twice (pigeonhole), creating a loop • That loop can be "pumped"

  8. Intuition (RE) • A regular language L has a RE that accepts it. • The only way to make an infinite RE is using a "*", e.g. αβ * γ | ω , with non-empty β • β can be pumped

  9. Formal Statement • Let L be a regular language. Then there exists p ≥ 1 such that every string w in L of length at least p ( p is called the "pumping length") can be written as w = xyz , satisfying the following conditions: • |y| ≥ 1 • |xy| ≤ p • for all i ≥ 0, xy i z ∈ L (wikipedia)

  10. Not the only way • Closure properties • Myhill–Nerode theorem

  11. The pumping lemma is necessary, not sufficient • There are non-regular languages that are "pumpable" • Usually combination of pumping lemma and closure properties is enough

  12. Example • L = { (ab) i | i ≥ 2 } • L is regular • L = 〚 abab(ab)* 〛

  13. Example • L = { a i b i | i ≥ 0 } • L is not regular • Given p , let w = a p b p • Since |xy| ≤ p, y is only 'a's. Pumping y will lead to more 'a's than 'b's.

  14. Example • L = { a 2i | i ≥ 0 } • L is regular • L = 〚 (aa)* 〛

  15. Example • L = { a 2 i | i ≥ 0 } • L is not regular • Pumping any y = a j will work at most once, because the gaps between 2 i double each time.

  16. Example • L = { ww | w ∈ {a,b}* } • L is not regular • Given p , let w = a p b a p b • Since |xy| ≤ p, y is only 'a's. Pumping y will lead to more 'a's in the first half than the second half.

  17. Example • L = { matching "parentheses" } ⊆ {a,b}* • L is not regular • Intersect L with 〚 a*b* 〛 and you get { a i b i | i ≥ 0 }. • We already showed that's not regular

  18. Exercise • Write CFGs for the previous examples • For example: • L = { a 2i | i ≥ 0 } • Rules: • S → aaS • S → ε

  19. Write CFGs for: • L = { a i b i | i ≥ 0 } • L = { (ab) i | i ≥ 2 } • L = { a 2 i | i ≥ 0 } • L = { ww | w ∈ {a,b}* } • L = { matching "parentheses" } ⊆ {a,b}*

  20. Converting FSAs to REs (bonus, not on final)

  21. Converting FSAs to REs • The Floyd-Warshall/Roy/Kleene/Gauss-Jordan/McNaughton/ Yamada dynamic programming algorithm computes: • transitive closures • shortest paths • highest probability paths • total probabilities of all paths • the regular expression for a finite automaton • solution to linear equations

  22. Kleene's Algorithm • Recurrence: • C (-1) is adjacency matrix • (paths from i to j that don't pass through any other states in between) • C ( k ) i,j = C ( k-1 ) i,j | (C ( k-1 ) i,k (C ( k-1 ) k , k )* C ( k-1 ) k , j ) • (paths from i to j that possibly pass through 0,...,k) • Answer is C n,start,final

  23. Kleene's Algorithm a,b 2 b a,b a 0 1 3 a b

  24. Example a,b Paths from i to j without passing through 2 b a,b any other nodes in between: a C (-1) 0 1 2 3 0 1 3 a ∅ ∅ b 0 a b ∅ ∅ 1 a b ∅ ∅ ∅ 2 a|b 3 ∅ ∅ a|b ∅

  25. Example a,b Paths from i to j that 2 possibly pass through 0 b a,b a C (-1) 0 1 2 3 0 1 3 a ∅ ∅ b 0 a b ∅ ∅ 1 a b C (0) 0 1 2 3 ∅ ∅ ∅ ∅ ∅ 2 a|b 0 a b 3 ∅ ∅ a|b ∅ ∅ ∅ 1 a b ∅ ∅ ∅ 2 a|b 3 ∅ ∅ a|b ∅

  26. Example a,b Paths from i to j that 2 possibly pass through 0,1 b a,b a C (0) 0 1 2 3 0 1 3 a ∅ ∅ b 0 a b ∅ ∅ 1 a b C (1) 0 1 2 3 ∅ ∅ ∅ ∅ 2 a|b b|aa ab 0 a 3 ∅ ∅ a|b ∅ ∅ ∅ 1 a b ∅ ∅ ∅ 2 a|b 3 ∅ ∅ a|b ∅

  27. Example a,b Paths from i to j that 2 possibly pass through 0,1,2 b a,b a C (1) 0 1 2 3 0 1 3 a ∅ b|aa ab b 0 a ∅ ∅ 1 a b C (2) 0 1 2 3 (b|aa) ∅ ∅ ∅ ∅ 2 a|b 0 a ab (a|b)* 3 ∅ ∅ a|b ∅ ∅ ∅ 1 b a(a|b)* ∅ ∅ ∅ 2 (a|b)* (a|b) 3 ∅ ∅ ∅ (a|b)*

  28. Example a,b Paths from i to j that 2 possibly pass through 0,1,2,3 b a,b a C (2) 0 1 2 3 0 1 3 a (b|aa) ∅ b 0 a ab (a|b)* ∅ ∅ 1 b C (3) 0 1 2 3 a(a|b)* ((b|aa) ∅ ∅ ∅ ∅ 2 0 a ab (a|b)* (a|b)*)| (ab (a|b) (a|b)*) (a(a|b)*)| (a|b) 3 ∅ ∅ ∅ ∅ ∅ 1 b (b (a|b) (a|b)* (a|b)*) ∅ ∅ ∅ 2 (a|b)* (a|b) 3 ∅ ∅ ∅ (a|b)*

  29. Example a,b Paths from i to j that 2 possibly pass through 0,1,2,3 b a,b a C (2) 0 1 2 3 0 1 3 a (b|aa) ∅ b 0 a ab (a|b)* ∅ ∅ 1 b C (3) 0 1 2 3 a(a|b)* ((b|aa) ∅ ∅ ∅ ∅ 2 0 a ab (a|b)* (a|b)*)| (ab (a|b) (a|b)*) (a(a|b)*)| (a|b) 3 ∅ ∅ ∅ ∅ ∅ 1 b (b (a|b) (a|b)* (a|b)*) ∅ ∅ ∅ 2 (a|b)* (a|b) 3 ∅ ∅ ∅ (a|b)*

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend