1.2 Regular Expressions - PowerPoint PPT Presentation

1 / 33
About This Presentation
Title:

1.2 Regular Expressions

Description:

1.2 Regular Expressions Regular Expression RE -NFA DFA RE Regular Expressions A FA is a blueprint for constructing a machine recognizing a regular language. – PowerPoint PPT presentation

Number of Views:172
Avg rating:3.0/5.0
Slides: 34
Provided by: infoRucE
Category:

less

Transcript and Presenter's Notes

Title: 1.2 Regular Expressions


1
1.2 Regular Expressions
  • Regular Expression
  • RE ? ?-NFA
  • DFA ? RE

2
Regular Expressions
  • A FA is a blueprint for constructing a machine
    recognizing a regular language.
  • A regular expression is a user-friendly
    declarative way of describing a language.
  • Example 0110
  • Used in e.g.
  • UNIX grep
  • Perl programming language

3
Inductive Definition of RE
  • R is a regular expression if R is
  • Ø, the empty set
  • ?, the empty string
  • a, for some a ? S
  • R1R2, where R1 and R2 are reg. exp.
  • R1R2, where R1 and R2 are reg. exp.
  • R1, where R1 is a regular expression

4
Language of a RE
  • A regular expression R describes the language
    L(R)
  • L(Ø) Ø
  • L(?) ?
  • L(a) a
  • L(R1R2) L(R1)?L(R2)
  • L(R1R2) L(R1)L(R2)
  • L(R1) (L(R1))

union
concatenation
closure
5
Concatenation of Languages
  • If L1 and L2 are languages, we can define the
    concatenation
  • L1L2 w wxy, x?L1, y?L2
  • Examples
  • ab, bacd, dc ? abcd, abdc, bacd, badc
  • Øab ? Ø

6
Exponentiation of a Language
  • Li is the language L concatenated with itself i
    times.
  • Recursive definition
  • Base L0 ?
  • Induction Li1 LLi
  • Example
  • ab, ba2 ? abab, abba, baab, baba
  • Ø0 ? ?
  • Ø2 ? ?

7
Kleene Closure
  • L ??i0Li
  • L0 ? L1 ? L2 ?
  • Examples
  • ab, ba ? ?, ab, ba, abab, abba,
  • Ø ? ?
  • ? ? ?

8
Example
  • Order of precedence for operators
  • ( ) gt Closure gt Concatenation gt Union
  • L w 0 and 1 alternate in w, S0, 1, how
    to describe L in a regular expression?
  • (01)(10)0(10)1(01)
  • Or equivalently, (?1)(01)(0?)

9
Exercises
  • S 0, 1
  • What is the language for
  • 01
  • What is the regular expression for
  • w w has at least one 1
  • w w starts and ends with same symbol
  • w w ? 5
  • w every 3rd position of w is 1
  • L L1 ? L2 ?
  • L? (means an optional L)

10
Equivalence of RE and FA
?-NFA ? NFA ? DFA
RE
11
1.2 Regular Expressions
  • Regular Expression
  • RE ? ?-NFA
  • DFA ? RE

12
From RE to ?-NFA
  • For every regular expression R, we can construct
    an ?-NFA A, s.t. L(A) L(R).
  • Proof by structural induction
  • Ø
  • ?
  • a

13
From RE to ?-NFA
  • RS
  • RS
  • R

14
Example (01)1(01)
15
Example 1.31 (ab)aba
16
1.2 Regular Expressions
  • Regular Expression
  • RE ? ?-NFA
  • DFA ? RE

17
From DFA to RE
  • For every DFA A(Q, S, ?, q0, F), there is a
    regular expression R, s.t. L(R) L(A).
  • Proof
  • Let Q 1, 2, n and q0 1
  • Let Ri, jk be a RE describing the set of all
    label paths in A from state i to state j going
    through the states 1,k only.
  • Then the union of all R1, jn (j?F) will be the
    final RE describing the language of A

18
From DFA to RE
  • We computes the Ri, jk inductively
  • Initially (k0)
  • Ri, i ?ab, whenever ?(i, a)?(i, b)i,
    etc.
  • Ri, j ab, if i?j and ?(i, a)?(i, b)j,
    etc.
  • Ri, j Ø, if i?j and there are no direct
    transitions from state i to state j.
  • Induction
  • Ri, jk Ri, jk-1 Ri, kk-1 (Rk, kk-1)
    Rk, jk-1

19
From DFA to RE
  • Proof that Ri, jk is correctly computed
  • A path that goes through the states 1,k only
    either
  • never goes through state k, in which case the
    path's label is in the language of Ri, jk-1
  • or goes through k one or more times. In this
    case
  • Ri, kk-1 contains the portion of the path that
    goes from i to k for the first time.
  • (Rk, kk-1) contains the portion of the path
    (possibly empty) from the first k visit to the
    last.
  • Rk, jk-1 contains the portion of the path from
    the last k visit to state j.

20
Example
  • L(A) x0y x?1, y?0, 1

1
0
0,1
2
1
R1, 10 ?1
R1, 20 0
R2, 10 Ø
R2, 20 ?01
k 0
21
Example Contd
  • We need the following simplification rules
  • (?R) R
  • RRS RS
  • ØR RØ Ø (Annihilation)
  • ØR RØ R (Identity)

22
Example Contd
R1, 10 ?1
R1, 20 0
R2, 10 Ø
R2, 20 ?01
k 1
R1, 11 ?1(?1)(?1)(?1) 1
R1, 21 0(?1)(?1)0 10
R2, 11 ØØ(?1)(?1) Ø
R2, 21 ?01Ø(?1)0 ?01
23
Example Contd
R1, 11 1
R1, 21 10
R2, 11 Ø
R2, 21 ?01
k 2
R1, 12 1 10(?01)Ø 1
R1, 22 1010(?01)(?01) 10(01)
R2, 12 Ø(?01)(?01)Ø Ø
R2, 22 ?01(?01)(?01)(?01) 01
24
Example Contd
  • The final regular expression for A is
  • R1, 22 10(01)

25
Observation
  • for k 1 to n
  • for i 1 to n
  • for j 1 to n
  • Bi, j Ri, j Ri, k (Rk, k) Rk, j
  • end
  • end
  • R B (copy all the updates to R).
  • End
  • n3 expressions Ri, jk, and Ri, jk could have
    size 4n
  • Alternative approach state elimination

26
State Elimination Technique
  • Label the edges of the FA with regular
    expressions instead of symbols.
  • Add a new start state, qstart, with an ? edge to
    the old start state and a new accept state,
    qaccept, with ? arrows from the old accept
    states.
  • eliminate all states except qstart and qaccept,
    the regular expression on the label is the answer.

27
State Elimination Technique
  • Eliminate qrip

R2
qrip
R3
qi
qj
R1
R4
R1(R2)R3R4
qi
qj
28
Example 1.35
29
Example 1.36
b
a
2
a
1
3
a
b
b
30
Algebraic Laws for RE
  • Regexs E and F are equivalent L(E) L(F).
  • (E F) G E (F G)
  • Union is associative
  • E F F E
  • Union is commutative
  • Ø E E Ø E
  • Ø is identity for union
  • E E E
  • Union is idempotent

31
Algebraic Laws for RE
  • (E F) G E (F G)
  • Concatenation is associative
  • ? E E ? E
  • ? is right and left identity for concatenation
  • Ø E E Ø Ø
  • Ø is right and left annihilator for concatenation
  • E (F G) E F E G
  • Concatenation is left distributive over union
  • (F G) E F E G E
  • Concatenation is right distributive over union

32
Algebraic Laws for RE
  • Ø ?
  • ? ?
  • (E) E
  • Closure is idempotent
  • E EE ?

33
Assignment 2
  • 1. Convert 0(100) to an ?-NFA
  • 2. Eliminate the ? arcs in your answer
  • 3. Convert the following FA into RE

0
s
q
0
1
1
0
t
p
1
1
0
Write a Comment
User Comments (0)
About PowerShow.com