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

  1. 1、本文档共6页,可阅读全部内容。
  2. 2、本文档付费后,不意味着付费购买了版权,只能用于单位或个人使用,不得用于商业用途(如:【转卖】进行直接盈利和【编辑后售卖】进行间接盈利)。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:19108035856(电话支持时间:9:00-19:00)。
编译原理与实践第四章答案

The exercises of Chapter Four 4.2 Grammar: A → ( A ) A | ε Assume we have lookahead of one token as in the example on p. 144 in the text book. Procedure A() if (LookAhead() ∈ {‘(‘}) then Call Expect(‘(‘) Call A() Call Expect (‘)’) Call A() else if (LookAhead()∈ {‘)‘, $}) then return() else /* error */ fi fi end 4.3 Given the grammar statement→ assign-stmt|call-stmt|other assign-stmt→identifier:=exp call-stmt→identifier(exp-list) [Solution] First, convert the grammar into following forms: statement→ identifier:=exp | identifier(exp-list)|other Then, the pseudocode to parse this grammar: Procedure statement Begin Case token of ( identifer : match(identifer); case token of ( := : match(:=); exp; ( (: match((); exp-list; match()); else error; endcase (other: match(other); else error; endcase; end statement 4.7 a Grammar: A → ( A ) A | ε First(A)={(,ε } Follow(A)={$,)} 4.7 b See theorem on P.178 in the text book First{(}∩First{ε}=Φ ε∈Fist(A), First(A) ∩Follow(A)= Φ both conditions of the theorem are satisfied, hence grammar is LL(1) 4.9 Consider the following grammar: lexp→atom|list atom →number|identifier list→(lexp-seq) lexp-seq→lexp, lexp-seq|lexp a. Left factor this grammar. b. Construct First and Follow sets for the nonterminals of the resulting grammar. c. Show that the resulting grammar is LL(1). d. Construct the LL(1) parsing table for the resulting grammar. e. Show the actions of the corresponding LL(1) parser, given the input string (a,(b,(2)),(c)). [Solution] a. lexp→atom|list atom →number|identifier list→(lexp-seq) lexp-seq→lexp lexp-seq’ lexp-seq’→, lexp-seq|ε b. First(lexp)={number, identifier, ( } First(atom)={number, identifier} First(list)={( } First(lexp-seq)={ number, identifier, ( } First(lexp-seq’)={, , ε} Follow(lexp)={, $, } } Follow(atom)= {, $, } } Follow(list)= {, $, } } Follow(lexp-seq)={$, }

您可能关注的文档

文档评论(0)

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

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

认证主体张**

相关文档

相关课程推荐