Download presentation
Presentation is loading. Please wait.
Published byKaren Barnett Modified over 8 years ago
1
CSCI 3130: Automata theory and formal languages Andrej Bogdanov http://www.cse.cuhk.edu.hk/~andrejb/csc3130 The Chinese University of Hong Kong Text search and closure properties Fall 2011
2
Text search
3
The program grep grep -E regexp file Searches for the occurrence of patterns matching a regular expression [ab][12] { a1, a2, b1, b2 } concatenation cat|12 { cat, 12 } union [abc] { a, b, c } shorthand for a|b|c [ab]{2} { aa, ab, ba, bb } { n } copies (ab)* { , ab, abab,...} star [ab]? { , a, b} zero or one (cat)+ { cat, catcat,...} one or more
4
Searching with grep grep –E `savou?r` words outsavor savor savored savorer savorily savoriness savoringly savorless savorous savorsome savory savour unsavored unsavoredly unsavoredness unsavorily unsavoriness unsavory Words containing savor or savour grep –E `[ab]{5}` words grabbable Words with 5 consecutive a or b grep –E `zo+zo+` words zoozoo
5
More grep commands $ end of line. any symbol \< beginning of line [a-z] anything in a range s u f f e r r e g u l a r s t a r n f a a l e n 1 2 3 4 5 1 If a DFA is too hard, I do an... 2 CSCI 3130 homework makes me... 3 If a DFA likes it, it is... 4 $10000000 = $10 ? 5 I study 3130 hard because it will make me... grep –E `\<.ff.u..t` words
6
how do you look for... grep –E `([aeiouy].*){10}` words grep –E `\<cat.*cat` words grep –E `\<[^AEIOUYaeiouy]*$` words Words with at least ten vowels? Words that start in cat and have another cat Words without any vowels? [^ R ] does not contain R grep –E `\<[^AEIOUYaeiouy]* ([aeiouy][^AEIOUYaeiouy]*){10}$` words Words with exactly ten vowels?
7
How grep (could) work regular expression NFA NFA without DFA text file input [ab]?, a+, (cat){3} not allowedallowed input handling matches wholelooks for pattern outputaccept/reject finds pattern differencesin classin grep
8
Implementation of grep How do you handle expressions like: [ab]? zero or one (cat)+ one or more a{3} { n } copies → ()|[ab] R ? → |R → (cat)(cat)* R + → RR * → aaa R { n } → RR...R n times [^aeiouy] not containing any ?
9
Closure properties
10
Example The language L of strings that end in 101 is regular How about the language L of strings that do not end in 101 ? (0+1)*101
11
Example Hint: w does not end in 101 if and only if it ends in: or it has length 0, 1, or 2 So L can be described by the regular expression 000, 001, 010, 011, 100, 110 or 111 (0+1)*(000+001+010+010+100+110+111) + + (0 + 1) + (0 + 1)(0 + 1)
12
Complement The complement L of a language L contains those strings that are not in L Examples ( = {0, 1} ) –L 1 = all strings that end in 101 –L 1 = all strings that do not end in 101 = all strings end in 000, …, 111 or have length 0, 1, or 2 –L 2 = 1* = { , 1, 11, 111, …} –L 2 = all strings that contain at least one 0 = (0 + 1)*0(0 + 1)*
13
Example The language L of strings that contain 101 is regular How about the language L of strings that do not contain 101 ? (0+1)*101(0+1)* You can write a regular expression, but it is a lot of work!
14
Closure under complement To argue this, we can use any of the equivalent definitions for regular languages: The DFA definition will be most convenient –We assume L has a DFA, and show L also has a DFA regular expression DFANFA If L is a regular language, so is L.
15
Arguing closure under complement Suppose L is regular, then it has a DFA M Now consider the DFA M’ with the accepting and rejecting states of M reversed accepts L accepts strings not in L this is exactly L
16
Food for thought Can we do the same thing with an NFA? 1 0 0, 1 qq qq qq (0+1)*10 1 0 0, 1 qq qq qq (0+1)* NO!
17
Intersection The intersection L L’ is the set of strings that are in both L and L’ Examples: If L, L’ are regular, is L L’ also regular? L = (0 + 1)*11L’ = 1* L L’ = L = (0 + 1)*10L’ = 1* L L’ = 1*11 ∅
18
Closure under intersection If L and L’ are regular languages, so is L L’. regular expression DFANFA To argue this, we can use any of the equivalent definitions for regular languages: Suppose L and L’ have DFAs, call them M and M’ Goal: Construct a DFA (or NFA) for L L’
19
An example L = even number of 0 s s0s0 s1s1 0 0 1 1 M’M’ L’ = odd number of 1 s L L’ = even number of 0 s and odd number of 1 s r 0, s 1 1 1 1 1 0 0 0 0 r 0, s 0 r 1, s 1 r 1, s 0 r0r0 r1r1 1 M 1 0 0
20
Closure under intersection M and M’ DFA for L L’ states Q = {r 1,..., r n } Q’ = {s 1,..., s n’ } Q × Q’ = {(r 1, s 1 ), (r 1, s 2 ),..., (r 2, s 1 ),..., (r n, s n’ )} Whenever M is in state r i and M’ is in state s j, the DFA for L L’ will be in state (r i, s j ) start state accepting states r i for M s j for M’ (r i, s j ) F for M F’ for M’ F × F’ = {(r i, s j ): r i ∈ F, s j ∈ F’}
21
Closure under intersection M and M’ DFA for L L’ transitions s i’ s j’ a r i, s i’ r j, s j’ a in M in M’ riri rjrj a
22
Reversal The reversal w R of a string w is w written backwards The reversal L R of a language L is the language obtained by reversing all its strings w = dog w R = god L = { cat, dog }L R = { tac, god }
23
Reversal of regular languages L = all strings that end in 101 is regular How about L R ? This is the language of all strings beginning in 101 Yes, because it is represented by (0+1)*101 101(0+1)*
24
Closure under reversal How do we argue? If L is a regular language, so is L R. regular expression DFANFA
25
Arguing closure under reversal Take a regular expression E for L We will show how to reverse E A regular expression can be of the following types: –The special symbols and –Alphabet symbols like a, b –The union, concatenation, or star of simpler expressions
26
Proof of closure under reversal regular expression E a (alphabet symbol) E 1 + E 2 reversal E R E1E2E1E2 E1*E1* a E 1 R + E 2 R E2RE1RE2RE1R (E 1 R )*
27
A question If L is regular, is L DUP also regular? regular expression DFANFA ? L DUP = {ww: w L} L = { cat, dog } L DUP = { catcat, dogdog }
28
A question Let’s try with regular expression: Let’s try with NFA: q0q0 q1q1 NFA for L L DUP = L 2 L = { a, b } L DUP = { aa, bb } LL = { aa, ab, ba, bb }
29
An example Let’s try to design an NFA for L DUP L = 0*1 is regular L DUP = {11, 0101, 001001, 00010001,...} = {0 n 10 n 1: n ≥ 0} L DUP = {1, 01, 001, 0001,...}
30
An example L DUP = {11, 0101, 001001, 00010001,...} = {0 n 10 n 1: n ≥ 0} 0 000 0001 1 001 1 01 1 1 1
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.