CS 6353 Compilr Construction, Homwork #1 1. Writ rgular xprssions for th following informally dscribd languags: a. All strings of 0 s and 1 s with th substring 01*1. Answr: (0 1)*01*1(0 1)* b. All strings of 0 s and 1 s with an odd numbr of 0 s and odd numbr of 1 s. Answr: vn*( 0 vn*1 1 vn* 0 ) vn* vn* = (00 11)*((01/10)(00 11)*(01/10)(00 11)*)* as shown in nots 2. Considr. Answr th following DFA rlatd qustions. Whn constructing DFA, thr is no nd to show your construction stps, but you nd to informally stat how you gt th DFAs. a. Construct a DFA that accpts (a b)* xcpt for a*b*b. b. Basd on th tchniqus you us in (a) and (b), can you com up with a DFA construction algorithm for th xcpt for typ of languags? Stp 1: (Optional) Construct NFA accpting th languag corrsponding to th xcpt for part. Stp 2: Convrt NFA to DFA Stp 3: Rvrs stats, i.., changing original accpt stats to rjct stats and original rjct stats to accpt stats.
3. Considr th rgular xprssion aac* b(a b)c* dfind on. a. Construct th NFA for th rgular xprssion. You can dirctly draw th NFA without going through th RE-to-NFA stps. b. Convrt th NFA to DFA. You nd to show th convrsion stps. c. Minimiz th DFA. You nd to show th minimization stps.
4. A tokn rcognizr is dsignd to handl th following tokns, whr. T1 = aab T2 = bcc* T3 = bcd* T4 = aabcd* a. Construct a minimizd DFA for tokn rcognition and th tokns ar dfind as follows. Th DFA should spcify th spcific tokn nams (T1, T2, ) it accpts at th corrsponding final stats. You do not nd to show th stps for th construction if you can draw th DFA dirctly. Not that th longst matching and first matching ruls for ambiguity rsolution should b usd.
b. Excut your DFA to procss th following string to idntify tokns. List vry tokn string and its tokn nam. Also, dscrib all th backtracking actions takn during th procss. aabaabcdddbcbccccaabbcddd aab aabcdddbcbccccaabbcddd aab aabcddd bcbccccaabbcddd -- got tokn aab(t1) -- got tokn aabcddd (T4) aab aabcddd bc bccccaabbcddd aab aabcddd bc bcccc aabbcddd -- got tokn bc(t2) -- got tokn bcccc(t2) aab aabcddd bc bcccc aab bcddd aab aabcddd bc bcccc aab bcddd -- got tokn aab(t1) -- got tokn bcddd (T3) c. Crat th lx dfinition fil for th tokn rcognizr. Us lx to gnrat th scannr program from th dfinition fil. Aftr compiling th scannr, gnrat input strings to tst it. Attach th print out for th dfinition fil, th input string, and th output of th scannr. dfition fil: %{ #includ <studio.h> %} %% aab {printf("tokn: T1; string = %s.\n", yytxt);} bcc* {printf("tokn: T2; string = %s.\n", yytxt);} bcd* {printf("tokn: T3; string = %s.\n", yytxt);}
aabcd* {printf("tokn: T4; string = %s.\n", yytxt);} %% Input string: aabaabcdddbcbccccaabbcddd Output of th scannr: Tokn: T1; string = aab. Tokn:T4; string = aabcddd. Tokn: T2; string = bc. Tokn: T2; string = bcccc. Tokn: T1; string = aab. Tokn: T3; string = bcddd. 5. Considr th languag: All strings of 0 s and 1 s such that vry 0 is immdiatly followd by at last on 1. (a) Construct a DFA for th languag. 1 0 0 1 1 (b) Convrt th DFA to a rgular grammar (following th convrsion algorithm). S 0 = 1S 0 0S 1 S 1 = 1S 0 (c) As w discussd in class, th grammar you constructd in (a) is a rgular grammar (typ 3 grammar). Th following grammar G (G is not a rgular grammar) is also for th sam languag. Show a pars tr for th string 1011101. S -> BA A -> 01BA B -> 1B
S B A 1 B 0 1 B A 1 B 0 1 B A 1 B (d) Giv th lftmost drivation for (b) from G. () Giv th rightmost drivation for (b) from G. (f) How many diffrnt drivations from G can th string 1011101 has? Brifly driv your answr. S AB ( 01BA ( ( 011B 0111B 0111) ( 01BA ( 01 ε)) )) ( 1B 1) a b c d f g h i j Equivalnt to xcution of: a { b { ( c d ) ( f ( g h )) } } ( i j ) Simplifid to: a { b { ( cd ) ( f ( g h )) } } ( ij ) Numbr of diffrnt ordrs in xcuting (g h) = C(2,1) = 2 --.g., hg or gh Numbr of diffrnt ordrs in xcuting f (g h) = 2 -- squntial xcution has no altrnativ Numbr of diffrnt ordrs in xcuting (cd fgh) = C(6,3) = 20 --.g., cfdgh, cdfgh, fgcdh,
Numbr of diffrnt ordrs in xcuting (cd f (g h)) = C(6,3) = 20 * 2 = 40 Lt X = b { ( c d ) ( f ( g h )) } and Xk is on way to xcut X Numbr of diffrnt ordrs in xcuting Xk ij = C(9,2) = 36 Numbr of diffrnt ordrs in xcuting X ij = 36 * 40 = 1440 -- X has 40 xcution ordrs Numbr of diffrnt ordrs in xcuting a { X ij } is still 1440 So, th answr is 1440 6. Considr th following languag: Th languag consists of strings of som numbrs of 0 s, 1 s, and * s. Th strings hav th sam numbr of 0 s and 1 s, and at any point in a string, thr ar qual numbr of 0 s and 1 s or mor 0 s than 1 s. Zro or mor * s can appar anywhr in a string. For xampl, 00011*1, 001*0**1011, 01, ******, and ar in th languag, but 01001110* is not in th languag. Construct a contxt fr grammar for th languag. Mak sur th grammar you giv is not ambiguous. 7. Considr th following grammar dfind ovr S -> 0S11 S -> S1 S -> (a) Show that this grammar is ambiguous by giving a string that can b parsd in two diffrnt ways and showing th two corrsponding pars trs. Th pars tr for string 0011111 can b th following:
S S 0 S 1 1 0 S 1 1 S 1 0 S 1 1 0 S 1 1 S 1 (b) Rwrit th grammar to liminat th ambiguity. S -> 0E11 E-> E1 8. Construct a typ-0/1 grammar for th languag, whr can b any string of a s and b s. For xampl abcab is in th languag but abcba is not. Basic concpt: gnrat δ c δ, which is contxt fr and asy to do. Thn, switch δ to δ. Us R to mark th rvrs mov, F th forward mov, and E th nd of string S X R E X a X A b X B c First mov th last nontrminal backward, us R to mark it A A R A R A A B R B R A B A R A R B B B R B R B
Till th last nontrminal rachs a trminal, now it is at th corrct position for th nontrminal, chang it to a trminal, chang R to F so that F can latr b movd forward c A R c a F c B R c b F a A R a a F a B R a b F b A R b a F b B R b b F F nds to b movd to th nd, th ordr of th nontrminals do not chang F A A A F A F A B A F B F B A B F A F B B B F B Whn F movs to th nd, bfor th last trminal, switch and chang it to R F A E A R E F B E B R E Trmination, should b don whn only on trminal lft a A R E a a -- could also trminat on y F Y E, but won t b symmtric with c Y R E a B R E a b b A R E b a b B R E b b c A R E c a - whn w is of lngth 1 c B R E c b c R E c - whn w is an mpty string