Professional Documents
Culture Documents
cs212 Lect02 63 Inter
cs212 Lect02 63 Inter
cs212 Lect02 63 Inter
PROGRAMMING LANGUAGES
TRY THIS
4
REGULAR EXPRESSION
Example
A regular expression for the set of strings over
={a,b} that contain exactly two b’s. Any number of a’s
may occur before, between, and after b’s.
r = a*ba*ba*
Example
The regular expression for the set of strings over
={a,b,c} containing the substring bc.
r = (a+b+c)*bc(a+b+c)* 5
REGULAR EXPRESSION
Example
A regular expression for the set of strings over
={a,b} that contain even number of a’s.
String in r : , aa, aba, baa, babab, b, babbabaa
String not in r : a, ba, ab, aaa, ababa, baaa, …
r = ( b + (ab*ab*))*
6
REGULAR EXPRESSION
Example
A regular expression for the set of strings over ={a,b}
that count number of a’s divisible by 3.
String in r : , aaa, bababa, aaaaaa, baaaaaab, b, bb,
bbb, babababababab, ….
String not in r : a, ba, ab, aa, abba, babaaba, …
r = ( b + (ab*ab*ab*))*
7
REGULAR EXPRESSION
Example
The regular expression for the set of strings over ={a,b} with an
even number of a’s and an even number of b’s
Strings in this language will have clumps of 3 kinds:
Type 1 — aa
Type 2 — bb
Type 3 — starting with ab (or with ba ) and eventually balancing
with another ab or ba.
So:
( aa+bb+ ((ab+ba)(aa+bb)*(ab+ba)) )* 8
REGULAR EXPRESSION
The regular expression R is the string over ={a,b} with an
odd number of a’s.
String in R : a,ab,ba,bab,abbbb,bbbba,bbabbb,…, aaa,
ababa, aabbbba,ababab,abababbb,
abbbabbabbb,bbbbabbabbabbb,…
String not in R : ,aa,baa,aab,bbaa,baba,abab,aaaa, …
The odd number of a’s came from ‘a’ concat with ‘aa’.
bbb….babbb…b bbb…babbb…babbb…b
So that , R = b*ab*(b*ab*ab*)*
9
REGULAR EXPRESSION EXAMPLE
The regular expression R is the string over ={a,b} ending
with b and not containing aa.
String in R : b,ab,bab,abb..b,abab,bb..b, ababb..b,
bb..babb..bab
String not in R : ,aa,ba,baa,bbaa,aabb,aba,aaaab,…
So, string must be the combination of b and ab. The two
smallest string are b, ab.
R= ? (b + ab) ,
R = (b + ab)*(b+ab)
10
TRY THIS
The regular expression of the string over
={a,b,c} in which all the a’s precede the b’s
which in turn precede the c’s.
The regular expression of the string over ={a,b,c} in which all the
a’s precede the b’s which in turn precede the c’s, but without the
empty string .
The regular expression of the string over ={a,b,c} that begin with a,
contain exactly two b's, and end with cc.
15
16
FINITE STATE MACHINE
• ={5,10}
• State diagram
18
DFA AND NFA
19
EXAMPLE DFA
20
EXAMPLE DFA
21
EXAMPLE DFA
Start
22
EXAMPLE DFA
23
• Step 2 : Think about (a+b)* in front of bb. It
should be aaaaaabb or abababababbb
24
EXAMPLE
DFA
R = a*b*c*
Step 1: Build a*
25
R = a*b*c*
Final : Build abc
26
EXAMPLE
28
• From start state , if get one a then #of a is odd and
#of b is even. If get another a the #of a is even and
# b is even.
29
• From state EaOb, if get one a then #of a is odd
and #of b is odd. If get another a the #of a is even
and # b is odd.
30
•From state OaOb, if get one b then #of b is
even and #of a is odd. If get another b the #of
b is odd and # a is odd.
31
EXAMPLE
• R1 = (aa(a+b)*+(a+b)*bb)
32
33
34
Combined them together , we get the final solution for
R1 = (aa(a+b)*+(a+b)*bb) :
35
EXAMPLE
• Build a DFA for regular expression
R = (001)* + (110)*
36
37
38
TRY THIS
No. 1 : Build a DFA that accept a regular expression R = (1+01)+ over
={0,1}
No. 2 : Build a DFA that accept all words that start with triple letter over
={a,b} . ( R = (aaa+bbb)(a+b)* )
No. 3 : Build a DFA that accept all words with even count of letters having
“a” in an even position from the start, where the first letter is letter number
one over ={a,b} . ( R = (a+b)a((a+b)a)* )
No. 4 : Build a DFA that accept set of strings where the number of 0s in
every string is multiple of 3 over ={0,1} . ( R = (1+ 01*01*0)* )
No. 5 : Build a DFA that accept set of strings the odd number of 0s over
39
={0,1} . (R = ((1*01*0)*1*01* )