regular expressionsdeepakd/atc-2013/regular-exp.pdf · regular expressions kleene’s theorem...

23
Regular Expressions Kleene’s Theorem Equation-based alternate construction Regular Expressions Deepak D’Souza Department of Computer Science and Automation Indian Institute of Science, Bangalore. 19 August 2013

Upload: others

Post on 18-Mar-2020

17 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Regular Expressions

Deepak D’Souza

Department of Computer Science and AutomationIndian Institute of Science, Bangalore.

19 August 2013

Page 2: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Outline

1 Regular Expressions

2 Kleene’s Theorem

3 Equation-based alternate construction

Page 3: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Examples of Regular Expressions

Expressions built from a, b, ε, using operators +, ·, and ∗.(a∗ + b∗) · c“Strings of only a’s or only b’s, followed by a c .”

(a + b)∗abb(a + b)∗

“contains abb as a subword.”

(a + b)∗b(a + b)(a + b)“3rd last letter is a b.”

(b∗ab∗a)∗b∗

“Even number of a’s.”

Ex. Give regexp for “Every 4-bit block of the formw [4i , 4i + 1, 4i + 2, 4i + 3] has even parity.”(0000 + 0011 + · · ·+ 1111)∗(ε + 0 + 1 + · · ·+ 111)

Page 4: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Examples of Regular Expressions

Expressions built from a, b, ε, using operators +, ·, and ∗.(a∗ + b∗) · c“Strings of only a’s or only b’s, followed by a c .”

(a + b)∗abb(a + b)∗

“contains abb as a subword.”

(a + b)∗b(a + b)(a + b)“3rd last letter is a b.”

(b∗ab∗a)∗b∗

“Even number of a’s.”

Ex. Give regexp for “Every 4-bit block of the formw [4i , 4i + 1, 4i + 2, 4i + 3] has even parity.”(0000 + 0011 + · · ·+ 1111)∗(ε + 0 + 1 + · · ·+ 111)

Page 5: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Examples of Regular Expressions

Expressions built from a, b, ε, using operators +, ·, and ∗.(a∗ + b∗) · c“Strings of only a’s or only b’s, followed by a c .”

(a + b)∗abb(a + b)∗

“contains abb as a subword.”

(a + b)∗b(a + b)(a + b)“3rd last letter is a b.”

(b∗ab∗a)∗b∗

“Even number of a’s.”

Ex. Give regexp for “Every 4-bit block of the formw [4i , 4i + 1, 4i + 2, 4i + 3] has even parity.”

(0000 + 0011 + · · ·+ 1111)∗(ε + 0 + 1 + · · ·+ 111)

Page 6: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Examples of Regular Expressions

Expressions built from a, b, ε, using operators +, ·, and ∗.(a∗ + b∗) · c“Strings of only a’s or only b’s, followed by a c .”

(a + b)∗abb(a + b)∗

“contains abb as a subword.”

(a + b)∗b(a + b)(a + b)“3rd last letter is a b.”

(b∗ab∗a)∗b∗

“Even number of a’s.”

Ex. Give regexp for “Every 4-bit block of the formw [4i , 4i + 1, 4i + 2, 4i + 3] has even parity.”(0000 + 0011 + · · ·+ 1111)∗(ε + 0 + 1 + · · ·+ 111)

Page 7: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Formal definitions

Syntax of regular expresions over an alphabet A:

r ::= ∅ | a | r + r | r · r | r∗

where a ∈ A.

Semantics: associate a language L(r) ⊆ A∗ with regexp r .

L(∅) = {}L(a) = {a}L(r + r ′) = L(r) ∪ L(r ′)L(r · r ′) = L(r) · L(r ′)L(r∗) = L(r)∗.

Question: Do we need ε in syntax?No. ε ≡ ∅∗.

Page 8: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Formal definitions

Syntax of regular expresions over an alphabet A:

r ::= ∅ | a | r + r | r · r | r∗

where a ∈ A.

Semantics: associate a language L(r) ⊆ A∗ with regexp r .

L(∅) = {}L(a) = {a}L(r + r ′) = L(r) ∪ L(r ′)L(r · r ′) = L(r) · L(r ′)L(r∗) = L(r)∗.

Question: Do we need ε in syntax?

No. ε ≡ ∅∗.

Page 9: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Formal definitions

Syntax of regular expresions over an alphabet A:

r ::= ∅ | a | r + r | r · r | r∗

where a ∈ A.

Semantics: associate a language L(r) ⊆ A∗ with regexp r .

L(∅) = {}L(a) = {a}L(r + r ′) = L(r) ∪ L(r ′)L(r · r ′) = L(r) · L(r ′)L(r∗) = L(r)∗.

Question: Do we need ε in syntax?No. ε ≡ ∅∗.

Page 10: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Example: Semantics of regexp

(a∗ + b∗) · c

+

{ε, a, b, aa, bb, . . .}

·

a b

{a} {b}

c

{c}

{ε, a, aa, . . .} {ε, b, bb, . . .}

{c, ac, bc, aac, bbc, . . .}

Page 11: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Example: Semantics of regexp

(a∗ + b∗) · c

+

{ε, a, b, aa, bb, . . .}

·

a b{a} {b} c {c}

{ε, a, aa, . . .} {ε, b, bb, . . .}

{c, ac, bc, aac, bbc, . . .}

Page 12: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Example: Semantics of regexp

(a∗ + b∗) · c

+

{ε, a, b, aa, bb, . . .}

·

a b{a} {b} c {c}

{ε, a, aa, . . .} {ε, b, bb, . . .}

{c, ac, bc, aac, bbc, . . .}

Page 13: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Example: Semantics of regexp

(a∗ + b∗) · c

+{ε, a, b, aa, bb, . . .}

·

a b{a} {b} c {c}

{ε, a, aa, . . .} {ε, b, bb, . . .}

{c, ac, bc, aac, bbc, . . .}

Page 14: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Example: Semantics of regexp

(a∗ + b∗) · c

+{ε, a, b, aa, bb, . . .}

·

a b{a} {b} c {c}

{ε, a, aa, . . .} {ε, b, bb, . . .}

{c, ac, bc, aac, bbc, . . .}

Page 15: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Kleene’s Theorem: RE = DFA

Class of languages defined by regular expressions coincides withregular languages.Proof

RE → DFA: Use closure properties of regular languages.

DFA → RE:

Page 16: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

DFA → RE: Kleene’s construction

Let A = (Q, s, δ, F ) be given DFA.

Define Lpq = {w ∈ A∗ | δ̂(p,w) = q}.Then L(A) =

⋃f ∈F Lsf .

For X ⊆ Q, define LXpq = {w ∈ A∗ | δ̂(p,w) =

q via a path that stays in X except for first and last states}X

p q

Then L(A) =⋃

f ∈F LQsf .

Page 17: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

DFA → RE: Kleene’s construction

p q

r

X

Advantage:

LX∪{r}pq = LX

pq + LXpr · (LX

rr )∗ · LX

rq.

Page 18: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

DFA → RE: Kleene’s construction (2)

Method:

Begin with LQsf for each f ∈ F .

Simplify by using terms with strictly smaller X ’s:

LX∪{r}pq = LX

pq + LXpr · (LX

rr )∗ · LX

rq.

For base terms, observe that

L{}pq =

{{a | δ(p, a) = q} if p 6= q{a | δ(p, a) = q} ∪ {ε} if p = q.

Exercise: convert NFA/DFA’s below to RE’s:

s

b

a, b

f

ba

b

a

e o

Page 19: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

DFA → RE: Kleene’s construction (2)

Method:

Begin with LQsf for each f ∈ F .

Simplify by using terms with strictly smaller X ’s:

LX∪{r}pq = LX

pq + LXpr · (LX

rr )∗ · LX

rq.

For base terms, observe that

L{}pq =

{{a | δ(p, a) = q} if p 6= q{a | δ(p, a) = q} ∪ {ε} if p = q.

Exercise: convert NFA/DFA’s below to RE’s:

s

b

a, b

f

ba

b

ae o

Page 20: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

DFA → RE using system of equations

Aim: to construct a regexp for

Lq = {w ∈ A∗ | δ̂(q,w) ∈ F}.

Note that L(A) = Ls .

Example:b

ab

a

e o

Set up equations to capture Lq’s:

xe = b · xe + a · xo

xo = a · xe + b · xo + ε.

Solution is a RE for each x , such that languages denoted byLHS and RHS RE’s coincide.

Page 21: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

DFA → RE using system of equations

Aim: to construct a regexp for

Lq = {w ∈ A∗ | δ̂(q,w) ∈ F}.

Note that L(A) = Ls .

Example:b

ab

ae o

Set up equations to capture Lq’s:

xe = b · xe + a · xo

xo = a · xe + b · xo + ε.

Solution is a RE for each x , such that languages denoted byLHS and RHS RE’s coincide.

Page 22: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Solutions to a system of equations

Lq’s are a solution to the system of equations

In general there could be many solutions to equations.

Consider x = A∗x (Here A is the alphabet). What are thesolutions to this equation?

In the case of equations arising out of automata, Lq’s can beseen to be the unique solution to the equations.

Page 23: Regular Expressionsdeepakd/atc-2013/regular-exp.pdf · Regular Expressions Kleene’s Theorem Equation-based alternate construction Examples of Regular Expressions Expressions built

Regular Expressions Kleene’s Theorem Equation-based alternate construction

Computing the least solution to a system of equations

Equations arising from our automaton can be viewed as:[xe

xo

]=

[b aa b

] [xe

xo

]+

[ε∅

]System of linear equations over regular expressions have thegeneral form:

X = AX + B

where X is a column vector of n variables, A is an nxn matrixof regular expressions, and B is a column vector of n regularexpressions.Claim: The column vector A∗B represents the least solution tothe equations above. [See Kozen, Supplementary Lecture A].Definition of A∗ when A is a 2x2 matrix:[

a bc d

]∗=

[(a + bd∗c)∗ (a + bd∗c)∗bd∗

(d + ca∗b)∗ca∗ (d + ca∗b)∗

]