




已阅读5页,还剩1页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
The exercises of Chapter Three3.2 Given the grammar AAA|(A)|a. Describe the language it generates;b. Show that it is ambiguous.Solution:a. Generates a string of balanced parenthesis, including the empty string.b. parse trees of ():AAAA()A()A3.3 Given the grammarexp exp addop term | termaddop + | -term term mulop factor| factormulop *factor ( exp ) | numberWrite down leftmost derivations, parse trees, and abstract syntax trees for the following expression:a. 3+4*5-6b. 3*(4-5+6)c. 3-(4+5*6)Solution:a. The leftmost derivations for the expression 3+4*5-6:Exp = exp addop term =exp addop term addop term =term addop term addop term= factor addop term addop term=3 addop term addop term = 3 + term addop term=3+term mulop factor addop term =3+factor mulop factor addop term=3+4 mulop factor addop term = 3+4* factor addop term=3+4*5 addop term = 3+4*5-term= 3+4*5-factor=3+4*5-63.5 Write a grammar for Boolean expressions that includes the constants true and false, the operators and, or and not, and parentheses. Be sure to give or a lower precedence than and and and a lower precedence that not and to allow repeated nots, as in the Boolean expression not not true. Also be sre your grammar is not ambiguous.solutionbexpbexp or A | AA A and B | BB not B | CC (bexp) | true | falseEx: not not true boolExp A B not B not not B not not C not not true 3.8 Given the following grammar statementif-stmt | other | if-stmt if ( exp ) statement else-partelse-part else statement | exp 0 | 1a. Draw a parse tree for the stringif(0) if (1) other else else otherb. what is the purpose of the two elses?The two elses allow the programmer to associate an else clause with the outmost else, when two if statements are nested and the first does not have an else clause.c. Is similar code permissible in C? Explain.The grammar in C looks like:if-stmtif ( exp ) statement | if (exp) statement else statementthe way to override “dangling else” problem is to enclose the inner if statement in s. e.g. if (0) if(1) other else other.3.10 a. Translate the grammar of exercise 3.6 into EBNF. b. Draw syntax diagramms for the EBNF of part (a).Solutiona. The original grammarlexp atom|listatomnumber|identifierlist (lexp-seq)lexp-seq lexp-seq lexp| lexp The EBNF of the above grammar:lexp atom|listatomnumber|identifierlist (lexp-seq)lexp-seq lexp lexpb. The syntax diagramms for the above EBNF:atomlist lexpNUM atomIDE)lexp-seq(listlexplexp-seqlexp3.12. Unary minuses can be added in several ways to the simple arithmetic expression grammar of Exercise 3.3. Revise the BNF for each of the cases that follow so that it satisfies the stated rule.a. At most one unary minus is allowed in each expression, and it must come at the beginning of an expression, so -2-3 is legal ( and evaluates to -5 ) and -2-(-3) is legal, but -2-3 is not.exp exp addop term | termaddop + | -term term mulop factor| factormulop *factor ( exp ) | (-exp) | number | b. At most one unary minus are allowed before a number or left parenthesis, so -2-3 is legal but -2 and -2-3 are not.exp exp addop term | termaddop + | -term term mulop factor| factormulop *factor ( exp ) | -(exp) | number | -numberc. Arbitrarily many unary minuses are allowed before numbers and left parentheses, so everything above is legal.3.19 In some languages ( Modula-2 and Ada are examples), a procedure declaration is expected to be terminated by syntax that includes the name of the procedure. For example, in Modular-2 a procedure is declared as follows:PROCEDURE P;BEGINEND P;Note the use of the procedure name P alter the closing END. Can such a requirement be checked by a parser? Explain.AnswerThis requirement can not be handled as part of the grammar without making a new rule for each legal variable name, which makes it intractable for all practical purposes, even if variable names are restricted to a very short length. The parser will just check the structure, that an identifier follows the keyword PROCEDURE and an identifier also follows the keyword END, however checking that it is the same identifier is left for semantic analysis. See the discussion on pages 131-132 of your text. 3.20 a. Write a regular expression that generate the same language as the following grammar:A aA|B|
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025年安徽省高校毕业生三支一扶计划招募工作政策问答考前自测高频考点模拟试题完整参考答案详解
- 2025江苏海安经济技术开发区西场办事处招聘公益性岗位人员4人模拟试卷及答案详解(各地真题)
- 2025年甘肃财贸职业学院考核招聘博士研究生模拟试卷及完整答案详解一套
- 2025年宁波慈溪市中西医结合医疗健康集团招聘派遣制工作人员3人考前自测高频考点模拟试题及答案详解(历年真题)
- 2025年春季中国石油高校毕业生招聘模拟试卷带答案详解
- 2025年新能源汽车电池管理系统在电动高尔夫球场中的应用研究报告
- 新能源市场2025年需求变化趋势预测与产品调整创新分析报告
- 兴业协议书费
- 问卷网协议书
- 2025年新能源行业企业生产效率提升与流程优化报告
- 中考英语过去将来时趣味讲解动态课件(43张课件)
- 人工智能基础与应用(第2版)全套教学课件
- MOOC 跨文化交际通识通论-扬州大学 中国大学慕课答案
- 高血压与气温的关系
- 赛题 模块一 职业素养测试-2023年全国职业院校技能大赛拟设赛项赛题
- 有害物质管理培训课件
- GB/T 33363-2016预应力热镀锌钢绞线
- GB/T 23510-2009车用燃料甲醇
- 实用英语口语900句
- 食品安全事故流行病学个案调查表
- 风机运行记录表
评论
0/150
提交评论