版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1) What is the difference between a compiler and an interpreter一theA compiler is a program that can read a program in one language - the sourcethat it detects duringlanguage - and translate it into an equivalent program in another language target language and report any errors in the source program
2、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
3、 at mapping inputs to outputs.(b) an interpreter over a compiler(c) 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 assem
4、bly language rather than machine languageThe compiler may produce an assembly-language program as its output, because assembly 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
5、immediately followed by at least 1.S - SS | 1 | 01 |4.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 | r
6、factorr factor - rfactor * | rprimary r primary - a | ba) Left factor this grammar.rexpr - rexpr + rterm | rterm rterm - rterm rfactor | rfactor r factor - rfactor * | rprimary rprimary - a | bb) Does left factoring make the grammar suitable for top-down parsing No, left recursion is still in the gr
7、ammar.c) In addition to left factoring, eliminate left recursion from the original grammar.rexpr -rterm rexpr rexpr - +rterm rexpr |rterm -rfactor rtermrterm-rfactor rterm |rfactor -rprimary rfactor,r factor -* rfactor |r primary -a | bd) Is the resulting grammar suitable for top-down parsing Yes.pa
8、rseleftExercise 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 a
9、lso show the predictive table.a) The grammar of exercise 4.2.2(a).4.2.2 a) S - 0S1 | 01This grammar has no left recursion. It could possibly benefit from factoring. Here is the recursive decent PP code.s() match( 0);i f (lookahead =0)s();match( 1);OrLeft factoring the grammar first:S - 0SS- S1 | 1s(
10、) match( 0 ); s (); s () i f (lookahead = 0)s(); match( 1 ); elsematch( 1 );9Now we will build the PP tableS-0SS - S1 | 1First(S) = 0First(S ) = 0,1Follow(S) = 1, $Follow(S ) = 1, $Non-TermiInput Symbolnal01$SS-0SS,S -S1S -1The predictive parsing algorithm on page 227 and can use this table for non-
11、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() i f(lookahead =+)match( +,); s(); s();else i f(lookahead =*)match( * ); s(); s();else if(lookahead =a)match( a);els
12、ereport(syntax error );First(S) = +, *, aFollow(S) = $, +, *, aNon-Termi nalInput Symbol+*a$SS- +SSS-*SSS-aThe predictive parsing algorithm on page 227 and can use this table for non-recursive predictive parsing.5.1.1 a, b, c: Investigating GraphViz as a solution to presenting treesExtend the SDD of
13、 Fig. to handle expressions as in Fig.:1. L - E N1. =2. E -FE1. = E.syn2. E.inh =3. E -+TEsubone1. Esubone.inh = E.inh +2. E.syn = Esubone.syn4. T - F T1. T.inh =2. = T.syn5. T - * F Tsubone1. Tsubone.inh = T.inh *2. T.syn =Tsubone.syn6. T - epsilon1. T.syn =T.inh7. E - epsilon1. E.syn =E.inh8. F -d
14、igit1. =9. F -(E )1. =10. E -T1. =5.1.3 a, b, c: Investigating GraphViz as a solution to presenting treesWhat are all the topological sorts for the dependency graph of Fig.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
15、,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 treesSuppose that we have a production A - BCD. Each of the four nonterminals A,B, C, and D have two attributes:s is a synthesized attribute, an
16、di is aninherited 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) = +1. No-contains in
17、herited attribute2. Yes-From above or from the left3. Yes-L-attributed so no cyclesb) = + and = +1. No-contains inherited attributes2. Yes-From above or from the left3. Yes-L-attributed so no cyclesc) = +1. Yes-all attributes synthesized2. Yes-all attributes synthesized3. Yes-S- and L-attributed, so
18、 no cyclesd) =+ =+1. No-contains inherited attributes2. uses , which depends on , which depends on (cycle)3. No-Cycle implies no topological sorts (evaluation orders) using the rulesBelow is a grammarfor expressions involving operator + and integer or floating-point operands. Floating-point numbers
19、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. = if = float | = float ) = float else = integer 2. E - T1. =3. T -numsubone . numsubtwo1. =float4. T -egerb) Extend your
20、 SDD of (a) to translate expressions into postfix notation. Use the binary operator intToFloat to turn an integer into an equivalentI use character , to separate floating point numbers in the resulting postfix notation. Also, the symbol | implies concatenation.1. E - Esubone + T1. = | , | | +2. E -
21、T1. =3. T -numsubone . numsubtwo1. =| . |4. T -num1. =intToFloat5.3.2 Give 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. =nil2. =2. E - Esubone + T1. =2. =3. =|+|4. =+3. E - T1. =2. =3. =4. T - Tsubone * F1. =*2. =*3. =|*|4. =*
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 统计与成对数据的统计分析课件-2026届高三数学二轮复习
- 海南护师考试试题及答案
- 2026年湖南中考试卷及答案英语
- 2026三年级数学上册 乘法解决问题
- 2026三年级数学上册 测量的实际操作
- 保安员岗位责任制度
- 中国水利知识试题及答案
- 企业园区保安岗位制度
- 人福危险辨识与风险评价、控制制度
- 小组讨论奖惩制度及细则
- 山东高考英语语法单选题100道及答案
- 职业道德与法治知识点总结中职高教版
- 2025年绿色低碳先进技术示范工程实施方案-概述及范文模板
- 2025上半年广西现代物流集团社会招聘校园招聘149人笔试参考题库附带答案详解
- 事故后企业如何进行危机公关与赔偿管理
- 2025年春新人教PEP版英语三年级下册全册教案
- OptixOSN3500智能光传输设备业务配置手册
- 【浙科综合实践】四上第四课项目一、美味的中秋月饼
- 深圳大学《算法设计与分析》2021-2022学年期末试卷
- 西泠印社三年级下册书法教案
- 大客户经理招聘笔试题及解答
评论
0/150
提交评论