lecture04-Top-Down Parsing I.pptx

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

Lecture 4 Top-Down Parsing Where We Are Lexical Analysis Syntax Analysis Semantic Analysis IR Generation IR Optimization Code Generation Optimization Source Code Machine Code Review from Last Time Goal of syntax analysis: recover the intended structure of the program. Idea: Use a context-free grammar to describe the programming language. Given a sequence of tokens, look for a parse tree that generates those tokens. Recovering this syntax tree is called parsing. Different Types of Parsing Top-Down Parsing Beginning with the start symbol, try to guess the productions to apply to end up at the users program. Bottom-Up Parsing Beginning with the users program, try to apply productions in reverse to convert the program back into the start symbol Top-Down Parsing int + ( int + int ) T E T E T T E E Challenges in Top-Down Parsing Top-down parsing begins with virtually no information. Begins with just the start symbol, which matches every program How can we know which productions to apply? In general, we cant. There are some grammars for which the best we can do is guess and backtrack if were wrong If we have to guess, how do we do it? Parsing as a Search An idea: treat parsing as a graph search Each node is a sentential form (a string of terminals and nonterminals derivable from the start symbol). There is an edge from node α to node β iff α ? β. Parsing as a Search E T T+E int int + E T+T+E T+T (E) int + T int + (E) (T) (T + E) … … … … int + T + E T + (E) int + int … … … … … E T T+E int int + E T+T+E T+T (E) int + T int + (E) (T) (T + E) … … … … int + T + E T + (E) int + int … … … … … Parsing as a Search Our First Top-Down Algorithm Breadth-First Search Maintain a worklist of sentential forms, nitially just the start symbol S. While the worklist isnt empty: Remove an element from the worklist. If it matches the target string, youre done. Otherwise, for each possible string that can be derived in one step, add that string to the

文档评论(0)

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

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

1亿VIP精品文档

相关文档