Homework 1 Solutions A) Problem 3.7 (18 Points)
Homework 1 Solutions A) Problem 3.7 (18 Points)
Homework 1 Solutions
By Theorem 2.4 in Hopcroft and Ullman this can be converted to a regular expression but it gets rather tedious. Here is a regular de nition for all digits: (e) All strings of digits with at most one repeated digit. (2 points) The DFA for the digits 0,1,2:
0 b 0 a 1 2 0 f 0 1 2 1 c 0 1 1 g 2 2 0
0 2 1 d 1 2 0 2 h 0 0 l 1 2 1 e 1 0 i 0 m 2 1 2
e
0,1,2
1 2 1 0 2 j k 2
By Theorem 2.4 in Hopcroft and Ullman this can be converted to a regular expression but it is also quite tedious. (f) All strings of 0's and 1's with an even number of 0's and odd number of 1's. (2 points) The DFA:
a 0 b 0 1 1 1 1 0 d c 0
The regular expression: AB*, where A = 1 j 0(00 j 11)*(10 j 01) B = 00 j 11 j (01 j 10)(00 j 11)*(01 j 10) (g) The set of chess moves, such as p - k4 or kbp qn. (2 points) pieces = (kjq)(rjnjbj )p? moves = pieces pieces j pieces-(kjq)(rjnjb) 1-8]) j 0-0-0 j 0-0 (h) All strings of 0's and 1's that do not contain the substring 011. (2 points) The DFA:
0 1 0 0 1 1 2 1 3 0,1 0
The regular expression: 1*0* j 1*00*1(00*1)* or 1*(0 j 01)* (i) All strings of 0's and 1's that do not contain the subsequence 011. (2 points) The DFA:
0 1 0 0 1 1 2 0 1 3 0,1
The following diagram shows the moves made in accepting the input string ababbab b a b 0 ! 1 ! 2 ba ! 3 ! 6 ! 1 ! 4a ! 5 !6 !1 !2 !3 !6 !1 !4 ! 5 ! b 6 !1 !4 !5 !6 !1 !2 !3 !6 !1 !4 !5 !6 !7 (b) (a*jb*)* (2 points)
e e 2 e 0 e 1 e 6 e 7 e e b e e 8 e e 9 e 3 a 4 e 5
e 10 e 11
The following diagram shows the moves made in accepting the input string ababbab b 0 !1 !2 !3 a !4 !5 ! 10 ! 1 ! 6 !7 ! 8 ! 9 ! 10 ! 1 a !2 !3 a ! b b 4 ! 5 ! 10 ! 1 b! 6 ! 7 ! 8 ! 7 ! 8 ! 9 ! 10 ! 1 ! 2 ! 3 ! 4 ! 5 ! 10 ! 1 ! 6 ! 7 ! 8 ! 9 ! 10 ! 11 (c) (( ja)b*)* (2 points)
e e a e 0 e 1 e 4 e 5 e e e 2 3 e 6 e 7 e b 8 e 9 e 10 e 11
The following diagram shows the moves made in accepting the input string ababbab 3
e a e 0 e 1 e 4 b e 5 e 2 3 e 6 e 7 a 8 b 9 b 10 e 11 e 14 e 12
e a 13 e 16 b e 15 e e 17
The following diagram shows the moves made in accepting the input string ababbab b b b 0 !1 !2 a ! 3b ! 6 ! 1 ! 4 ! 5 !6 !7 a !8 ! 9 ! 10 ! 11 ! 12 a ! 13 ! 16 ! 11 ! 14 ! 15 ! 16 ! 17
The following diagram shows the moves made in accepting the input string ababbab b b 0 a !1 ! 2 a !1 b !2 ! 2 a !1 b !2 (b) (a*jb*)* (2 points)
a a 0 b b 2 b 1 a or the minimized version 0 a b
The following diagram shows the moves made in accepting the input string ababbab b b 0 a !1 ! 2 a !1 b !2 ! 2 a !1 b !2 (c) (( ja)b*)* (2 points)
a a 0 b b 2 b 1 a or the minimized version 0 a b
The following diagram shows the moves made in accepting the input string ababbab b b 0 a !1 ! 2 a !1 b !2 ! 2 a !1 b !2 (d) (ajb)*abb(ajb)* (2 points)
b 0 a 1 a a b 2 b 3 b a
The following diagram shows the moves made in accepting the input string ababbab b b 0 a !1 ! 2 a !1 b !2 ! 3 a !3 b !3
R ) RR (by de nition of ) and R ! RR in productions of G) R )*r1R (by inductive hypothesis) R )*r1r2 (by inductive hypothesis) So r1 r2 2 L(G) R ) R* (by de nition of ) and R ! R* in productions of G) R )*r1* (by inductive hypothesis) So r*2 L(G) R ) (R) (by de nition of ) and R ! (R) in productions of G) R )*(r1) (by inductive hypothesis) So (r) 2 L(G) (b) Show that this grammar is ambiguous. (2 points) One ambiguous derivation is:
R RR RR aa RR bc a
R RR RR a b RR c
(c) Construct an equivalent unambiguous grammar that gives the operators *, concatenation, and j the precedences and associativities de ned in Section 3.3. (3 points) A ! A0 j0B j B B ! BC j C C !D jD D ! (A) j a j b j c (d) Construct a parse tree in both grammars for the sentence ajb*c. (2 points)
R RR R|R a R* b c B B B a C C* D b A A|B BC D c
E) (10 points)
Write an unambiguous grammar that generates fxjx 2 L and x contains no redundant parens g. E ! LP + RP j LM RM LP ! LP + RP j LM RM j a RP ! (LP + RP ) j LM RM j a LM ! LM RM j (LP + RP ) j a RM ! (LM RM ) j (LP + RP ) j a