Вы находитесь на странице: 1из 4
Time 2Hr Compiler DesignMa41101/MA61011 Autumn2012 Instruction: There are 25 questions on 4 printed sides. There are 2 pages. Each page is printed on both sides. Every question carries 2 marks. There is no fractional marking, In every question you will get either 0 mark or 1 mark or 2 marks. In case of any doubt or mistake any query should not be made. In case of wrong question full marks will be awarded later. ‘Use of calculators is not permitted. Rough work involved in the question must be shown, However final answer should be written in a box. If a question has two possible answers then both of them should be written in the same box. Rough work must be shown along with the answer. No rough ‘work should be done on the question paper. On the question nothing other than the name and the roll number should be written. If rough work is not shown then you shall get 0 marks even if the answer is correct, 1, Grammat: Syj2y—paP,gQyh Pye7—one P29—otkp Qyy—Pzn (Start symbol S) ('n’ isa single digit number) Parsing of a6egz4h is a6cgz4h—>aP3g24h—>aP 3gQi2h > S37 Parsing of atkpgz7h is_atkpgzTh—> aP9g27h > aP 9gQ2h—> S71 Grammar: S2.1715-PaP,2 Pastix—PNCQut Qoy—zn Show parsing of a6ez9tg 2. What are missing, and Rule Setting | Output missing: in the given syntax — [ $y do{S}while(id,T+E rule Ts+E,+E5 is applied then output is [pT f+s. What is the output when T;+ Ey—>Es Toba is applied? Tose 4. An expression is either (A) anumber or (B) number— expression or (C) expression + number On the basis of above rules write an expression (smallest size, involving only + and ~ ), which can not be generated Hint: 5-4 + 3 has two possible generations: E+Et—n-Etnn-nin and = E-n-£-yn-E+nm-nin 5. What is the problem with the following grammar? Write answer in a box using less than 8 letters. S—Pid; P-—+Pid, PT id, T-rint T-float T-schar (start symbol S) Example: The problem with the following grammar is that float ykyexj can also be generated. It will be considered as an answer in 13 letters, STG; G-ridG Grid Ge T-rint T-rfloat T—>char (start symbol S) 6. In grammar S—>aTrefgjypt T->ghKcF K-—uv F-xy the reduction of “uv” into K is made when the next symbol is ‘c’. What should be the next symbol so that “xy” can be reduced to F? Write answer of this question in a box. The box is very important in this question. If there are more than one possible symbols then write all of them in the same box. P.T.O. 7, In simple LR (SLR) parser a state has 3 items { G—+miUkHmj U—.dUy U->.dGr }, Write thie state when transition is made on ‘d"? That state has 6 items. Write all six in the same box. Example: Let an state has 4 items { Y-»tk.ZkHmj Z—>.pKb Z-».mfYe Z-».pekHmj } ‘When transition is made on ‘p’ the state is {Z—>p.Kb Z—p.gkHmj K-.Tje T>.mj) 8. Ina grammar let state no.5 be the state of a canonical LR parser after processing string “abcd”. (A)In state 5 if ‘w’ is observed then “ed” is reduced to “K”. (B)In state 5 if ty’ is observed then “bed” is reduced to “M”. Write the grammar in less than 25 letters. Example: In a grammar let state no. 27 be the state of a Canonical LR (and LALR) parser after processing string “pqrs”. (A) In state 27 of Canonical LR parser if ‘c’ is observed then “rs” is reduced to “2”. (B)In state 27 of LALR parser if'c’, ‘y’ or “q’ is observed then “rs” is reduced to “Z”. ‘The grammar (less than 16 letters) is So paZeZyZq Zor say2 exykxyuxymxyfS 9. Let S-raTgIkTuTmTf T->xy (Start symbol S) —saeTrgansys kx yuxymxyfS be a (incomplete) grammar. Parsing of the string sagTyggTokioxiyaux ymxy CS axygxykaxyuxymxyf using some LR parser is saeTrgsTokioTiuioxiyzm x yf shown. What is the error report in the parsing o saeTrgsTokioT iunaT ammaxsya fS string axyw when same parser is used? “4 sagTageTokioTriuiaTismmiaT she sSi78 accept inta,b,p.q.r.tu: 10. Optimize the given program. In the scanf{a,b); optimized program t=a*b; is replaced by pratb; tp;. No change in any other instruction -pt67; should be made. pratb; =p*76; 11. Write equivalent program for (A) using only one (+). No (if) should be used. (Example: The equivalent program for (B) is (C).] Program (B) Program (C) scanf("%d",&x); i xExt5; if (x<80) while (x<80) xext5; xext13; while (x<80) printf("%d\n",x); xext17; . }while(x<80); printf("%d\n",x); printf("%d\n",x); 12.Let a, b, c,d, e and f be operators. To decompose (under left most doing) the expression Sa7b9cBd4e619 the operators are observed in the order abcbdbaefe. Write the highest precedence operator. [Example: Let operators are observed in the order abcdedcbfba hence the highest precedence operator is ‘e’). The method is as following: a b © d e d € b f ba acb(eagain) [bend ‘The answer of this question must be written in a box. The box is very important. (Same box) Mixed Address Code Tnstruction Operation Toad b Put *b” in the accumulator Koad b Put ‘b’ in the register Out Print the value of the accumulator Store a Store the value of the accumulator in the memory “a” ‘Add b ‘Accumulator=accumulator + °b* Sub abe abe Mul Accumulator=accumulator*register Divab ararb Here ‘a’ stands for some memory (x.y oF 2). Here ‘b’ and ‘c’ stand for some memory (x,y,z) or some number. Output of few programs is given. Find output of the remaining. Load 17 | Sub x,60,7 [Load 30 [Load 45 | Sub x,88,20 | Load 768 | Load 79 | Load 79 Add29 | Load 100 | Store x | Koad 12 | Divx,10 | Subx,88,20 | Koad 20 | Koad 20 Out ‘Add x, Load 42 | Mul Load 768 Out Mul out Koad 5 | Out Storey | Storex | Addx Divx2 — | out add 23 Mul Divx6 | Divx18 | Out Add x add 23 | Mul Out Divyx | Out Out Our | Out Loady | Addx Out Out 46,230 _| 153 84 ‘540,570 | missing; | missingy | missings | missings 13, Find missing:. 14, Find missing. 15, Find missings. 16, Find missing. Example: Translation of few expressions (in mixed address code) are given as following. Se _ [12-8 31rd [45+12 | 9-(6r4) 34(8-6) +4) _| G-6)*87 Load 53| Sub x12,8 | Load 31 [Lond 45 | Load 6 ‘Sub x,8,6 Sub x,3,6 Add 71 | Load x Koad 4 | Store x | Koad 4 Divx4 - | Load x Out Out Mul Div x12 | Mul Load 3 Koad 87 Out Load x | Store x Add x Mul Out Subx9x | Out Out Load x Out (27 G-A) | (4S TIYHOD) [2H 7T1) | CDHCIA-M1)_[ 9116) | GOT Subx,12,7 | Load 14 Load6 | Load 14 | Load 6 Load3 | Load 3 Load x Store x Koad2 | Storex | Koad 2 Add6 | Add 6 Subx,34 | Divx,71 Mut — | Divx,71 | Mul Storex | Koad 91 Koad x Store x Storex | Storex | Subx,14,71 | Koad x | Mut Mul Load 6 Load 14 | Load 6 | Add x Load 91 | Out Out Koad 2 Storey | Koad2 | Out Mul Mul Divy,71 | Mul Out Store y Storey | Add x Load x Loadx | Out Add y Add y Out ou | Similarly write translation of each of following in mixed address code. 17. Translate 73+(4*7) 19. Translate (7*4)*9 18, Translate 17 (27+4) 20, Translate (27-(482-174)) P.T.O. Translation of Functions (Using Activation Record) tion record is used to store memory ofa | Function ‘Translation function, Here a function(t) and its translation | int tGnt x,int y) are shown. Let us assume that the activation | { int p,q; record is as following [R+4]=[R+2}+[R+3]*29 R Re _[R+2 [R¥3 [Rv4 [Re [R+5]=[R+4]*[R+2}-1R+3] Retum}Retum |x y |p |q [RIFIR+5]*12 goto [R+1] value | address bo 21, Write the translation of given function ‘m’. int m(int p, int g, int k) Assume that the activation record is following. { int wz; RIR+H[R+2[R+3 [TR+4_ [R45 [R46 +8; ple |[k |Rewm |Reum|z |w | w=gtk+77;, address | value retum 2-w; J 22.A function “and its int w(int x, int y, translation are given. { int p; What is the activation — p=x+k-y+z; [R+6]>[R#5] +[R+IHR+4] +{R43] record? return p [RHI=IR+6] goto [R] } Following is the translation of.a function under a given acti R RH [R+2]R+3 [R+4[ R+5[Ri6[ R47] Ri8_ [R19 Rel Retum{Retum [ce fw |[t |p jm b | Retum [Return | Second | First value_| address |__| vatue_| address | argument | argument Memory R to R+7 is the activation record of function ‘s’. From R+8 onwards the activation record {oF function ‘k’ starts, The size of activation record for function ‘s' is (2+3+3). The size of activation record for function ‘k’ is (2+2+the number of local variables in the function *k’). int int, int w, int c) | s: {int bmp; battw; prk(o-b,123)+88; [R+10}-123 [R#9]-L1 R=R+8- gotok LER-R-8 —_ [R+5]=[R+8]+88 m=p*(b+6); [R+6]=[R+5]*(R+7]+6) return m*t; [RIF[IR+6}*(R+4] goto [R+1] i 23. Write the translation of given function ‘ int h(int a, int b) ‘Assume that the activation record is following. { int ed R[R+I[R#2 [R+3_ [R+4[R+5[R+G [R47 [RS c=atb; a[b |Retum|Retum [dc | First | Return | Return d=q(o*71)*43; value _| address argument | value _| address return d-69; 24. The size of the activation record for inta(intx, inty, intz) int b(inte, int g, int f, int w) function ‘a’ is 12. What is the size of the {int p,q[5],r; { int h,k[564],t,u,c; activation record for the function *b”? . } } 25.In some function the translation of — [R+87]=[R+47] [R48 g-2(c)*(u-d) is written. In the same function T:R=R-87 — [R+59]=[R+89]*(R+73]}{R+29)) write the translation of c=u+(d+219)-g.

Вам также может понравиться