theory of computation (fall 2014): regular expressions, compiling regular expressions into nfas...

22
Theory of Computation Regular Expressions, Compiling Regular Expressions to NFAs & DFAs to Regular Expressions Vladimir Kulyukin http://www.vkedco.blogspot.com/

Upload: vladimir-kulyukin

Post on 28-Nov-2014

129 views

Category:

Science


9 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Theory of Computation

Regular Expressions, Compiling Regular Expressions to NFAs & DFAs to Regular Expressions

Vladimir Kulyukin

http://www.vkedco.blogspot.com/

Page 2: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Regular Expressions

Page 3: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Atomic & Compound Regular Expressions

● Regular expressions (RegExps) can be atomic and compound

● Atomic regular expressions consists of one symbol● Compound regular expressions are constructed from atomic

and other compound regular expression with three operations: concatenation, selection (+ or |), Kleene * (zero or more occurrences), and Kleene + (one or more occurrences)

Page 4: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Three Language Operations: Concatenation, Kleene*, Kleene+

.,1|... is then language, a is If

.,0|... is of closure Kleene thelanguage, a is If

. and | languages, theare and If

121

021*

221212121

LxnxxxLL

LxnxxxLLL

LxLxxxLLLL

nin

nin

Page 5: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Four RegExp Operations

● Concatenation: if r1 and r

2 are regular expressions, then r

1r

2 is a concatenation

of two regular expressions such that L(r1r

2) = L(r

1)L(r

2)

● Selection (OR): if r1 and r

2 are regular expressions, then r

1 + r

2 is a regular

expression that denotes the inclusive-OR of two regular (aka known as pipe:

r1 | r

2) so that L( r

1+ r

2) = L(r

1) U L(r

2)

● Kleene*: if r is a regular expression, r* is a regular expression such that

L(r*) = (L(r))* ● Kleene+: If r is a regular expression, r+ is a regular expression such that

L(r+ ) = (L(r))+

Page 6: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Atomic Regular Expressions

L

L

aaLa

ΣrL

r

.3

.2

then , If 1.

:sexpressionregular atomic of typesThree

.alphabet someover

language thedenotes that string a is expressionregular A

Page 7: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Examples: RegExps & Languages

.0|

,...,,, ; expressionRegular

., ; expressionRegular

. ; expressionRegular

**

nba

baaabaababbaLba

cabcabLcab

ababLab

n

Page 8: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Examples: RegExps & Languages

.,...,,,,,,

.,...,,,

.,,,

.,

.,

Language | expressionRegular

****

**

***

aaababbbbaababaLba

abcabcabcabcabcabcabcLabc

bdbcadacdcba

ababLab

babaLba

Page 9: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Compiling Regular Expressions to NFAs

Page 10: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Atomic RegExps NFAs

aa

Generalization: for any symbol in the alphabet, an FA can be constructed that accepts just this symbol

Page 11: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Atomic RegExps NFAs

ε

This FA accepts just the epsilon (empty string)

Page 12: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Atomic Reg Exps NFAs

Here is an FA that accepts the empty set.

Page 13: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Compound RegExps NFAs

:follows as is toingcorrespondNFA then thes,expressionregular these

toingcorrespond NFAs are and wheres,expressionregular are , If

21

2121

rr

RRrr

ε

ε

ε

ε

R1

R2

Page 14: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Example

:follows as is toingcorrespondNFA then thes,expressionregular these

toingcorrespond NFAs are and wheres,expressionregular are , If ba

ba

RRba

ε

ε

ε

ε

a

b

Ra

Rb

Page 15: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Compound RegExps NFAs

:follows as dconstructe becan toingcorrespondNFA an then

NFA, ingcorrespond a is and expressionregular be Let

1

11

r

Rr

ε

ε

εR

1

Page 16: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Compound RegExps NFAs

:follows as testedbecan toingcorrespondNFA an then

, expressionregular a toingcorrespondNFA an be Let *

1

11

r

rR

ε

ε

εR

1

ε

Page 17: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Theorem: RegExps NFAs

languages. same with theNFAslarger into combined

becan sexpressionregular theseon,constructiBy languages. same with theNFAs are

there,hypothesis inductive by the Thus, operators. 1 fewer than have and ,,

that know We.,,, form theof becan Then operators. 1 has

that expressionregular aConsider .for trueisstatement that theAssume .0

for trueis case base .Thein operators ofnumber on theinduction use We:Proof

.such that NFA an is e then ther,expressionregular a is If :Theorem

21

2121*

krrr

rrrrrrqk

qkn

r

rLNLNr

Page 18: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Theorem: DFA RegExps

Theorem: If there is a DFA M then there is a regular expression R such that L(M) = L(R).

Basic Insight: Sweep M left to right and augment to build a regular expression incrementally.

Page 19: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Example

a

b

a | b

Page 20: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Example

a

b

a

b

a+ | b+

Page 21: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

Equivalence Constructions

NFA with ε-trans

NFA w/0 ε-trans

DFA

Regular Exps

Page 22: Theory of Computation (Fall 2014): Regular Expressions, Compiling Regular Expressions into NFAs & DFAs to Regular Expressions

References

● Brooks Webber. Formal Language: A Practical Introduction, Franklin, Beedle & Associates, Inc

● Davis, Weyuker, Sigal. Computability, Complexity, and Languages, 2nd Edition, Academic Press

● Hopcroft and Ullman. Introduction to Automata Theory, Languages, and Computation, Narosa Publishing House