欢迎来到麦多课文档分享! | 帮助中心 海量文档,免费浏览,给你所需,享你所想!
麦多课文档分享
全部分类
  • 标准规范>
  • 教学课件>
  • 考试资料>
  • 办公文档>
  • 学术论文>
  • 行业资料>
  • 易语言源码>
  • ImageVerifierCode 换一换
    首页 麦多课文档分享 > 资源分类 > PPT文档下载
    分享到微信 分享到微博 分享到QQ空间

    Syntax Analysis - LR(0) Parsing.ppt

    • 资源ID:389540       资源大小:70.50KB        全文页数:21页
    • 资源格式: PPT        下载积分:2000积分
    快捷下载 游客一键下载
    账号登录下载
    微信登录下载
    二维码
    微信扫一扫登录
    下载资源需要2000积分(如需开发票,请勿充值!)
    邮箱/手机:
    温馨提示:
    如需开发票,请勿充值!快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。
    如需开发票,请勿充值!如填写123,账号就是123,密码也是123。
    支付方式: 支付宝扫码支付    微信扫码支付   
    验证码:   换一换

    加入VIP,交流精品资源
     
    账号:
    密码:
    验证码:   换一换
      忘记密码?
        
    友情提示
    2、PDF文件下载后,可能会被浏览器默认打开,此种情况可以点击浏览器菜单,保存网页到桌面,就可以正常下载了。
    3、本站不支持迅雷下载,请使用电脑自带的IE浏览器,或者360浏览器、谷歌浏览器下载即可。
    4、本站资源下载后的文档和图纸-无水印,预览文档经过压缩,下载后原文更清晰。
    5、试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。

    Syntax Analysis - LR(0) Parsing.ppt

    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

    10、e - all the possible ways that you could have reached that state. Example: I = S .P Closure (I) = S.P,P.aPa,P.bPb,P.c In Arithmetic Expression: S.E closure(I)= ,GOTO Operation,Let I be the set of items and let X be a grammar symbol (nonterminal/terminal). Then GOTO(I,X) = Closure(A alpha X.beta| A a

    11、lpha . X beta is in I) It is a new set of items moving a dot over X. Intuitively, we have seen either an input symbol (terminal symbol) or seen a derivation starting with that nonterminal.,Canonical set of Items (states),Enumerate possible states for an LR(0) parser. Each state is a canonical set of

    12、 items. Algorithm: 1) Start with a canonical set, Closure(S.S) 2) If I is a canonical set and X is a grammar symbol such that I=goto(I,X) is nonempty, then make I a new canonical set (if it is not already a canonical set). Keep repeating this until no more canonical sets can be created. The algorith

    13、m terminates!.,Example,S0: S .P , P .a P a, P .bP b, P.c S1: S P. S2: P a.Pa, P.aPa,P.bPb,P.c S3:P b.P b, P.aPa,P.bPb,P.c S4: P c. S5: P aP.a S6:P bP.b S7: P aPa. S8: P bP b.,Finite State Machine,Draw the FSA. The major difference is that transitions can be both terminal and nonterminal symbols.,Key

    14、 Idea in Canonical states,If a state contains an item of the form A beta ., then state prompts a reduce action (provided the correct symbols follow). If a state contains A alpha . delta, then the state prompts the parser to perform a shift action (of course on the right symbols). If a state contains

    15、 S S. and there are no more input symbols left, then the parser is prompted to accept. Else an error message is prompted.,Prasing Table,state Input symbol gotoa b c $ P 0 s2 s3 s4 2 1. acc 2. s2 s3 s4 5 3. s2 s3 s4 6 4. r3 r3 5. s7 6. s8 7. r1 r1 r1 r1 8. r2 r2 r2 r2,Parsing Table Contd,si means shi

    16、ft the input symbol and goto state I. rj means reduce by jth production. Note that we are not storing all the items in the state in our table.example: abcba$ if we go thru, parsing algorithm, we get,Example Contd,State input action $S0 abcba$ shift $S0aS2 bcba$ shift $S0aS2bS3 cba$ shist $S0aS2bS3cS

    17、4 ba$ reduce,Shift/Reduce Conflicts,An LR(0) state contains aconflict if its canonical set has two items that recommend conflicting actions. shift/reduce conflict - when one item prompts a shift action, the other prompts a reduce action. reduce/reduce conflict - when two items prompt for reduce acti

    18、ons by different production.A grammar is said be to be LR(0) grammar, if the table does not have any conflicts.,LALR Grammar,Programming languages cannot be generated by LR(0) grammar. We usually have a look ahead symbol, to deteremine what kind of action parser will be prompted for. These lookaheads refine states and actions.,Comments and Feedback,Project 2 will be in the web by Friday (this). Please keep reading chapter 4 and understand the material. Work out as many exercises as you can.,


    注意事项

    本文(Syntax Analysis - LR(0) Parsing.ppt)为本站会员(deputyduring120)主动上传,麦多课文档分享仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知麦多课文档分享(点击联系客服),我们立即给予删除!




    关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1 

    收起
    展开