Professional Documents
Culture Documents
Lec 05
Lec 05
Siu On CHAN
Fall 2018
Chinese University of Hong Kong
1/28
Text Search
grep program
2/28
Searching with grep
Words containing
savor or savour
cd /usr/share/dict/
grep -E 'savou?r' words
savor
savor's
savored
savorier
savories
savoriest
savoring
savors
savory
savory's
unsavory
3/28
Searching with grep
Words containing Words with 5 consecutive a or b
savor or savour grep -E '[abAB]{5}' words
cd /usr/share/dict/ Babbage
grep -E 'savou?r' words
savor
savor's
savored
savorier
savories
savoriest
savoring
savors
savory
savory's
unsavory
3/28
More grep commands
. any symbol
[a-d] anything in a range
^ beginning of line
$ end of line
4/28
How do you look for
5/28
How grep (could) work
regular
NFA DFA
expression
input
text file
6/28
Implementation of grep
7/28
Closure properties
Example
(0 + 1)∗ 101
8/28
Example
(0 + 1)∗ 101
8/28
Complement
L = {w ∈ Σ∗ | w ∈
/ L}
10/28
Closure under complement
If L is a regular language, so is L
regular
NFA DFA
expression
11/28
Arguing closure under complement
accepts L
Now consider the DFA M 0 with the accepting and rejecting states of
M reversed
12/28
Can we do the same with an NFA?
0, 1
1 0 (0 + 1)∗ 10
q0 q1 q2
0, 1
q0 1 q1 0 q2
13/28
Can we do the same with an NFA?
0, 1
1 0 (0 + 1)∗ 10
q0 q1 q2
0, 1
(0 + 1)∗
q0 1 q1 0 q2 Not the complement!
13/28
Intersection
Examples:
L L0 L ∩ L0
(0 + 1)∗ 11 1∗ 1∗ 11
L L0 L ∩ L0
(0 + 1)∗ 10 1∗ ∅
14/28
Closure under intersection
regular
NFA DFA
expression
15/28
Example
1
s0 s1
1
0 0
r1 1
1
s0 s1
1
0 0 0 0 0 0
1
r1 r1 , s0 r1 , s1
1
1
accepting F for M F × F0 =
states F 0 for M 0 {(ri , sj ) | ri ∈ F , sj ∈ F 0 }
a
transitions ri rj ri , sk a rj , s`
a
sk s`
18/28
Reversal
19/28
Reversal of regular languages
How about LR ?
20/28
Closure under reversal
If L is a regular language, so is LR
How do we argue?
regular
NFA DFA
expression
21/28
Arguing closure under reversal
22/28
Inductive proof of closure under reversal
∅ ∅
ε ε
a a
E1 + E2 E1R + E2R
E1 E2 E2R E1R
E1∗ (E1R )∗
23/28
Duplication?
Example:
L DUP
= {ww | w ∈ L} L = {cat, dog}
LDUP = {catcat, dogdog}
24/28
Attempts
LDUP = L2
?
25/28
Attempts
q0 ε ε ε q1
NFA for L NFA for L
25/28
An example
L = language of 0∗ 1 (L is regular)
L = {1, 01, 001, 0001, . . . }
L DUP
= {11, 0101, 001001, 00010001, . . . }
= {0n 10n 1 | n > 0}
26/28
An example
0 0 0 0 …
1 1 1 1
1 01 001 0001
27/28
An example
0 0 0 0 …
1 1 1 1
1 01 001 0001
Next lecture: will show that languages like LDUP are not regular
27/28
Backreferences in grep
28/28