Bottom-up parsing.ppt
《Bottom-up parsing.ppt》由会员分享,可在线阅读,更多相关《Bottom-up parsing.ppt(31页珍藏版)》请在麦多课文档分享上搜索。
1、Bottom-up parsing,Goal of parser : build a derivation Top-down parser : build a derivation by working from the start symbol towards the input. Builds parse tree from root to leaves Builds leftmost derivation Bottom-up parser : build a derivation by working from the input back toward the start symbol
2、 Builds parse tree from leaves to root Builds reverse rightmost derivation,Bottom-up parsing,The parser looks for a substring of the parse trees frontier. .that matches the rhs of a production and .whose reduction to the non-terminal on the lhs represents on step along the reverse of a rightmost der
3、ivation Such a substring is called a handle. Important: Not all substrings that match a rhs are handles.,Bottom-up parsing techniques,Shift-reduce parsing Shift input symbols until a handle is found. Then, reduce the substring to the non-terminal on the lhs of the corresponding production. Operator-
4、precedence parsing Based on shift-reduce parsing. Identifies handles based on precedence rules.,Example: Shift-reduce parsing,1. S E 2. E E + E 3. E E * E 4. E num 5. E id,Input to parse: id1 + num * id2,Accept,$ S,Reduce (rule 1),$ E,Reduce (rule 2),$ E + E,Reduce (rule 3),$ E + E * E,Reduce (rule
5、5),$ E + E * id2,Shift,$ E + E *,Shift,$ E + E,Reduce (rule 4),$ E + num,Shift,$ E +,Shift,$ E,Reduce (rule 5),$ id1,Shift,$,ACTION,Grammar:,Handles: underlined,STACK,Shift-Reduce parsing,A shift-reduce parser has 4 actions: Shift - next input symbol is shifted onto the stack Reduce - handle is at t
6、op of stack pop handle push appropriate lhs Accept - stop parsing & report success Error - call error reporting/recovery routine,Shift-Reduce parsing,How can we know when we have found a handle? Analyze the grammar beforehand. Build tables Look ahead in the input LR(1) parsers recognize precisely th
7、ose languages in which one symbol of look-ahead is enough to determine whether to reduce or shift. L : for left-to-right parse of the input R : for reverse rightmost derivation 1: for one symbol of lookahead,How does it work?,Read input, one token at a time Use stack to keep track of current state T
8、he state at the top of the stack summarizes the information below. The stack contains information about what has been parsed so far. Use parsing table to determine action based on current state and look-ahead symbol. How do we build a parsing table?,LR parsing techniques,SLR (not in the book) Simple
9、 LR parsing Easy to implement, not strong enough Uses LR(0) items Canonical LR Larger parser but powerful Uses LR(1) items LALR (not in the book) Condensed version of canonical LR May introduce conflicts Uses LR(1) items,Class examples,L,R,id,L,* R,L,R,S,L = R,S,S,S,Finding handles,As a shift/reduce
10、 parser processes the input, it must keep track of all potential handles. For example, consider the usual expression grammar and the input string x+y. Suppose the parser has processed x and reduced it to E. Then, the current state can be represented by E +E where means that an E has already been par
11、sed and that +E is a potential suffix, which, if found, will result in a successful parse. Our goal is to eventually reach state E+E, which represents an actual handle and should result in the reduction EE+E,LR parsing,Typically, LR parsing works by building an automaton where each state represents
12、what has been parsed so far and what we hope to parse in the future. In other words, states contain productions with dots, as described earlier. Such productions are called items States containing handles (meaning the dot is all the way to the right end of the production) lead to actual reductions d
13、epending on the lookahead.,SLR parsing,SLR parsers build automata where states contain items (a.k.a. LR(0) items) and reductions are decided based on FOLLOW set information. We will build an SLR table for the augmented grammar,SS S L=R S R L *R L id R L,SLR parsing,When parsing begins, we have not p
14、arsed any input at all and we hope to parse an S. This is represented by SS. Note that in order to parse that S, we must either parse an L=R or an R. This is represented by SL=R and SR closure of a state: if AaBb represents the current state and B is a production, then add B to the state. Justificat
15、ion: aBb means that we hope to see a B next. But parsing a B is equivalent to parsing a , so we can say that we hope to see a next,SLR parsing,Use the closure operation to define states containing LR(0) items. The first state will be:From this state, if we parse, say, an id, then we go to state If,
16、after some steps we parse input that reduces to an L, then we go to state,S S S L=R S R L *R L id R L,L id ,S L =R R L ,id,SLR parsing,Continuing the same way, we define all LR(0) item states:,S S S L=R S R L *R L id R L,L id ,S L =R R L ,S S ,I0,I1,I2,I3,S R ,I4,L * R R L L id L * R,I5,S,L,*,id,R,S
17、 L= R R L L *R L id,I6,=,R,S L=R ,R L ,L,L,I7,id,I3,*,*,L *R ,R,I8,I9,SLR parsing,The automaton and the FOLLOW sets tell us how to build the parsing table: Shift actions If from state i, you can go to state j when parsing a token t, then slot i,t of the table should contain action “shift and go to s
18、tate j“, written sj Reduce actions If a state i contains a handle A, then slot i, t of the table should contain action “reduce using A“, for all tokens t that are in FOLLOW (A). This is written r(A) The reasoning is that if the lookahead is a symbol that may follow A, then a reduction A should lead
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- BOTTOMUPPARSINGPPT
