CCE 531, pring 2017, Midterm Exm Answer Key 1. (15 points) Using the method descried in the ook or in clss, convert the following regulr expression into n equivlent (nondeterministic) finite utomton: ( *c)* You my contrct -trnsitions provided the resulting utomton is equivlent. Answer: Any equivlent nswer etween the these two extremes is correct. c c There re other possile correct nswers. 2. (15 points) Using the sets-of-sttes pproch descried in clss, simulte the (nondeterministic) finite utomton given elow (in tulr form) on the input string w = cc. Tht is, for every prefix x of w (so x =,, c, c, c, cc, cc, cc in tht order), list ll sttes rechle from the strt stte y reding x. For ech prefix, list the sttes in incresing order without duplictes. Does the utomton ccept w? Why or why not? c 0 5 1, 2 1 3 2, 3 2 1 2 4 3 3, 6 0 4 7 4, 5 5 7 2, 4 6 6 0 6 7 3, 6 1
Answer: prefix sttes 0, 1, 2, 4 2, 3, 4, 5 c 0, 1, 2, 3, 4, 6, 7 (ll ut 5) c 1, 2, 3, 4, 5, 6, 7 (ll ut 0) c 0, 1, 2, 3, 4, 5, 6 (ll ut 7) cc 0, 1, 2, 3, 4, 6, 7 (ll ut 5) cc 1, 2, 3, 4, 5, 6, 7 (ll ut 0) cc 0, 1, 2, 3, 4, 5, 6 (ll ut 7) The input is rejected, ecuse the finl set of sttes does not contin n ccepting stte (i.e., stte 7). 3. (10 points) Give single flex-suitle regulr expression tht mtches ll Pscl-style comments. Tht is, your regex should mtch ll strings tht strt with (*, end with *), nd do not hve ny occurrence of *) in etween s sustring. For exmple, mtch not mtch -------------+------------------ (* hi *) (* hi *) (* ho *) (*********) (*) (*(*(* **) (* (* nest *) *) (*)))*(*) (* * Note tht opening nd closing delimiters cnnot overlp. Your nswer my include ny nmed suexpressions in curly rces, provided you define them fully s you would in the premle. You re NOT llowed to use the flex / opertor. Be s concise s possile. [Keep in mind tht the flex scnner will look for the longest mtch possile.] Answer: There re numer of correct nswers. To void confusion, I will define some nmed suexpressions, then list some equivlent ptterns. str "*" rpren ")" nonstr [^*] neither [^)*] open "(*" close "*)" %% {open}{nonstr}*{str}(({neither}{nonstr}*)?{str})*{rpren} {open}({nonstr} {str}+{neither})*{str}+{rpren} {open}{nonstr}*({str}({neither}{nonstr}*)?)*{close} {open}({nonstr} {str}+{neither})*{str}*{close} 2
There re other correct nswers. 4. (15 points totl) Here is grmmr with strt symol : () () (10 points) Using this grmmr, show complete prse tree yielding the string (((()))()()). () (5 points) Is this grmmr miguous? Explin. Answer: Here is prse tree tht does not use the second -production, tht is, it does not use. There re other prse trees for the sme string tht do use this production. ( ) ( ) ( ) ( ) ( ) ( ) The grmmr is miguous, ecuse nother string, (), is yielded y two different prse trees (equivlently, two different leftmost derivtions): () () () () () () () () 3
5. (25 points totl) Recll one of our stndrd, simplified grmmrs for rithmetic expressions, given in ycc/ison form (expr is the strt symol): expr : term expr + term expr - term term : fctor term * fctor term / fctor fctor : CONT VAR ( expr ) () (15 points) Alter to the grmmr so tht expressions cn involve nmed function clls. A nmed function cll hs the form f( ), where f is nme (VAR) nd is commseprted list of zero or more expressions (the rguments). Ech rgument cn e n ritrry expression. For exmple, the following re grmmticlly correct function cll expressions: foo() foo(r) r(t,ft) t(t+foo(6),t-foo*r)... Your ltered grmmr should still e unmiguous. () (10 points) Add semntic ctions to the originl, unltered grmmr ove so tht the root of the prse tree contins s its ttriute the sum of ll constnt vlues ppering in the expression. You cn ssume tht ll constnt vlues re integers. Also ssume tht the lexicl scnner sets the ttriute of ech CONT token to its vlue. If there re no constnts ppering in the expression, then the root ttriute should e 0. For exmple, if the input is 3*(42+z)+6, then the root should hve ttriute vlue 51 if the input is x-y+z, then the root should hve ttriute 0. Mke your ctions s simple s possile, nd do not use ny lirry routines. Any omitted ctions re ssumed to e the defult ction {$$ = $1}. 4
Answer: () We dd four productions to the grmmr ove, strting with fctor (the expr nd term productions re unchnged): fctor : CONT VAR ( expr ) VAR ( ) VAR ( rgs ) rgs : expr rgs, expr () Here is correct nswer: expr : term expr + term { $$ = $1 + $3 } expr - term { $$ = $1 + $3 } term : fctor term * fctor { $$ = $1 + $3 } term / fctor { $$ = $1 + $3 } fctor : CONT VAR { $$ = 0 } ( expr ) { $$ = $2 } Any missing ctions re defult ctions ($$ = $1). The first four ctions re identicl. 5