編譯原理龍書(shū)課后部分答案(英文版)_第1頁(yè)
編譯原理龍書(shū)課后部分答案(英文版)_第2頁(yè)
編譯原理龍書(shū)課后部分答案(英文版)_第3頁(yè)
編譯原理龍書(shū)課后部分答案(英文版)_第4頁(yè)
編譯原理龍書(shū)課后部分答案(英文版)_第5頁(yè)
已閱讀5頁(yè),還剩4頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶(hù)提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、1) What is the difference between a compiler and an interpreter? A compiler is a program that can read a program in one language - the source language - and translate it into an equivalent program in another language the target language and report any errors in the source program that it detects dur

2、ing the translation process. Interpreter directly executes the operations specified in the source program on inputs supplied by the user.2) What are the advantages of:(a) a compiler over an interpretera. The machine-language target program produced by a compiler is usually much faster than an interp

3、reter at mapping inputs to outputs.(b) an interpreter over a compiler?b. An interpreter can usually give better error diagnostics than a compiler, because it executes the source program statement by statement.3) What advantages are there to a language-processing system in which the compilerproduces

4、assembly language rather than machine language?The compiler may produce an assembly-language program as its output, becauseassembly language is easier to produce as output and is easier to debug.4.2.3 Design grammars for the following languages:a) The set of all strings of 0s and 1s such that every

5、0 is immediately followed by at least 1.S - SS | 1 | 01 | e4.3.1 The following is a grammar for the regular expressions over symbols a and b only, using + in place of | for unions, to avoid conflict with the use of vertical bar as meta-symbol in grammars:rexpr - rexpr + rterm | rtermrterm - rterm rf

6、actor | rfactorrfactor - rfactor * | rprimaryrprimary - a | ba) Left factor this grammar.rexpr - rexpr + rterm | rtermrterm - rterm rfactor | rfactorrfactor - rfactor * | rprimaryrprimary - a | bb) Does left factoring make the grammar suitable for top-down parsing?No, left recursion is still in the

7、grammar.c) In addition to left factoring, eliminate left recursion from the original grammar.rexpr - rterm rexprrexpr - + rterm rexpr | erterm - rfactor rtermrterm - rfactor rterm | erfactor - rprimary rfactorrfactor - * rfactor | erprimary - a | bd) Is the resulting grammar suitable for top-down pa

8、rsing?Yes.Exercise 4.4.1 For each of the following grammars, derive predictive parsers and show the parsing tables. You may left-factor and/or eliminate left-recursion from your grammars first. A predictive parser may be derived by recursive decent or by the table driven approach. Either way you mus

9、t also show the predictive parse table.a) The grammar of exercise 4.2.2(a). 4.2.2 a) S - 0S1 | 01 This grammar has no left recursion. It could possibly benefit from left factoring. Here is the recursive decent PP code.s() match(0);if (lookahead = 0)s();match(1);OrLeft factoring the grammar first:S -

10、 0SS - S1 | 1s() match(0); s();s() if (lookahead = 0)s(); match(1);elsematch(1);Now we will build the PP tableS - 0SS - S1 | 1First(S) = 0First(S) = 0, 1Follow(S) = 1, $Follow(S) = 1, $Non-TerminalInput Symbol01$SS-0SSS-S1S-1The predictive parsing algorithm on page 227 (fig4.19 and 4.20) can use thi

11、s table for non-recursive predictive parsing.b) The grammar of exercise 4.2.2(b).4.2.2 b) S - +SS | *SS | a with string +*aaa.Left factoring does not apply and there is no left recursion to remove.s() if(lookahead = +)match(+); s(); s();else if(lookahead = *)match(*); s(); s();else if(lookahead = a)

12、match(a);elsereport(“syntax error”);First(S) = +, *, aFollow(S) = $, +, *, aNon-TerminalInput Symbol+*a$SS- +SSS-*SSS-aThe predictive parsing algorithm on page 227 (fig4.19 and 4.20) can use this table for non-recursive predictive parsing.5.1.1 a, b, c: Investigating GraphViz as a solution to presen

13、ting trees5.1.2:Extend the SDD of Fig. 5.4 to handle expressions as in Fig. 5.1:1. L - E N1. L.val = E.syn2. E - F E1. E.syn = E.syn2. E.inh = F.val3. E - + T Esubone1. Esubone.inh = E.inh + T.syn2. E.syn = Esubone.syn4. T - F T1. T.inh = F.val2. T.syn = T.syn5. T - * F Tsubone1. Tsubone.inh = T.inh

14、 * F.val2. T.syn = Tsubone.syn6. T - epsilon1. T.syn = T.inh7. E - epsilon1. E.syn = E.inh8. F -digit1. F.val =digit.lexval9. F - ( E )1. F.val = E.syn10. E - T1. E.syn = T.syn5.1.3 a, b, c: Investigating GraphViz as a solution to presenting trees5.2.1:What are all the topological sorts for the depe

15、ndency graph of Fig. 5.7?1. 1, 2, 3, 4, 5, 6, 7, 8, 92. 1, 2, 3, 5, 4, 6, 7, 8, 93. 1, 2, 4, 3, 5, 6, 7, 8, 94. 1, 3, 2, 4, 5, 6, 7, 8, 95. 1, 3, 2, 5, 4, 6, 7, 8, 96. 1, 3, 5, 2, 4, 6, 7, 8, 97. 2, 1, 3, 4, 5, 6, 7, 8, 98. 2, 1, 3, 5, 4, 6, 7, 8, 99. 2, 1, 4, 3, 5, 6, 7, 8, 910. 2, 4, 1, 3, 5, 6, 7

16、, 8, 95.2.2 a, b: Investigating GraphViz as a solution to presenting trees5.2.3: Suppose that we have a production A - BCD. Each of the four nonterminals A, B, C, and D have two attributes:sis a synthesized attribute, andiis an inherited attribute. For each of the sets of rules below, tell whether (

17、1) the rules are consistent with an S-attributed definition (2) the rules are consistent with an L-attributed definition, and (3) whether the rules are consistent with any evaluation order at all?a) A.s = B.i + C.s1. No-contains inherited attribute2. Yes-From above or from the left3. Yes-L-attribute

18、d so no cyclesb) A.s = B.i + C.s and D.i = A.i + B.s1. No-contains inherited attributes2. Yes-From above or from the left3. Yes-L-attributed so no cyclesc) A.s = B.s + D.s1. Yes-all attributes synthesized2. Yes-all attributes synthesized3. Yes-S- and L-attributed, so no cyclesd) A.s = D.i B.i = A.s

19、+ C.s C.i = B.s D.i = B.i + C.i1. No-contains inherited attributes2. No-B.i uses A.s, which depends on D.i, which depends on B.i (cycle)3. No-Cycle implies no topological sorts (evaluation orders) using the rules5.3.1:Below is a grammar for expressions involving operator+and integer or floating-poin

20、t operands. Floating-point numbers are distinguished by having a decimal point.1. E - E + T | T2. T -num . num|numa)Give an SDD to determine the type of each term T and expression E.1. E - Esubone + T1. E.type = if (E.type =float| T.type =float) E.type =float else E.type =integer2. E - T1. E.type =

21、T.type3. T -numsubone . numsubtwo1. T.type =float4. T -num1. T.type =integerb)Extend your SDD of (a) to translate expressions into postfix notation. Use the binary operatorintToFloatto turn an integer into an equivalent float.Note: I use character , to separate floating point numbers in the resultin

22、g postfix notation. Also, the symbol | implies concatenation.1. E - Esubone + T1. E.val = Esubone.val | , | T.val | +2. E - T1. E.val = T.val3. T -numsubone . numsubtwo1. T.val =numsubone.val | . |numsubtwo.val4. T -num1. T.val =intToFloat(num.val)5.3.2Give an SDD to translate infix expressions with

23、 + and * into equivalent expressions without redundant parenthesis. For example, since both operators associate from the left, and * takes precedence over +, (a*(b+c)*(d) translates into a*(b+c)*d.Note: symbol | implies concatenation.1. S - E1. E.iop =nil2. S.equation = E.equation2. E - Esubone + T1

24、. Esubone.iop = E.iop2. T.iop = E.iop3. E.equation = Esubone.equation | + | T.equation4. E.sop = +3. E - T1. T.iop = E.iop2. E.equation = T.equation3. E.sop = T.sop4. T - Tsubone * F1. Tsubone.iop = *2. F.iop = *3. T.equation = Tsubone.equation | * | F.equation4. T.sop = *5. T - F1. F.iop = T.iop2. T.equation = F.equation3. T.sop = F.sop6. F -char1. F.equation =char.lexval2. F.sop =nil7. F - ( E )1. if (F.iop = * & E.sop = +) F.equation = ( | E.equation | ) else F.equation = E.equation 2. F.sop =nil5.3.3:Give an SDD to differentiate expressio

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶(hù)所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶(hù)上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶(hù)上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶(hù)因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論