Upload
abir-dutta
View
224
Download
0
Embed Size (px)
Citation preview
7/28/2019 Lecture 05 RE1 Djn
1/38
Regular Expressions
7/28/2019 Lecture 05 RE1 Djn
2/38
2
Regular Expressions
A regular expression is used to specify a language, and it does
so precisely.
Regular expressions are very intuitive.
Regular expressions are very useful in a variety of contexts.
Given a regular expression, an NFA- can be constructed from it
automatically.
Thus, so can an NFA, a DFA, and a corresponding program, all
automatically!
7/28/2019 Lecture 05 RE1 Djn
3/38
3
Definition of a Regular Expression Let be an alphabet. The regular expressions over are:
Represents the empty set { } Represents the set {}
a Represents the set {a}, for any symbol a in
Let r and s be regular expressions that represent the sets R and S,respectively.
r+s Represents the set R U S (precedence 3)
rs Represents the set RS (precedence 2)
r* Represents the set R* (highest precedence)
(r) Represents the set R (not an op, provides precedence)
If r is a regular expression, then L(r) is used to denote the correspondinglanguage.
7/28/2019 Lecture 05 RE1 Djn
4/38
4
Identities:
1. u = u = Multiply by 0
2. u = u = u Multiply by 1
3. * = L* = Li = L0 U L1 U L2U
4. * = = {}
5. u+v = v+u
6. u + = u
7. u + u = u
8. u* = (u*)*9. u(v+w) = uv+uw
10. (u+v)w = uw+vw
11. (uv)*u = u(vu)*
12. (u+v)* = (u*+v)*
= u*(u+v)*
= (u+vu*)*
= (u*v*)*
= u*(vu*)*
= (u*v)*u*
0i
7/28/2019 Lecture 05 RE1 Djn
5/38
Regular grammar and regular
expression They are equivalent
Every regular expression can be expressed by
regular grammar
Every regular grammar can be expressed byregular expression
Different ways to express the same thing
RE is more concise
7/28/2019 Lecture 05 RE1 Djn
6/38
6
Operations on Languages
Let L, L1, L2 be subsets of*
Concatenation: L1L2 = {xy | x is in L1 and y is in L2}
Concatenating a language with itself: L0= {}
Li = LLi-1, for all i >= 1
Kleene Closure: L* = Li = L0 U L1 U L2U
Positive Closure: L+ = Li = L1 U L2U
Question: Does L+contain ?
0i
1i
7/28/2019 Lecture 05 RE1 Djn
7/38
abc, abbbc, abbbbccc, bc
a, aa, aaa, aaaa, aaaaa
, b, bb, bbb, bbbbb, bbbbb
7/28/2019 Lecture 05 RE1 Djn
8/38
RE operations
Operation Notati
on
Definition Example
L={a, b} M={0,1}
union of L
and M
L M L M = {s | s is in L
or s is in M}
{a, b, 0, 1}
concatenatio
n of L and M
LM LM = {st | s is in L
and t is in M}
{a0, a1, b0, b1}
Kleene
closure of L
L* L* denotes zero or
more
concatenations of L
All the strings consists
of a and b, plus theempty string. {, a, aa,
bb, ab, ba, aaa, }
positive
closure
L+ L+ denotes one or
more
concatenations of
L
All the strings consists
of a and b.
7/28/2019 Lecture 05 RE1 Djn
9/38
9
Examples: Let = {0, 1}
(0 + 1)* All strings of 0s and 1s
0(0 + 1)* All strings of 0s and 1s, beginning with a 0
(0 + 1)*1 All strings of 0s and 1s, ending with a 1
(0 + 1)*0(0 + 1)* All strings of 0s and 1s containing at least one 0
(0 + 1)*0(0 + 1)*0(0 + 1)* All strings of 0s and 1s containing at least two 0s
(0 + 1)*01*01* All strings of 0s and 1s containing at least two 0s
(1 + 01*0)* All strings of 0s and 1s containing an even numberof 0s
1*(01*01*)* All strings of 0s and 1s containing an even numberof 0s
(1*01*0)*1* All strings of 0s and 1s containing an even numberof 0s
7/28/2019 Lecture 05 RE1 Djn
10/38
Regular expressions
R is a regular expression if R is
a, for some a
, the empty string
, the empty set
(R1 R2), where R1 and R2 are reg. exprs.
(R1 R2), where R1 and R2 are reg. exprs.
(R1*), where R1 is a regular expressionA reg. expression R describes the language L(R).
7/28/2019 Lecture 05 RE1 Djn
11/38
Regular expressions
example: R = (0 1)
if= {0,1} then use as shorthand for R
example: R = 0 *
shorthand: omit R = 0*
precedence: *, then , then , unless override by
parentheses in example R = 0(*), not R = (0)*
7/28/2019 Lecture 05 RE1 Djn
12/38
Some examples
{w : w has at least one 1}
= *1*
{w : w starts and ends with same symbol}
= 0*0 1*1 0 1
{w : |w| 5}
= ()()()()()
{w : every 3rd position of w is 1}
= (1)*( 1 1)
alphabet
= {0,1}
7/28/2019 Lecture 05 RE1 Djn
13/38
Manipulating regular expressions
The empty set and the empty string: R = R
R = R = R
R = R = and behave like +, x; , behave like 0,1
additional identities: R R = R (here + and differ)
(R1*R2)*R1* = (R1 R2)*
R1(R2R1)* = (R1R2)*R1
7/28/2019 Lecture 05 RE1 Djn
14/38
Languages of Regular Expressions
: language of regular expression
Example
rL r
,...,,,,,*)( bcaabcaabcacbaL
7/28/2019 Lecture 05 RE1 Djn
15/38
Definition (continued)
For regular expressions and
1r 2r
2121 rLrLrrL
2121 rLrLrrL
** 11 rLrL
11 rLrL
7/28/2019 Lecture 05 RE1 Djn
16/38
Example Regular expression: *aba
*abaL *aLbaL
*aLbaL
*aLbLaL
*aba
,...,,,, aaaaaaba
,...,,,...,,, baababaaaaaa
7/28/2019 Lecture 05 RE1 Djn
17/38
Example
Regular expression bbbaar **
}0,:{ 22 mnbbarL mn
7/28/2019 Lecture 05 RE1 Djn
18/38
Example
Regular expression *)10(00*)10( r
)(rL = { all strings with at least
two consecutive 0 }
7/28/2019 Lecture 05 RE1 Djn
19/38
Example
Regular expression )0(*)011( r
)(rL = { all strings without
two consecutive 0 }
7/28/2019 Lecture 05 RE1 Djn
20/38
Equivalent Regular Expressions
Definition:
Regular expressions and
are equivalent if
1r 2r
)()( 21 rLrL
7/28/2019 Lecture 05 RE1 Djn
21/38
Example
L = { all strings without
two consecutive 0 }
)0(*)011(1 r
)0(*1)0(**)011*1(2 r
LrLrL )()( 211r 2rand
are equivalent
regular expr.
7/28/2019 Lecture 05 RE1 Djn
22/38
Therefore:
** 11
2121
2121
rLrL
rLrLrrL
rLrLrrL
Are regular
languages
7/28/2019 Lecture 05 RE1 Djn
23/38
1. All words begin with a, end with a and in between
any word using b.
2. Is A*b* = (ab)*
3. Define language such that it contain at least one
double letter.
a+ab*a
(a+b)*(aa+bb)(a+b)*
7/28/2019 Lecture 05 RE1 Djn
24/38
Regular expression exercises
Can the string baa be created from the
regular expression a*b*a*b* ?
Describe the language (in words)represented by (a*a)b|b.
Write the regular expression that represents:
All strings over={a, b} that end in a.
All strings over={0,1} of even length.
7/28/2019 Lecture 05 RE1 Djn
25/38
Regular expressions and FA
a language L is recognized by a FA if and
only ifL is described by a regular expression.
Must prove two directions:
() L is recognized by a FA implies L is described
by a regular expression
() L is described by a regular expression implies L
is recognized by a FA.
7/28/2019 Lecture 05 RE1 Djn
26/38
Regular expressions and FA
() L is described by a regular expression implies L
is recognized by a FA
Proof: given regular expression R we will builda NFA that recognizes L(R).
then NFA, FA equivalence implies a FA for L(R).
7/28/2019 Lecture 05 RE1 Djn
27/38
RE to NFA
7/28/2019 Lecture 05 RE1 Djn
28/38
Thompson construction
Introduced by Ken Thompson.
Key idea:
NFA pattern for each symbol and operator;
Join them with moves;
Based on the inductive definition of RE.
7/28/2019 Lecture 05 RE1 Djn
29/38
29
Basis: OP(r) = 0
Then r is either, , ora, for some symbol a in
For :
For:
Fora:
qfq0
qf
qfq0a
7/28/2019 Lecture 05 RE1 Djn
30/38
Thompson construction (basis)
For epsilon: The NFA for the
expression has an
arc labeled from its
start node (i) to its endnode (f).
For c:
The NFA for theregular expression c,
for any characterc,
has an arc labeled c
from its start node (i) to
its end node (f).
fi
fi
c
7/28/2019 Lecture 05 RE1 Djn
31/38
Induction step in Thompson construction:
s|t Given REs s and t, suppose N(s) and N(t) are NFAs for s
and t.
NFA(s | t) is:
Add two new states i and f.
Add two -transitions from i to the start states of N(s) andN(t);
Add two transitions from the final states of N(s) and N(t)to f.
N(s)
N(t)
i f
7/28/2019 Lecture 05 RE1 Djn
32/38
Induction step for st
Given REs s and t, suppose N(s) and N(t) are NFAs
New start state: start state of N(s);
New final state: final state of N(t);
Final state of N(s) is merged with the start state ofN(t);
Q: What if there are multiple final states in N(s)?
N(s) N(t) fi
7/28/2019 Lecture 05 RE1 Djn
33/38
Induction step for s*
N(s) is NFA for s;
Add two new states: start state i and final state f;
The NFA for the regular expression s* has empty arcs
from ito f, from ito s.i, from s.fto s.i, and from s.fto f.
N(s)
i f
7/28/2019 Lecture 05 RE1 Djn
34/38
Example for constructing (a|b)*abb
Recall the DFA
and NFA. We
have seen how to
transform theNFA to DFA. But
how the NFA can
be constructed
automatically?
start0
bba
a
b
1 2 3
start 0 3b
21ba
b
a
b
a
a
7/28/2019 Lecture 05 RE1 Djn
35/38
Another example for Thompson construction
Construct NFA for
a, b, and c.
Construct b|c
(b|c)*
a
b
c
b
c
b
c
7/28/2019 Lecture 05 RE1 Djn
36/38
Example:
r = 0(0+1)*
r = r1r2
r1 = 0
r2 = (0+1)*
r2 = r3*
r3 = 0+1
r3 = r4 + r5
r4 = 0
r5 = 1
q6 q5q4
q0 q11
q2 q30
qf
7/28/2019 Lecture 05 RE1 Djn
37/38
Definitions Required to
Convert a DFA to a Regular Expression
Let M = (Q, , , q1, F) be a DFA with state set Q = {q1,
q2
, , qn
}, and define:
Ri,j= { x | x is in * and (qi,x) = qj}
Ri,j is the set of all strings that define a path in M from qito qj.
7/28/2019 Lecture 05 RE1 Djn
38/38
Identities for regular expression