版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 expressions such as x * (3*x +
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 西建大工业设计概论讲义第10讲 工业设计教育
- 2025-2026学年观察.花 教学设计
- 室外管网综合施工方案
- 屋面岩棉板保温层施工方案
- 2025年江苏省昆山市高考物理强基计划试卷(轻巧夺冠)附答案详解
- 2026年甘肃省玉门市高考物理模拟预测考试卷及一套完整答案详解
- 2026年湖南省汨罗市高考物理周测试卷附答案详解(能力提升)
- 2025年青海省德令哈市高考物理二模考试卷附答案详解(预热题)
- 2025年河南省林州市高考物理一模考试卷及答案详解【名师系列】
- 2026年山东省禹城市高考物理一模模拟卷及参考答案详解(精练)
- 2026年人教大同版(新教材)小学英语四年级下册期末学情测试卷及答案
- 哈尔滨工业大学2026年强基计划综合面试+体质测试模拟试题及答案解析
- 2026年小学生暑期安全教育课件(详细版)
- 顾问服务合同协议书
- 海洋工程综合管线测量技术方案
- 班级管理与心理辅导知到智慧树章节测试课后答案2024年秋河南大学
- 隋唐史学习通超星期末考试答案章节答案2024年
- 人教版三年级《语文下册》期末试卷(下载)
- 2024年北京市中考英语试卷真题(含答案)
- (正式版)JBT 14581-2024 阀门用弹簧蓄能密封圈
- 安全员B证考试资料
评论
0/150
提交评论