语法分析review学习.pptx
《语法分析review学习.pptx》由会员分享,可在线阅读,更多相关《语法分析review学习.pptx(42页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、自上而下分析 自上而下分析一般过程 LL(1)文法 预测分析程序 -递归下降分析实现-表驱动分析实现第1页/共42页自上而下分析一般过程In the top-down parsing,we begin with the start symbol and at each step,expand one of the remaining nonterminals by replacing it with the right side of one its productions.We repeat until only terminals remain.The top-down parse cre
2、ate a leftmost derivation of the sentence.第2页/共42页LL(1)文法The first“L”means we scan the input from left to right;the second“L”means we create a leftmost derivation;and the 1 means one input symbol of lookahead.A grammar G is LL(1)iff whenever A u|v are two distinct productions of G,the following cond
3、itions hold:-for no terminal a do both u and v derive strings beginning with a(i.e.first sets are disjoint)-at most one of u and v can derive the empty string-if v=*then u does not derive any string beginning with a terminal in Follow(A)第3页/共42页The first set of a sequence of symbols u,written as Fir
4、st(u)is the set of terminals whichstart all the sequences of symbols derivable from u.A bit more formally,consider allstrings derivable from u by a leftmost derivation.If u=*v,where v begins with someterminal,that terminal is in First(u).If u=*,then is in First(u).First 集第4页/共42页Follow 集The follow s
5、et of a nonterminal A is the set of terminal symbols that can appear immediately to the right of A in a valid sentential form.A bit more formally,for every valid sentential form S=*uAv,where v begins with some terminal,that terminal is in Follow(A).第5页/共42页计算First 集To calculate First(u)where u has t
6、he form X1X2.Xn,do the following:1.If X1 is a terminal,then add X1 to First(u),otherwise add First(X1)-to First(u).2.If X1 is a nullable nonterminal,i.e.,X1=*,add First(X2)-to First(u).Furthermore,if X2 can also go to ,then add First(X3)-and so on,through all Xn until the first nonnullable one.3.If
7、X1X2.Xn=*,add to the first set.第6页/共42页计算Follow 集For each nonterminal in the grammar,do the following:1.Place#in Follow(S)where S is the start symbol and#is the inputs right endmarker.The endmarker might be end of file,it might be newline,it might be a special symbol,whatever is the expected end of
8、input indication for this grammar.We will typically use#as the endmarker.2.For every production A uBv where u and v are any string of grammar symbols and B is a nonterminal,everything in First(v)except is placed in Follow(B).3.For every production A uB,or a production A u Bv where First(v)contains (
9、i.e.v is nullable),then everything in Follow(A)is added to Follow(B).第7页/共42页预测分析程序Predictive parser is a non-backtracking top-down parser.A predictive parser is characterized by its ability to choose the production to apply solely on the basis of the next input symbol and the current nonterminal be
10、ing processed.To enable this,the grammar must take a particular form,that is,a grammar LL(1).第8页/共42页递归下降分析技术The first technique for implementing a predictive parser is called recursive-descent.A recursive descent parser consists of several small functions(procedures),one for each nonterminal in the
11、 grammar.As we parse a sentence,we call the functions(procedures)that correspond to the left side nonterminal of the productions we are applying.If these productions are recursive,we end up calling the functions recursively.第9页/共42页表驱动分析技术 Another method for implementing a predictive parser is calle
12、d table-driven parsing that uses a table,called parse table,to store that production along with an explicit stack to keep track of where we are in the parse.We push the start symbol on the stack and read the first input token.As the parser works through the input,there are the following possibilitie
13、s for the top stack symbol X and the input token nonterminal a:(1)If X=a and a=end of input(#):parser halts and parse completed successfully.(2)If X=a and a!=#:successful match,pop X and advance to next input token.This is called a match action.(3)If X!=a and X is a nonterminal,pop X and consult tab
14、le at X,a to see which production applies,push right side of production on stack.This is called a predict action.(4)If none of the preceding cases applies or the table entry from step 3 is blank,there has been a parse error第10页/共42页分析表的构造1.For each production A u of the grammar,do steps 2 and 32.For
15、 each terminal a in First(u),add A u to MA,a3.If in First(u),(i.e.A is nullable)add A u to MA,b for each terminal b in Follow(A),If in First(u),and#is in Follow(A),add A u to MA,#4.All undefined entries are errors第11页/共42页自下而上分析 自上而下分析一般过程 移进归约模式 LR 分析技术 补充:算符优先分析技术第12页/共42页自下而上分析一般过程A bottom-up par
16、se works in reverse.We begin with the sentence of terminals and each step applies a production in reverse,replacing a substring that matches the right side with the nonterminal on the left.We continue until we have substituted our way back to the start symbol.If you read from the bottom to top,the b
17、ottom-up parse prints out a rightmost derivation of the sentence.第13页/共42页移进归约模式Bottom-up parsing algorithms are in general more powerful than top-down methods,but not surprisingly,the constructions required in these algorithms are also more complex.It is difficult to write a bottom-up parser by han
18、d for anything but the most trivial of grammars,but fortunately,there are excellent parser generator tools like yacc that build a parser from an input specification.Shift-reduce parsing is the most commonly used and most powerful of the bottom-up techniques.第14页/共42页LR parsers(“L”for left to right s
19、can of input;“R”for rightmost derivation)are efficient,tabledriven shift-reduce parsers.An LR parser uses two tables:1.The action table Actions,a tells the parser what to do when the state on top of the stack is s and terminal a is the next input token.The possible actions are to shift a state onto
20、the stack,to reduce the handle on top of the stack,to accept the input,or to report an error.2.The goto table Gotos,X indicates the new state to place on top of the stack after a reduce of the non-terminal X while state s is on top of the stack.The two tables are usually combined,with the action tab
21、le specifying entries for terminals,and the goto table specifying entries for non-terminals.LR技术第15页/共42页There are three types of LR parsers:LR(k),simple LR(k),and lookahead LR(k)(abbreviated to LR(k),SLR(k),LALR(k).The k identifies the number of tokens of lookahead.We will usually only concern ours
22、elves with 0 or 1 tokens of lookahead,but it does generalize to k 1.The different classes of parsers all operate the same way(as shown above,being driven by their action and goto tables),but they differ in how their action and goto tables are constructed,and the size of those tables.Types of LR pars
23、ers第16页/共42页LR(0)configuration or item.A configuration is a production of the grammar with a dot at some position on its right side.1.Construct C=I0,I1,.In,the collection of configurating sets for G.2.State i is determined from Ii.The parsing actions for the state are determined as follows:a)If A u
24、is in Ii then set actioni,a to reduce A u for all input.(A not equal to S).b)If S S is in Ii then set actioni,#to accept.c)If A uav is in Ii and GO(Ii,a)=Ij,then set actioni,a to shift j(a is a terminal).3.The goto transitions for state i are constructed for all non-terminals A using the rule:If GO(
25、Ii,A)=Ij,then goto i,A=j.4.All entries not defined by rules 2 and 3 are errors.5.The initial state is the one constructed from the configurating set containing S S.Constructing LR(0)parsing tables第17页/共42页A grammar is LR(0)if the following two conditions hold for each configurating set:For any confi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语法分析 review 学习
限制150内