Professional Documents
Culture Documents
Reading: Chapter 3
Offers a declarative way to express the pattern of any string we want to accept
Regular expressions => more program syntax-like Unix environments heavily use regular expressions
Perl scripting good for string processing Lexical analyzers such as Lex or Flex
2
Regular Expressions
Regular expressions Syntactical expressions
Language Operators
L U M = all strings that are either in L or M Note: A union of two languages produces a third language
L . M = all strings that are of the form xy s.t., x L and y M The dot operator is usually omitted
i here refers to how many strings to concatenate from the parent language L to produce strings in the language Li
Kleene Closure of a given language L: L0= {} L1= {w | for some w L} L2= { w1w2 | w1 L, w2 L (duplicates allowed)} Li= { w1w2wi | all ws chosen are L (duplicates allowed)}
Therefore, an abbreviated way of saying there is an arbitrary language L over an alphabet is:
L *
6
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)
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 Case A: Case B: Case C: Case D: (01)* (10)* 0(10)* 1(01)*
Reg Exp for L = (01)* + (10)* + 0(10)* + 1(01)* Reg Exp for L = ( +1)(01)*( +0)
Precedence of Operators
Highest to lowest
* operator (star)
(concatenation)
+ operator
Example:
01* + 1
( 0 . ((1)*) ) + 1
9
Theorem 1: For every DFA A there exists a regular expression R such that L(R)=L(A) Theorem 2: For every regular expression R there exists an -NFA E such that L(E)=L(R)
-NFA
Theorem 2
Reg Ex
DFA
Theorem 1
Reg Ex
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 q0 0 0 q1 0,1
q2
(1*) 0 1*
(0*) 1
00*
1*00*1(0+1)*
11
Reg Ex
Theorem 2
-NFA
RE to -NFA construction
Example: (0+1)*01(0+1)*
(0+1)*
0
01
(0+1)*
0 1
12
Commutative:
Associative:
Identity:
Annihilator:
Algebraic Laws
Distributive:
(E*)* * * E+ E?
= E* = = =EE* = +E
14
True or False?
Let R and S be two regular expressions. Then:
1.
((R*)*)* = R*
2.
(R+S)* = R* + S*
(RS + R)* RS = (RR*S)*
?
?
3.
15
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
16