版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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 + | -te
2、rm 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
3、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 fo
4、r 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
5、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 t
6、ree 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.
7、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 t
8、he 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
9、-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 t
10、he 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-
11、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
12、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
13、 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
14、, 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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 构建心理安全净土护航健康成长小学二年级主题班会课件
- 小学教科版(2024)1.电和我们的生活教案设计
- 龙船调教学设计初中音乐人音版七年级下册-人音版
- 原2017春七年级地理下册 第六章 第一节 位置和范围教案 (新版)新人教版
- 四川省南充白塔中学八年级体育 第二十周 韵律体操与舞蹈、任选教材教学设计
- 湖北省宜都市外国语学校初中八年级信息技术下册 第二章 第五节《神奇的遮罩效果-FLASH遮罩动画的运用》说课稿 华东师大版
- 市场营销策略制定六步法实战手册
- 订单调整确认通知函(4篇范文)
- 重要客户流失挽回处理销售总监预案
- 物理必修第一册第一章运动的描述2时间位移第二课时教案
- 2026年中国电科集团面试常见问题含答案
- 火力发电厂劳动安全和工业卫生设计规程
- 不锈钢质量合同范本
- 2025至2030兽用器械发展趋势分析与未来投资战略咨询研究报告
- (正式版)DB42∕T 2401-2025 《生物防火隔离带建设技术规范》
- DB4401∕T313-2025 地理标志产品 派潭凉粉草
- 城配物流知识培训课件
- 【课件】工作危害分析法(JHA)专项培训课件丨
- 2024年陕西延长石油集团招聘笔试真题
- 《湖南省房屋建筑和市政工程消防质量控制技术标准》
- 2024年建筑三类人员考试题库(多选题)
评论
0/150
提交评论