编译原与实践 第三章 答案.doc

  1. 1、本文档共7页,可阅读全部内容。
  2. 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
编译原与实践 第三章 答案

The exercises of Chapter Three 3.2 Given the grammar A→AA|(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 (): AA A A A A ( ) ε ε A( A ( ) ε A 3.3 Given the grammar exp ? exp addop term | term addop ? + | - term ? term mulop factor| factor mulop ? * factor ? ( exp ) | number Write down leftmost derivations, parse trees, and abstract syntax trees for the following expression: a. 3+4*5-6 b. 3*(4-5+6) c. 3-(4+5*6) [Solution]: 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-6 3.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 not’s, as in the Boolean expression not not true. Also be sre your grammar is not ambiguous. [solution] bexp→bexp or A | A A→ A and B | B B→ not B | C C→ (bexp) | true | false Ex: not not true boolExp → A → B → not B → not not B → not not C → not not true 3.8 Given the following grammar statement→if-stmt | other | ε if-stmt→ if ( exp ) statement else-part else-part→ else statement | ε exp→ 0 | 1 a. Draw a parse tree for the string if(0) if (1) other else else other b. what is the purpose of the two else’s? The two else’s 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-stmt→if ( e

文档评论(0)

ybcm963 + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档