Sie sind auf Seite 1von 6

CS 412/CS413 Spring 1997

Intro to Compilers and Translators Handout 10 February 14, 1997

Homework 1 Solutions

A) Problem 3.7 (18 points)


Write regular de nitions for the following languages. (a) All strings of letters that contain the ve vowels in order (2 points) cons = ( bB] j cC] j dD] j fF] j gG] j hH] j jJ] j kK] j lL] j mM] j nN] j pP] j qQ] j rR] j sS] j tT] j vV] j wW] j xX j yY] j zZ]) (cons)* aA]*(cons)* eE]*(cons)* iI]*(cons)* oO]*(cons)* uU]*(cons)* (b) All strings of letters in which the letters are in ascending lexicographic order. (2 points) In the ascii character set we get A*B*C*D*E*F*G*H*I*J*K*L*M*N*O*P*Q*R*S*T*U*V*W*X*Y*Z*a*b*c*d*e*f*g*h*i*j* k*l*m*n*o*p*q*r*s*t*u*v*w*x*y*z* (c) Comments consisting of a string surrounded by /* and */ without an intervening */ unless it appears inside the quotes \ and ". (2 points) = (( ^\ ] j\: "j + ^=]) = (d) All strings of digits with no repeated digit. (2 points) The DFA for the digits 0,1,2:
b 0 a 1 2 1 d 0 c 2 2 1 2 0 1 e 0,1,2 0

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 regular expression: 1* j 1*00* j 1*00*10* 2

B) Problem 3.16 (8 points)


Construct nondeterministic nite automata for the following regular expressions using Algorithm 3.3. Show the sequence of moves made by each in processing the input string ababbab. Shaded states are accept states and `e' stands for in the gures. (a) (ajb)* (2 points)
e a e 0 e 1 e 4 b e 5 e 2 3 e 6 e 7

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

a b b b 0 !1 !2 ! 3 !6 ! 7 ! 8 ! 9 b! 10 ! 1 ! 2 a !3 !6 !7 ! 8 !7 ! a 8 ! 9 ! 10 ! 1 ! 2 ! 3 ! 6 ! 7 ! 8 ! 9 ! 10 ! 11 (d) (ajb)*abb(ajb)* (2 points)

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

C) Problem 3.17 (8 points)


Convert the NFA's in Exercise 3.16 into DFA's using Algorithm 3.2. Show the sequence of moves made by each in processing the input string ababbab. (a) (ajb)* (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 a b b b b 0 ! 1 ! 2 a !1 ! 2 ! 2 a !1 ! 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 a b b b b 0 ! 1 ! 2 a !1 ! 2 ! 2 a !1 ! 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 a b b b b 0 ! 1 ! 2 a !1 ! 2 ! 2 a !1 ! 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 a b b b b 0 ! 1 ! 2 a !1 ! 2 ! 3 a !3 ! 3

D) Problem 4.4 (10 points)


Consider the grammar R ! R0j0R j RR j R j (R) j a j b j c Note that the rst vertical bar is the \or" symbol, not a separator between alternatives. (a) Show that this grammar generates all regular expressions over the symbols a and b. (3 points) Note that the text's de nition of regular expressions excludes the empty set and . Proof: Let x be a re over a, b and c. Show x 2 L(G) by induction on jxj, i.e. the length of x. Base case: jxj = 1. The only re's of length 1 are a, b and c. Without loss of generality, assume x is a. Then R )*a because R ! a is a production of G. Inductive case: Assume all re's of length n are in L(G). Let jxj = n + 1. Then the form of x is either r1 jr2 or r1r2 , r* or (r), where each r1 , r2 or r is itself a re of length n. Then R ) RjR (by de nition of ) and R ! RjR in productions of G) R )*r1jR (by inductive hypothesis) R )*r1jr2 (by inductive hypothesis) So r1 jr2 2 L(G)

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