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

regular expressions finite state automata
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Regular Expressions / Finite State Automata

UW CSE 490u Quiz Section, Feb 8, 2017 Sam Thomson

slide-2
SLIDE 2

Denotations of REs

  • 〚∅〛= ∅
  • 〚ε〛= {ε}
  • 〚 c 〛= {c} for c ∈ Σ
  • 〚α β 〛= { x y | x ∈〚 α 〛, y ∈〚 β 〛}
  • 〚α | β 〛=〚 α 〛∪〚 β 〛
  • 〚α* 〛= { x0 ... xn | xi ∈〚 α 〛, n ≥ 0 }
slide-3
SLIDE 3

FSA Diagrams

ab

a b b a,b a a,b

slide-4
SLIDE 4

FSA Diagrams

a|b

a,b a,b a,b

slide-5
SLIDE 5

FSA Diagrams

a*b

b a,b a,b a

slide-6
SLIDE 6

Pumping Lemma

slide-7
SLIDE 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"
slide-8
SLIDE 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
slide-9
SLIDE 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, xyiz ∈ L

(wikipedia)

slide-10
SLIDE 10

Not the only way

  • Closure properties
  • Myhill–Nerode theorem
slide-11
SLIDE 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

slide-12
SLIDE 12

Example

  • L = { (ab)i | i ≥ 2 }
  • L is regular
  • L =〚 abab(ab)* 〛
slide-13
SLIDE 13

Example

  • L = { ai bi | i ≥ 0 }
  • L is not regular
  • Given p, let w = ap bp
  • Since |xy| ≤ p, y is only 'a's. Pumping y will lead to

more 'a's than 'b's.

slide-14
SLIDE 14

Example

  • L = { a2i | i ≥ 0 }
  • L is regular
  • L =〚 (aa)* 〛
slide-15
SLIDE 15

Example

  • L = { a2i | i ≥ 0 }
  • L is not regular
  • Pumping any y = aj will work at most once,

because the gaps between 2i double each time.

slide-16
SLIDE 16

Example

  • L = { ww | w ∈ {a,b}* }
  • L is not regular
  • Given p, let w = ap b ap 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.

slide-17
SLIDE 17

Example

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

Exercise

  • Write CFGs for the previous examples
  • For example:
  • L = { a2i | i ≥ 0 }
  • Rules:
  • S → aaS
  • S → ε
slide-19
SLIDE 19

Write CFGs for:

  • L = { ai bi | i ≥ 0 }
  • L = { (ab)i | i ≥ 2 }
  • L = { a2i | i ≥ 0 }
  • L = { ww | w ∈ {a,b}* }
  • L = { matching "parentheses" } ⊆ {a,b}*
slide-20
SLIDE 20

Converting FSAs to REs

(bonus, not on final)

slide-21
SLIDE 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
slide-22
SLIDE 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 Cn,start,final
slide-23
SLIDE 23

Kleene's Algorithm

a b b a,b a a,b

3 2 1

slide-24
SLIDE 24

Example

C(-1) 1 2 3 ∅ a b ∅ 1 ∅ ∅ a b 2 ∅ ∅ a|b ∅ 3 ∅ ∅ a|b ∅

a b b a,b a a,b

3 2 1

Paths from i to j without passing through any other nodes in between:

slide-25
SLIDE 25

Example

C(-1) 1 2 3 ∅ a b ∅ 1 ∅ ∅ a b 2 ∅ ∅ a|b ∅ 3 ∅ ∅ a|b ∅

a b b a,b a a,b

3 2 1

Paths from i to j that possibly pass through 0

C(0) 1 2 3 ∅ a b ∅ 1 ∅ ∅ a b 2 ∅ ∅ a|b ∅ 3 ∅ ∅ a|b ∅

slide-26
SLIDE 26

Example

C(0) 1 2 3 ∅ a b ∅ 1 ∅ ∅ a b 2 ∅ ∅ a|b ∅ 3 ∅ ∅ a|b ∅

a b b a,b a a,b

3 2 1

Paths from i to j that possibly pass through 0,1

C(1) 1 2 3 ∅ a

b|aa ab

1 ∅ ∅ a b 2 ∅ ∅ a|b ∅ 3 ∅ ∅ a|b ∅

slide-27
SLIDE 27

C(1) 1 2 3 ∅ a

b|aa ab

1 ∅ ∅ a b 2 ∅ ∅ a|b ∅ 3 ∅ ∅ a|b ∅

Example

a b b a,b a a,b

3 2 1

Paths from i to j that possibly pass through 0,1,2

C(2) 1 2 3 ∅ a

(b|aa) (a|b)*

ab 1 ∅ ∅

a(a|b)*

b 2 ∅ ∅

(a|b)*

∅ 3 ∅ ∅

(a|b) (a|b)*

slide-28
SLIDE 28

C(2) 1 2 3 ∅ a

(b|aa) (a|b)*

ab 1 ∅ ∅

a(a|b)*

b 2 ∅ ∅

(a|b)*

∅ 3 ∅ ∅

(a|b) (a|b)*

Example

a b b a,b a a,b

3 2 1

Paths from i to j that possibly pass through 0,1,2,3

C(3) 1 2 3 ∅ a

((b|aa) (a|b)*)| (ab (a|b) (a|b)*)

ab 1 ∅ ∅

(a(a|b)*)| (b (a|b) (a|b)*)

b 2 ∅ ∅

(a|b)*

∅ 3 ∅ ∅

(a|b) (a|b)*

slide-29
SLIDE 29

C(2) 1 2 3 ∅ a

(b|aa) (a|b)*

ab 1 ∅ ∅

a(a|b)*

b 2 ∅ ∅

(a|b)*

∅ 3 ∅ ∅

(a|b) (a|b)*

Example

a b b a,b a a,b

3 2 1

Paths from i to j that possibly pass through 0,1,2,3

C(3) 1 2 3 ∅ a

((b|aa) (a|b)*)| (ab (a|b) (a|b)*)

ab 1 ∅ ∅

(a(a|b)*)| (b (a|b) (a|b)*)

b 2 ∅ ∅

(a|b)*

∅ 3 ∅ ∅

(a|b) (a|b)*