Compilers Spring 2013 PRACTICE Midterm Exm This is full length prctice midterm exm. If you wnt to tke it t exm pce, give yourself 7 minutes to tke the entire test. Just like the rel exm, ech question hs point vlue. There re 7 points in the exm, so tht you cn pce yourself to verge 1 point per minute (some prts will e fster, some slower). Questions: 1. SML [ points] 2. Regulr Lnguges [ points] 3. NFA to regulr expressions [10 points] 4. NFA to DFA [10 points]. Regulr expression to NFA [10 points] 6. Context Free Lnguges [ points] 7. LL Prsing [10 points] 8. LR Prsing [10 points] 9. Types [10 points] This is the solution set to the prctice exm. The solutions pper in lue oxes. 1
Question 1: SML [ pts] Prt A: Evlute the following SML expressions (1 point ech): 1. let vl = 4 vl = in if ( > ) then 1 else 2 end 2 2. let vl = 2 vl = let vl = 4 vl q = in + q end in + end 11 3. let fun f (0, ) = f (, ) = f (-1, +) in f (3, ) end 11 2
Prt B: Write down the type for ech of these SML functions (1 point ech): 1. fun f (x) = x ^ " " ^ x string string 2. fun f (x) = let fun g ([], ns) = ns g (::l,ns) = g(l, ::::ns) in g(x,[]) end list list 3
Question 2: Regulr Lnguges [ pts] 1. Write regulr expression for ll strings of s nd s which contins the sustring (2 point). ( ) ( ) 2. Write regulr expression for ll strings of xs nd ys where every y is immeditely followed y t lest 3 xs (2 points). (x (yxxx)) 4
Question 3: NFA to Regexp [10 pts] Convert the following NFA to regulr expression : 2 c 1 d 3 g e 4 f The regulr expression is ( d) ( ( cg) (e f) ). We derive it in 4 steps shown elow (note you cn remove sttes in different orders nd come up with different, equivlent nswers which re still correct). Step 0: Put in new strt stte with n ɛ trnsition to the old strt. Step 1: Remove sttes 3 nd 4. Step 2: Remove stte 2. This results in loop on 1 nd new edge from 1 to. Step 3: Collpse the prllel 1 edges into n OR. Step 4: Remove stte 1. The nswer is written on the remining edge
Step 0 2 c 0 1 d 3 g e 4 f Step 1 2 0 1 d cg ef Workspce for question 3 6
Step 2 * d 0 1 * cg ef Step 3 * d 0 1 ( * cg) (ef) Step 4 0 ( * d) * (( * cg) (ef)) Workspce for question 3 7
Question 4: NFA to DFA [10 pts] Convert the following NFA to DFA: 1 2 3 4 {2,4} {1} {1,3} {3} {2,3,4} 8
Question : Regexp to NFA [10 pts] Drw n NFA for the following regulr expressions: 1. ( c) d (2 points) 1 2 3 d 4 c 2. (c) (2 points) 1 2 3 c 9
3. ( ( ) c (d e) ( f g) ) h (6 points) 0 1 2 3 h c 7 d 4 e f g 6 10
Question 6: Context Free Lnguges [ pts] Write context free grmmrs for the following lnguges (your grmmr does not hve to e LR(1), LL(1) etc): 1. The lnguge descried y the regulr expression (() (c d)) (2 points). S E S ɛ E A c A d A A ɛ Note tht this ws derived y mking A which is (). Then mking E which is the contents of the outer str. Then mking S e zero or more repetitions of E. 2. Expressions consisting of num, +, nd *. You should write your grmmr so tht * hs higher precedence thn + (3 points): E E + T T T T * num num 11
Question 7: LL Prsing [10 pts] Consider the following grmmr: S -> S S c Q q Q -> Q m Which non-terminls (if ny) cn derive empty? (1 point) Q cn derive empty. Wht re the FIRST sets of Q nd S? (1 point). The FIRST set of Q is {m}. The FIRST set of S is {m,q,c} Wht re the FOLLOW sets of Q nd S? (1 point) The FOLLOW set of Q is {q,m}. The FOLLOW set of S is {,}. This grmmr cn not e prsed y n LL(0) or LL(1) prser. Explin why not (2 points). The grmmr is left recursive (in oth the rules S S S nd Q Q m). LL prsers cnnot hndle left recursion. Rewrite the grmmr so tht it ccepts the sme lnguge, ut cn e prsed y n LL(1) prser ( points). S c Stil Q q Stil Stil S Stil Q m Q 12
Question 8: LR Prsing [10 pts] Consider the following grmmr : 0: S -> X 1: X -> X c 2: X -> X X 3: X -> 1. Wht is Closure({X X. X)? (2 points) X X. X X. X c X. X X X. 2. Wht is Goto({X. X c}, X)? (2 points) Closure{X X. c} which is just X X. c 3. Show the execution of the prser on the string c. The stte mchine for the prser is provided long with tle for you to fill in on the next pge (6 points). 13
Using the grmmr from the previous pge: 0: S -> X 1: X -> X c 2: X -> X X 3: X -> And the stte mchine for tht grmmr: Stte c $ S X 1 s2 s3 g g4 2 s2 s3 g6 3 r3 r3 r3 r3 4 s2 s3 r0 g7 Acpt 6 s3 s8 g7 7 r2 r2 r2 r2 8 r1 r1 r1 r1 Fill in the tle to the right. In one line, show the current sttus of the prser the position in the input, the stte the prser is in, nd the contents of the stck. In the next line, show the ction tht the prser tkes. Then show the new sttus in the following line. Repet this process until the prser ccepts the input. The first two re done for you. 14 Input Stte Stck. c 1 Shift to stte 2. c 2 1 Shift to stte 3. c 3 1 2 Reduce y rule 3. c 2 1 X 2 Goto 6. c 6 1 X 2 Shift to stte 3. c 3 1 X 2 6 Reduce y rule 3. c 6 1 X 2 X 6 Goto 7. c 7 1 X 2 X 6 Reduce y rule 2. c 2 1 X 2 Goto 6. c 6 1 X 2 Shift to stte 8 c. 8 1 X 2 c 6 Reduce y rule 1 c. 1 X 1 Goto 4 c. 4 X 1 Reduce y rule 0 c. 1 S 1 Goto c. S 1 Accept
Question 9: Types [10 pts] 1. Show the typing derivtion for the Tiger sttement x := f(r.) + 3. You my ssume tht your initil environment (Γ 0 ) hs the following mppings (in ddition to the se Tiger environment): Γ 0 (x) = int Γ 0 () = int Γ 0 (r) = Record(:string, :int) Γ 0 (f) = string int Γ 0 r : Record(:string,...) Γ 0 f : string int Γ 0 r. : string Γ 0 f(r.) : int Γ 0 3 : int Γ 0 x : int Γ 0 f(r.) + 3 : int Γ 0 x := f(r.) + 3 : unit 2. Fill in the correct premises for the su-typing rule for function types. T 1 S 1 S 2 T 2 S 1 S 2 T 1 T 2 1