




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 during 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 interpreter 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 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 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 rfactor | 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 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 parsing?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 must 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 - 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 this 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)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 presenting 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 * 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 dependency 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, 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 (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-attributed 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 + 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-point 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 = 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 resulting 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 + 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. 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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 演艺创新创业项目商业计划书
- 职场生存挑战真人秀创新创业项目商业计划书
- 农产品加工机械安全性能提升创新创业项目商业计划书
- 社区物流配送创新创业项目商业计划书
- 精密电子开关自动化生产企业制定与实施新质生产力项目商业计划书
- 老年人行走辅助机器人行业跨境出海项目商业计划书
- DB37T 4908-2025公路钢结构桥梁制造技术规范
- 五年级读书分享卡绘制
- 水疗产品知识培训总结课件
- 2025年网络安全工程师综合实力考核试题及答案解析
- 城乡燃气管道维护保养技术方案
- 2025年西藏公开遴选公务员笔试试题及答案(A类)
- 水土保持治理工应急处置考核试卷及答案
- 工业园区储能项目商业计划书
- 急性重症胰腺炎个案护理
- 带式电磁除铁器检修作业指导书
- 生命体征测量操作流程及评分标准
- 美术学院 本科培养方案 - 清华大学 - Tsinghua University
- 部编版道德与法治小学四年级上册同步配套教案(全册)
- 突发环境事件危险废物专项应急预案
- 财务收支记账表
评论
0/150
提交评论