Sei sulla pagina 1di 31

Regular Expressions

1
Regular Expressions vs. Finite
Automata
 Offers a declarative way to express the pattern of any
string we want to accept
 E.g., 01*+ 10*

 Automata => more machine-like


< input: string , output: [accept/reject] >
 Regular expressions => more program syntax-like

2
Regular Expressions
Regular = Finite Automata
expressions (DFA, NFA, -NFA)
Syntactical
expressions Automata/machines

Regular
Languages

Formal language
classes

3
Language Operators
 Union of two languages:
 L U M = all strings that are either in L or M
 Note: A union of two languages produces a third
language
 Contoh: {01,111,10}{00, 01} = {01,111,10,00}

4
Language Operators
 Concatenation of two languages:
 L . M = all strings that are of the form xy
s.t., x  L and y  M
 The dot operator is usually omitted
 i.e., LM is same as L.M
 Contoh : {01,111,10}{00, 01} = {0100,
0101, 11100, 11101, 1000, 1001}

5
Kleene Closure (the * operator)
 If L is a language, then L*, the Kleene
star or just “star,” is the set of strings
formed by concatenating zero or more
strings from L, in any order.
 L* = {ε}  L  LL  LLL  …
 Example: {0,10}* = {ε, 0, 10, 00, 010,
100, 1010,…}

6
“i” here refers to how many strings to concatenate from the parent
language L to produce strings in the language L i

Kleene Closure (the * operator)


 Kleene Closure of a given language L:
 L0= {}
 L1= {w | for some w  L}

L2= { w1w2 | w1  L, w2  L (duplicates allowed)}

Li= { w1w2…wi | all w’s chosen are  L (duplicates allowed)}

(Note: the choice of each wi is independent)

L* = Ui≥0 Li (arbitrary number of concatenations)
Example:
 Let L = { 1, 0}
 L0= {}
 L1= {1,0}
 L2= {11,10,01,00}
 L3= {111,110,101,100,000,001,010,011}

L* = L0 U L1 U L2 U …

7
RE’s: Definition
 Basis 1: If a is any symbol, then a is a
RE, and L(a) = {a}.
 Note: {a} is the language containing one
string, and that string is of length 1.
 Basis 2: ε is a RE, and L(ε) = {ε}.
 Basis 3: ∅ is a RE, and L(∅) = ∅.

8
RE’s: Definition – (2)

9
Precedence of Operators
 Parentheses may be used wherever
needed to influence the grouping of
operators.
 Order of precedence is * (highest), then
concatenation, then + (lowest).

10
Precedence of Operators

11
Examples: RE’s

12
Building Regular Expressions
 Let E be a regular expression and the
language represented by E is L(E)
 Then:
 (E) = E
 L(E + F) = L(E) U L(F)
 L(E F) = L(E) L(F)
 L(E*) = (L(E))*

13
Example: how to use these regular
expression properties and language
operators?
 L = { w | w is a binary string which does not contain two consecutive 0s or two
consecutive 1s anywhere)
 E.g., w = 01010101 is in L, while w = 10010 is not in L
 Goal: Build a regular expression for L
 Four cases for w:
 Case A: w starts with 0 and |w| is even
 Case B: w starts with 1 and |w| is even
 Case C: w starts with 0 and |w| is odd
 Case D: w starts with 1 and |w| is odd
 Regular expression for the four cases:
 Case A: (01)*
 Case B: (10)*
 Case C: 0(10)*
 Case D: 1(01)*
 Since L is the union of all 4 cases:
 Reg Exp for L = (01)* + (10)* + 0(10)* + 1(01)*
 If we introduce  then the regular expression can be simplified to:

Reg Exp for L = ( +1)(01)*( +0)

14
Precedence of Operators
 Highest to lowest
 * operator (star)

. (concatenation)
 + operator

 Example:
 01* + 1 = ( 0 . ((1)*) ) + 1

15
Finite Automata (FA) & Regular
Expressions (Reg Ex)
 To show that they are interchangeable,
consider the following theorems:
 Theorem 1: For every DFA A there exists a regular
Proofs expression R such that L(R)=L(A)
in the book  Theorem 2: For every regular expression R there
exists an  -NFA E such that L(E)=L(R)

 -NFA NFA
Theorem 2 Kleene Theorem

Reg Ex DFA
Theorem 1
16
DFA Reg Ex
Theorem 1

DFA to RE construction
Informally, trace all distinct paths (traversing cycles only once)
from the start state to each of the final states
and enumerate all the expressions along the way

Example: 1 0 0,1

q0 0 q1 1 q2

(1*) 0 (0*) 1 (0 + 1)*

1* 00* 1 (0+1)*

Q) What is the language?

1*00*1(0+1)*
17
RE to -NFA construction

Reg Ex  -NFA
Theorem 2

18
Form of ε-NFA’s Constructed

No arcs from outside,


Start state: no arcs leaving
“Final” state:
Only state Only state
with external with external
predecessors successors

19
RE to ε-NFA: Basis
a
 Symbol a:

ε
 ε:


∅:

20
RE to ε-NFA: Induction 1 – Union

For E1
ε ε

ε ε
For E2

For E1  E2
21
RE to ε-NFA: Induction 2 –
Concatenation

ε
For E1 For E2

For E1E2

22
RE to ε-NFA: Induction 3 – Closure
ε

ε ε
For E

For E*

23
RE to ε-NFA Example
 Convert R= (ab+a)* to an NFA
 We proceed in stages, starting from simple
elements and working our way up
a
a

b
b

a ε b
ab
RE to ε-NFA Example (2)
ab+a
a ε b
ε ε

a
ε ε

(ab+a)*
a ε b
ε ε
ε ε

a
ε ε

ε
RE to -NFA construction
RE to -NFA construction
Example: (0+1)*01(0+1)*

27
Algebraic Laws of Regular
Expressions
 Commutative:
 E+F = F+E
 Associative:
 (E+F)+G = E+(F+G)
 (EF)G = E(FG)
 Identity:
 E+Φ = E

E=E=E
 Annihilator:
 ΦE = EΦ = Φ

28
Algebraic Laws…
 Distributive:
 E(F+G) = EF + EG
 (F+G)E = FE+GE
 Idempotent: E + E = E
 Involving Kleene closures:
 (E*)* = E*
 Φ* = 
 * = 
 E+ =EE*
 E? =  +E

29
True or False?
Let R and S be two regular expressions. Then:

1. ((R*)*)* = R* ?

2. (R+S)* = R* + S* ?

3. (RS + R)* RS = (RR*S)* ?

30
Summary
 Regular expressions
 Equivalence to finite automata
 DFA to regular expression conversion
 Regular expression to -NFA conversion
 Algebraic laws of regular expressions
 Unix regular expressions and Lexical
Analyzer

31

Potrebbero piacerti anche