Syntax Analysis - LR(0) Parsing.ppt
《Syntax Analysis - LR(0) Parsing.ppt》由会员分享,可在线阅读,更多相关《Syntax Analysis - LR(0) Parsing.ppt(21页珍藏版)》请在麦多课文档分享上搜索。
1、Syntax Analysis - LR(0) Parsing,66.648 Compiler Design Lecture (02/04/98) Computer Science Rensselaer Polytechnic,Lecture Outline,LR(0) Parsing Algorithm Parse Tables Examples Administration,LR(k) Parsing Algorithms,This is an efficient class of Bottom-up parsing algorithms. The other bottom-up pars
2、ers include operator precedence parsers.The name LR(k) means: L - Left-to-right scanning of the input R - Constructing rightmost derivation in reverse k - number of input symbols to select a parser action,Yet Another Example,Consider a grammar to generate all palindromes. 1) S P 2) P a Pa 3) P b P b
3、 4) P c LR parsers work with an augmented grammar in which the start symbol never appears in the right side of a production. In a given grammar, if the start symbol appears in the RHS, we can add a production S S (S is the new start symbol and S was the old start symbol),STACK INPUT BUFFER ACTION $
4、abcba$ shift $a bcba$ shift $ab cba$ shift $abc ba$ reduce $abP ba$ shift $abPb a$ reduce $aP a$ shift $aPa $ reduce $P $ reduce $S $ accept,Example Cont.,Qn: How to select parser actions (namely shift, reduce, accept and error)? Ans: 1) By constructing a DFA that encodes all parser states, and tran
5、sitions on terminals and nonterminals. The transitions on terminals are the parser actions( also called the action table) and transitions on nonterminals resulting in a new state (also called the goto table). 2) Keeping a stack to simulate the PDA. This stack maintains the list of states.,LR(0) Pars
6、ers,LR(0) Items and Closure,LR(0) parser state needs to capture how much of a given production we have scanned . LR(0) parser (like a FSA) needs to know how much the production (on the rhs) we have scanned so far. For example: in the production: P a P a An LR(0) item is a production with a mark/dot
7、on the RHS. SO the items for this production will be P . a P a , P a . P a, P a P. a, P aPa.,Items and Closure Contd,Intuitively, there is a derivation (or we have seen the input symbols) to the left of dot. Two kinds of items, kernel items and nonkernel items - Kernel and nonkernel items. Kernel It
8、ems - Includes initial item S .S and all items in which dot does not appear at the left most position. Nonkernel Items- All other items which have dots at the leftmost position.,Closure of Items,Let I be the set of items. Then Closure (I) consists of the set of items that are constructed as follows:
9、 1) Every item I is also in the Closure(I) - reflexive 2 If A -. alpha . B beta is in Closure(I), and B gamma is production, then add the item B .gamma also in the Closure(I), if it is not already a member. Repeat this until no more items can be added.,Intuition,Closure represents an equivalent stat
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- SYNTAXANALYSISLR0PARSINGPPT
