FORMAL LANGUAGES,AUTOMATA AND COMPUTATION

REGULAR EXPRESSIONS

Carnegie Mellon University in Qatar

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 1 / 26 Guess and verify Nondeterministic FA Multiple transitions from a state with the same input symbol -transitions NFAs are equivalent to DFAs Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

SUMMARY

Nondeterminism Clone the FA at choice points

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 Nondeterministic FA Multiple transitions from a state with the same input symbol -transitions NFAs are equivalent to DFAs Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

SUMMARY

Nondeterminism Clone the FA at choice points Guess and verify

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 Multiple transitions from a state with the same input symbol -transitions NFAs are equivalent to DFAs Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

SUMMARY

Nondeterminism Clone the FA at choice points Guess and verify Nondeterministic FA

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 -transitions NFAs are equivalent to DFAs Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

SUMMARY

Nondeterminism Clone the FA at choice points Guess and verify Nondeterministic FA Multiple transitions from a state with the same input symbol

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 NFAs are equivalent to DFAs Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

SUMMARY

Nondeterminism Clone the FA at choice points Guess and verify Nondeterministic FA Multiple transitions from a state with the same input symbol -transitions

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

SUMMARY

Nondeterminism Clone the FA at choice points Guess and verify Nondeterministic FA Multiple transitions from a state with the same input symbol -transitions NFAs are equivalent to DFAs

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 SUMMARY

Nondeterminism Clone the FA at choice points Guess and verify Nondeterministic FA Multiple transitions from a state with the same input symbol -transitions NFAs are equivalent to DFAs Determinization procedure builds a DFA with up to 2k states for an NFA with k states.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 2 / 26 PROOF IDEABASEDON NFAS

CLOSURE THEOREMS

THEOREM The class of regular languages is closed under the union operation.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 3 / 26 CLOSURE THEOREMS

THEOREM PROOF IDEABASEDON NFAS The class of regular languages is closed under the union operation.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 3 / 26 PROOF IDEABASEDON NFAS

CLOSURE THEOREMS

THEOREM The class of regular languages is closed under the concatenation operation.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 4 / 26 CLOSURE THEOREMS

THEOREM PROOF IDEABASEDON NFAS The class of regular languages is closed under the concatenation operation.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 4 / 26 PROOF IDEABASEDON NFAS

CLOSURE THEOREMS

THEOREM The class of regular languages is closed under the star operation.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 5 / 26 CLOSURE THEOREMS

THEOREM PROOF IDEABASEDON NFAS The class of regular languages is closed under the star operation.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 5 / 26 Finite number of symbols, states, transitions Regular Expressions provide an algebraic expression framework to describe the same class of strings Thus, DFAs and Regular Expressions are equivalent.

REGULAR EXPRESSIONS

DFAs are finite descriptions of (finite or infinite) sets of strings

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 6 / 26 Thus, DFAs and Regular Expressions are equivalent.

Regular Expressions provide an algebraic expression framework to describe the same class of strings

REGULAR EXPRESSIONS

DFAs are finite descriptions of (finite or infinite) sets of strings Finite number of symbols, states, transitions

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 6 / 26 Thus, DFAs and Regular Expressions are equivalent.

REGULAR EXPRESSIONS

DFAs are finite descriptions of (finite or infinite) sets of strings Finite number of symbols, states, transitions Regular Expressions provide an algebraic expression framework to describe the same class of strings

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 6 / 26 REGULAR EXPRESSIONS

DFAs are finite descriptions of (finite or infinite) sets of strings Finite number of symbols, states, transitions Regular Expressions provide an algebraic expression framework to describe the same class of strings Thus, DFAs and Regular Expressions are equivalent.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 6 / 26 φ {} a for a ∈ Σ {a}  {} (R1 ∪ R2) (R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Set

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 φ {} a for a ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

Regular Expression Regular Set

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 φ {} a for a ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 a for a ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set φ {}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set φ {} a for a ∈ Σ {a}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set φ {} a for a ∈ Σ {a}  {}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 (R1R2) L(R1)L(R2) (R∗) L(R)∗

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set φ {} a for a ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2)

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 (R∗) L(R)∗

REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set φ {} a for a ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2)

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 REGULAR EXPRESSIONS

For every regular expression, there is a corresponding regular set or language

R, R1, R2 are regular expressions; L(R) denotes the corresponding regular set Regular Expression Regular Set φ {} a for a ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1R2) L(R1)L(R2) (R∗) L(R)∗

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 7 / 26 In (...), the parenthesis can be deleted In which case, interpretation is done in the precedence order: star, concatenation and then union. R+ = RR∗ and Rk for k-fold concatenation are useful shorthands.

REGULAR EXPRESSIONS–MORE SYNTAX Regular Expression Regular Set φ {} a for ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1 ◦ R2) L(R1) ◦ L(R2) (R∗) L(R)∗

Also some books use R1 + R2 to denote union.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 8 / 26 In which case, interpretation is done in the precedence order: star, concatenation and then union. R+ = RR∗ and Rk for k-fold concatenation are useful shorthands.

REGULAR EXPRESSIONS–MORE SYNTAX Regular Expression Regular Set φ {} a for ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1 ◦ R2) L(R1) ◦ L(R2) (R∗) L(R)∗

Also some books use R1 + R2 to denote union. In (...), the parenthesis can be deleted

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 8 / 26 R+ = RR∗ and Rk for k-fold concatenation are useful shorthands.

REGULAR EXPRESSIONS–MORE SYNTAX Regular Expression Regular Set φ {} a for ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1 ◦ R2) L(R1) ◦ L(R2) (R∗) L(R)∗

Also some books use R1 + R2 to denote union. In (...), the parenthesis can be deleted In which case, interpretation is done in the precedence order: star, concatenation and then union.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 8 / 26 REGULAR EXPRESSIONS–MORE SYNTAX Regular Expression Regular Set φ {} a for ∈ Σ {a}  {} (R1 ∪ R2) L(R1) ∪ L(R2) (R1 ◦ R2) L(R1) ◦ L(R2) (R∗) L(R)∗

Also some books use R1 + R2 to denote union. In (...), the parenthesis can be deleted In which case, interpretation is done in the precedence order: star, concatenation and then union. R+ = RR∗ and Rk for k-fold concatenation are useful shorthands.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 8 / 26 {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol} ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression 0∗10∗ →

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol} ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol} ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ →

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol} ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 {ω|ω starts and ends with the same symbol} ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 →

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 {ω|n1(ω) is even}

REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol} (0∗10∗1)∗0∗ →

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 REGULAR EXPRESSION EXAMPLES

Regular Expression Regular Language 0∗10∗ → {ω|ω contains a single 1} (0 ∪ 1)∗1(0 ∪ 1)∗ → {ω|ω has at least one 1} 0(0 ∪ 1)∗0 ∪ 1(0 ∪ 1)∗1 ∪ 0 ∪ 1 → {ω|ω starts and ends with the same symbol} ∗ ∗ ∗ ∗ (0 10 1) 0 → {ω|n1(ω) is even}

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 9 / 26 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1)

(1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ )

(0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

(0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1

All strings with no pair of consecutive 0s

All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1)

(1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ )

(0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

All strings such that fourth symbol from the end is a 1

All strings with no pair of consecutive 0s

All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ )

(0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

(0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1) All strings with no pair of consecutive 0s

All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ )

(0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

All strings with no pair of consecutive 0s

All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1)

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

(1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ ) All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1) All strings with no pair of consecutive 0s

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ ) All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1) All strings with no pair of consecutive 0s (1∗011∗)∗(0 ∪ ) ∪ 1∗

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

All strings that do not end in 01.

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1) All strings with no pair of consecutive 0s (1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ )

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1) All strings with no pair of consecutive 0s (1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ ) All strings that do not end in 01.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 WRITING REGULAR EXPRESSIONS

All strings with at least one pair of consecutive 0s (0 ∪ 1)∗00(0 ∪ 1)∗ All strings such that fourth symbol from the end is a 1 (0 ∪ 1)∗1(0 ∪ 1)(0 ∪ 1)(0 ∪ 1) All strings with no pair of consecutive 0s (1∗011∗)∗(0 ∪ ) ∪ 1∗ Strings consist of repetitions of 1 or 01 or two boundary cases: (1 ∪ 01)∗(0 ∪ ) All strings that do not end in 01. (0 ∪ 1)∗(00 ∪ 10 ∪ 11) ∪ 0 ∪ 1 ∪ 

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 10 / 26 (a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗

WRITING REGULAR EXPRESSIONS

All strings over Σ = {a, b, c} that contain every symbol at least once.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 11 / 26 WRITING REGULAR EXPRESSIONS

All strings over Σ = {a, b, c} that contain every symbol at least once. (a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗∪ (a ∪ b ∪ c)∗c(a ∪ b ∪ c)∗b(a ∪ b ∪ c)∗a(a ∪ b ∪ c)∗

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 11 / 26 DFAs and REs may need different ways of looking at the problem. For the DFA, you count symbols For the RE, you enumerate all possible patterns

WRITING REGULAR EXPRESSIONS

All strings over Σ = {a, b, c} that contain every symbol at least once.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 12 / 26 WRITING REGULAR EXPRESSIONS

All strings over Σ = {a, b, c} that contain every symbol at least once.

DFAs and REs may need different ways of looking at the problem. For the DFA, you count symbols For the RE, you enumerate all possible patterns

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 12 / 26 R = R = R φ∗ =  Note that we do not have explicit operators for intersection or complementation!

REIDENTITIES

R ∪ φ = R

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 13 / 26 φ∗ =  Note that we do not have explicit operators for intersection or complementation!

REIDENTITIES

R ∪ φ = R R = R = R

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 13 / 26 Note that we do not have explicit operators for intersection or complementation!

REIDENTITIES

R ∪ φ = R R = R = R φ∗ = 

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 13 / 26 REIDENTITIES

R ∪ φ = R R = R = R φ∗ =  Note that we do not have explicit operators for intersection or complementation!

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 13 / 26 Mostly some syntactic extensions/changes to basic regular expressions with some additional functionality for remembering matches

Substring matches in a string!

Search for and download Regex Coach to learn and experiment with regular expression matching

DIGRESSION:RESIN REALLIFE

Linux/Unix Shell, Perl, Awk, Python all have built in regular expression support for pattern matching functionality

See http://www.wdvl.com/Authoring/ Languages/Perl/PerlfortheWeb/ perlintro2 table1.html

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 14 / 26 Substring matches in a string!

Search for and download Regex Coach to learn and experiment with regular expression matching

DIGRESSION:RESIN REALLIFE

Linux/Unix Shell, Perl, Awk, Python all have built in regular expression support for pattern matching functionality

See http://www.wdvl.com/Authoring/ Languages/Perl/PerlfortheWeb/ perlintro2 table1.html

Mostly some syntactic extensions/changes to basic regular expressions with some additional functionality for remembering matches

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 14 / 26 DIGRESSION:RESIN REALLIFE

Linux/Unix Shell, Perl, Awk, Python all have built in regular expression support for pattern matching functionality

See http://www.wdvl.com/Authoring/ Languages/Perl/PerlfortheWeb/ perlintro2 table1.html

Mostly some syntactic extensions/changes to basic regular expressions with some additional functionality for remembering matches

Substring matches in a string!

Search for and download Regex Coach to learn and experiment with regular expression matching

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 14 / 26 LEMMA-THE if PART If a language is described by a regular expression, then it is regular

PROOF IDEA Inductively convert a given regular expression to an NFA.

EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 15 / 26 PROOF IDEA Inductively convert a given regular expression to an NFA.

EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

LEMMA-THE if PART If a language is described by a regular expression, then it is regular

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 15 / 26 EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

LEMMA-THE if PART If a language is described by a regular expression, then it is regular

PROOF IDEA Inductively convert a given regular expression to an NFA.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 15 / 26  a for a ∈ Σ

CONVERTING RESTO NFAS:BASIS CASES

Regular Expression Corresponding NFA

φ

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 16 / 26 a for a ∈ Σ

CONVERTING RESTO NFAS:BASIS CASES

Regular Expression Corresponding NFA

φ 

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 16 / 26 CONVERTING RESTO NFAS:BASIS CASES

Regular Expression Corresponding NFA

φ 

a for a ∈ Σ

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 16 / 26 CONVERTING RESTO NFAS

Union Let N1 and N2 be NFAs for R1 and R2 respectively. Then the NFA for R1 ∪ R2 is

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 17 / 26 CONVERTING RESTO NFAS

Concatenation

Let N1 and N2 be NFAs for R1 and R2 respectively. Then the NFA for R1R2 is

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 18 / 26 CONVERTING RESTO NFAS:STAR

Star Let N be NFAs for R. Then the NFA for R∗ is

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 19 / 26 RE TO NFA CONVERSION EXAMPLE

Let’s convert (a ∪ b)∗aba to an NFA.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 20 / 26 NFA → DFA via determinization

RE TO NFA TO DFA

Regular Expression → NFA (possibly with -transitions)

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 21 / 26 RE TO NFA TO DFA

Regular Expression → NFA (possibly with -transitions) NFA → DFA via determinization

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 21 / 26 LEMMA –THE only if PART If a language is regular then it is described by a regular expression

PROOF IDEA Generalized transitions: label transitions with regular expressions

Generalized NFAs (GNFA)

Iteratively eliminate states of the GNFA one by one, until only two states and a single generalized transition is left.

EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 22 / 26 PROOF IDEA Generalized transitions: label transitions with regular expressions

Generalized NFAs (GNFA)

Iteratively eliminate states of the GNFA one by one, until only two states and a single generalized transition is left.

EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

LEMMA –THE only if PART If a language is regular then it is described by a regular expression

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 22 / 26 Generalized NFAs (GNFA)

Iteratively eliminate states of the GNFA one by one, until only two states and a single generalized transition is left.

EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

LEMMA –THE only if PART If a language is regular then it is described by a regular expression

PROOF IDEA Generalized transitions: label transitions with regular expressions

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 22 / 26 Iteratively eliminate states of the GNFA one by one, until only two states and a single generalized transition is left.

EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

LEMMA –THE only if PART If a language is regular then it is described by a regular expression

PROOF IDEA Generalized transitions: label transitions with regular expressions

Generalized NFAs (GNFA)

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 22 / 26 EQUIVALENCE WITH FINITE AUTOMATA

THEOREM A language is regular if and only if some regular expression describes it.

LEMMA –THE only if PART If a language is regular then it is described by a regular expression

PROOF IDEA Generalized transitions: label transitions with regular expressions

Generalized NFAs (GNFA)

Iteratively eliminate states of the GNFA one by one, until only two states and a single generalized transition is left.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 22 / 26 Now consider

If you are in state p and a prefix of the remaining input matches the regular expression ab∗ ∪ bc∗ then go to state q

GENERALIZED TRANSITIONS

DFAs have single symbols as transition labels

If you are in state p and the next input symbol matches a, go to state q

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 23 / 26 If you are in state p and a prefix of the remaining input matches the regular expression ab∗ ∪ bc∗ then go to state q

GENERALIZED TRANSITIONS

DFAs have single symbols as transition labels

If you are in state p and the next input symbol matches a, go to state q Now consider

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 23 / 26 GENERALIZED TRANSITIONS

DFAs have single symbols as transition labels

If you are in state p and the next input symbol matches a, go to state q Now consider

If you are in state p and a prefix of the remaining input matches the regular expression ab∗ ∪ bc∗ then go to state q

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 23 / 26 In fact, all standard DFA transitions are generalized transitions with regular expressions of a single symbol!

A Generalized NFA is an NFA with generalized transitions.

GENERALIZED TRANSITIONSAND NFA

A generalized transition is a transition whose label is a regular expression

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 24 / 26 In fact, all standard DFA transitions are generalized transitions with regular expressions of a single symbol!

A Generalized NFA is an NFA with generalized transitions.

GENERALIZED TRANSITIONSAND NFA

A generalized transition is a transition whose label is a regular expression

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 24 / 26 In fact, all standard DFA transitions are generalized transitions with regular expressions of a single symbol!

GENERALIZED TRANSITIONSAND NFA

A generalized transition is a transition whose label is a regular expression

A Generalized NFA is an NFA with generalized transitions.

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 24 / 26 GENERALIZED TRANSITIONSAND NFA

A generalized transition is a transition whose label is a regular expression

A Generalized NFA is an NFA with generalized transitions.

In fact, all standard DFA transitions are generalized transitions with regular expressions of a single symbol!

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 24 / 26 ∗ ∗ (0 ∪ 10 1) takes the machine from q1 back to q1 So ?= 0∗1(0 ∪ 10∗1)∗ represents all strings that take the DFA from state q0 to q1

∗ 0 1 takes the DFA from state q0 to q1

GENERALIZED TRANSITIONS

Consider the 2-state DFA

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 25 / 26 So ?= 0∗1(0 ∪ 10∗1)∗ represents all strings that take the DFA from state q0 to q1

∗ 0 1 takes the DFA from state q0 to q1 ∗ ∗ (0 ∪ 10 1) takes the machine from q1 back to q1

GENERALIZED TRANSITIONS

Consider the 2-state DFA

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 25 / 26 ∗ ∗ (0 ∪ 10 1) takes the machine from q1 back to q1 So ?= 0∗1(0 ∪ 10∗1)∗ represents all strings that take the DFA from state q0 to q1

GENERALIZED TRANSITIONS

Consider the 2-state DFA

∗ 0 1 takes the DFA from state q0 to q1

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 25 / 26 So ?= 0∗1(0 ∪ 10∗1)∗ represents all strings that take the DFA from state q0 to q1

GENERALIZED TRANSITIONS

Consider the 2-state DFA

∗ 0 1 takes the DFA from state q0 to q1 ∗ ∗ (0 ∪ 10 1) takes the machine from q1 back to q1

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 25 / 26 GENERALIZED TRANSITIONS

Consider the 2-state DFA

∗ 0 1 takes the DFA from state q0 to q1 ∗ ∗ (0 ∪ 10 1) takes the machine from q1 back to q1 So ?= 0∗1(0 ∪ 10∗1)∗ represents all strings that take the DFA from state q0 to q1

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 25 / 26 with one generalized transition then we can “read” the regular expression from the label of the generalized transition (as in the example above)

GENERALIZED NFAS

Take any NFA and transform it into a GNFA with only two states: one start and one accept

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 26 / 26 then we can “read” the regular expression from the label of the generalized transition (as in the example above)

GENERALIZED NFAS

Take any NFA and transform it into a GNFA with only two states: one start and one accept with one generalized transition

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 26 / 26 GENERALIZED NFAS

Take any NFA and transform it into a GNFA with only two states: one start and one accept with one generalized transition then we can “read” the regular expression from the label of the generalized transition (as in the example above)

(CARNEGIE MELLON UNIVERSITYIN QATAR) SLIDESFOR 15-453 LECTURE 4 SPRING 2011 26 / 26