LL文法及其分析程序PPT课件_第1页
LL文法及其分析程序PPT课件_第2页
LL文法及其分析程序PPT课件_第3页
LL文法及其分析程序PPT课件_第4页
LL文法及其分析程序PPT课件_第5页
已阅读5页,还剩58页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、11.语法分析概念2.自上而下的语法分析的一般过程3.3.自上而下的语法分析面临的问题4. 开始符号集5. 后跟符号集6.select集7.LL(1)文法第1页/共63页21。语法分析在语言的编译实现中,把句子分析的过程称为语法分析,即完成这个任务的程序称为语法分析程序或称为识别程序。分析算法又称识别算法。从左到右的分析算法,即总是从左到右地识别输入符号串,首先识别符号串中的最左符号,进而依次识别右边的一个符号,直到分析结束。第2页/共63页3(上下文无关文法)句型的分析句型分析就是识别一个符号串是否为某文法的句型的过程,或者说是某个推导的构造过程。第3页/共63页4语法树推导的几何表示 句型

2、aabbaa的可能推导序列和语法树 例例: GS:SaASASbAASSSaAba S a A S S b A a a b aSaASaAaaSbAaaSbbaaaabbaaSaASaSbASaabASaabbaSaabbaaSaASaSbASaSbAaaabAaaabbaa第4页/共63页5分析算法分类分析算法可分为:自上而下分析法:从文法的开始符号出发,寻找与输入符号串匹配的推导,或者说,为输入串寻找一个最左推导。自下而上分析法:从输入符号串开始,逐步进行归约,直至归约到文法的开始符号。 第5页/共63页6两种方法反映了语法树的两种构造过程。自上而下方法是从文法符号开始,将它做为语法树的根

3、,向下逐步建立语法树,使语法树的结果正好是输入符号串自下而上方法则是从输入符号串开始,以它做为语法树的结果,自底向上的构造语法树第6页/共63页72。自上而下分析方法 对任何输入串,试图用一切可能的办法,从文法开始符号着手,自上而下地为输入串构造一棵语法树,或者说,为输入串寻找一个最左推导。本质上是一个试探过程,反复使用不同地产生式谋求匹配输入串的过程。 第7页/共63页8自上而下的语法分析的一般过程例:文法G: S cAd A ab A a识别输入串w=cabd是否为该文法的句子SSScAdcAd a b推导过程:推导过程:S cAd cAd cabd第8页/共63页9自上而下的语法分析的一

4、般过程(1)S cAd (2) A ab (3) A a 识别输入串w=cad是否为 该文法的该文法的句子句子1.S cAd 2.后选择(2)扩展A,得到推导S cAd cabd这时这时 w的第二个符号可以与叶子结点a得以匹配,但第三个符号d却不能与下一叶子结点b匹配怎么办?怎么办?-查看A有无另一个选择,有!回溯回溯,把A为根的子树剪掉,扫描过的输入串中的a吐出来,再试探用产生式(3)构造推导S cAd cad识别输入串识别输入串w=caa的的过程过程: 1.S cAd2.选择(2)扩展A,得到推导S cAd cabd3.回溯回回溯回到推导S cAd 4.选择(3)扩展A,得到推导S cAd

5、 cad5. A没有选择了!回溯回溯到推导S cAd 6.再回溯回溯S有无另一个选择?没有! 宣告分析失败。(请思考(请思考 若有 (4) S cB (5) B aa 会怎会怎样样? )第9页/共63页10自上而下分析的进一步讨论自上而下分析也称面向目标的分析方法,也就是从文法的开始符号出发企图推导出与输入的符号串完全匹配的句子,若能构造出推导则表明输入串是给定文法的句子,否则表明该输入不是给定文法的句子。自上而下分析对文法的要求文法不能含有左递归规则。自上而下分析又可分为确定的和不确定的两种 不确定的分析方法称为带回溯的分析方法,这种方法实际上是一种穷举的试探方法 确定的分析方法需对文法有一

6、定的限制第10页/共63页113。自上而下的语法分析面临的问题-实现考虑 回溯 文法的左递归性 SSa第11页/共63页12自上而下分析对文法的要求例文法G0S: (1) SSa (2) Sb 分析baa是不是文法的句子按照自上而下的分析思想选用产生式(1)来推导SSa 语法树末端结点最左符号为非终结符,所以选用(1)继续推导SSaSaa 此时语法树末端结点最左符号仍为非终结符,所以选用(1)继续推导SSaSaa Saaa 问题试图用S匹配输入串时,出现:在没有读入任何输入符 号的情况下,又得重新要求S去进行新的匹配.无法确定什么时候使用(2)产生式最适当,只能采用带回溯的不确定方法解决。原因

7、文法含有左递归。第12页/共63页13回溯的原因例GS: SxAy Aaba 若当前输入串为xay,首先构造的推导SxAy 匹配 进一步推导对A可选择Aab替换,得SxAy xaby xay xaby 匹配 xa都已匹配,当前面临输入符为y与b不能匹配,所以将输入串指针退回到a,对A的替换重新选用下一个产生式Aa进行试探, SxAy xay输入串中当前符a得到匹配,指针向前移动到y,与语法树中y匹配,匹配成功。由于相同左部的产生式的右部开始符号相同而引起回溯。第13页/共63页14在自上而下的分析方法中如何选择使用哪个产生式进行推导?假定要被代换的最左非终结符号是B,且有n条规则:BA1|A2

8、|An,那么如何确定用哪个右部去替代B?-什么信息用于Parser做正确选择?(输入串,文法特点)第14页/共63页15可预测的试探推导过程例 文法GS: S pA |qB A cAd| |a B d B | |c 识别输入串w= pccadd是否是G1S的句子可预测的试探推导过程:S pA pcAd pccAddpccadd 试探成功。第15页/共63页164 4。 开始符号集-FIRST-FIRST集设G=(VG=(VT T,V,VN N,P,P,S)S)是上下文无关文法FIRSTFIRST( )=a|=a| =* a a ,a,aV VT T, , 、 VV* * 若 =* 则规定FRI

9、STFRIST( )第16页/共63页17FOLLOWFOLLOW(A A)=a=a S S =* A A 且a a FRISTFRIST( ), VV* *, VV+ + 若S S =* u A u A , ,且 =* ,则#F#FOLLOW(A)OLLOW(A)5 5。后跟符号集-FOLLOW-FOLLOW集第17页/共63页186。SELECT集给定上下文无关文法的产生式 A AV VN N, , VV* *若* ,则SELECT(SELECT()=FIRST()=FIRST() )若=* , ,则SELECT(SELECT()=(FIRST()=(FIRST()-)- )FOLLOW(

10、A)FOLLOW(A) )第18页/共63页19 7 7。 LLLL(1 1)文法 一个文法G G是LLLL(1 1)的,当且仅当对于G G的每一个非终结符的任何两个不同产生式 ,下面的条件成立: SELECT(SELECT() SELECT(SELECT()=)= 其中和不能同时=* 第19页/共63页20 书中例子第20页/共63页21 5.2 LL5.2 LL(1 1)文法的判别判别步骤:1 1)。求出能推出的非终结符第21页/共63页222 2)。 计算FIRSTFIRST集X X V V , ,则FIRST(X)=XFIRST(X)=X2.2.若X X V VN N, ,且有产生式X

11、 Xaa,则把a a加入到FIRST(X)FIRST(X)中; ;若X X也是一条产生式, ,则把 也加到FIRST(X)FIRST(X)中. .X XYY是一个产生式且Y Y V VN N, ,则把FIRST(Y)FIRST(Y)中的所有非 元素都加到FIRST(X)FIRST(X)中; ;若X X Y Y1 1Y Y2 2YYK K 是一个产生式,Y,Y1 1,Y,Y2 2,Y,Y(i-1)(i-1)都是非终结符, ,而且, ,对于任何j,1j,1j j i-i-1, FIRST(Y1, FIRST(Yj j) )都含有 ( (即Y Y1 1.Y.Y(i-1) (i-1) =* ), ),

12、则把FIRST(YFIRST(Yj j) )中的所有非 元素和FIRST(YFIRST(Yi i) )中的所有元素都加到FIRST(X)FIRST(X)中; ;特别是, ,若所有的FIRST(YFIRST(Yj j , , j=1,2,K)j=1,2,K)均含有 , ,则把 加到FRIST(X)FRIST(X)中. . 第22页/共63页233 3)。 计算FOLLOWFOLLOW集1.1.对于文法的开始符号S,S,置# #于FOLLOW(S) FOLLOW(S) 中; ;BB是一个产生式, ,则把 FIRST()-FIRST()- 加至FOLLOW(B)FOLLOW(B)中; ;3.3.若B

13、B是一个产生式, ,或 B B是 一个产生式而 =* ( (即FIRST()FIRST()), 则把FOLLOWFOLLOW(A A)加至FOLLOWFOLLOW(B B)中第23页/共63页24G E: (1) E TE (2) E +TE (3) E (4) T FT (5) T *FT (6) T (7) F (E) (8) F a各非终结符的FIRST集合如下:FIRST(E)=(,aFIRST(E)=+,FIRST(T)=(,aFIRST(T)=*,FIRST(F)=(,a各非终结符的FOLLOW集合为:FOLLOW(E)=),FOLLOW(E)=),FOLLOW(T)=,),FOL

14、LOW(T)=,),# FOLLOW(F)=*,,),# 第24页/共63页254)。 计算SELECT集 计算产生式的SELECT集第25页/共63页26G E: (1) E TE (2) E +TE (3) E (4) T FT (5) T *FT (6) T (7) F (E) (8) F aE +TE | FIRST(+TE)=FIRST(+TE)=+ + FOLLOW(E)=FOLLOW(E)=) ),T *FT | FIRST(FIRST(* *FT)=FT)=* * FOLLOW(T)=FOLLOW(T)=+,)+,),F (E) | a FIRST( (E)=FIRST( (E

15、)=( ( FIRST( a)=FIRST( a)=a a所以GEGE是LLLL(1 1)的第26页/共63页275)。判断文法是否LL(1)文法 若文法所有具有相同左部产生式的SELECT集两两不相交,则文法是LL(1)文法。第27页/共63页28LL(1)LL(1)文法的性质: LL(1)LL(1)文法是无二义的 LL(1)LL(1)文法不含左递归第28页/共63页295.3 5.3 某些非LL(1)LL(1)文法的改造1 1。提取左公共因子 提左公因子: 将产生式 | 变换为: B B B B | 第29页/共63页30一般形式: 1 1| | 2 2| n n 提取左公共因子后: A

16、AAA A A1 1|2 2|n n第30页/共63页312。消除左递归左递归 关于非终结符P的规则直接左递归 : P P | | 、 V V* *且、不以P开头一般 左递归 : P =* P 例: SAa SAa A ASbSb 第31页/共63页32消除文法中左递归规则1) 消除直接左递归: 形如:P P | | 非 , ,不以P P打头 改写为:P Q Q Q Q Q| Q| 其中Q为新增加的非终结符第32页/共63页33消除文法中左递归规则举例例:E E+T|T |T T T TT* *F|FF|F F F (E)| a(E)| a G E: (1) E TE (2) E +TE (3

17、) E (4) T FT (5) T *FT (6) T (7) F (E) (8) F a第33页/共63页34消除一般左递归对文法要求: 1. 无回路(A(=+(A) 2. 无空产生式2 2) 消除一般左递归的方法:(1 1) .以某种顺序将文法非终结符排列A1 ,A2 An(2 2) for i:=1 to n dofor i:=1 to n do begin begin for j:=1 to i-1 do 用Aj-1| 2| k 替代形如Ai- Ajr的规则, 其中Aj- 1| 2| k是关于Aj的全部产生式; 消除Ai规则的直接左递归; end; end;(3 3)化简由(2)得到

18、的文法:去掉无用产生式第34页/共63页35 例P90第35页/共63页36消除左递归和提左公因子并不一定都能将非LL(1)LL(1)文法改造为LL(1)LL(1)的S S ifif C t S | C t S | ifif C t S e S C t S e SC bC b提左因子提左因子 S S ifif C t S A C t S A A e S | A e S | First First集集 FollowFollow集集S S ifif #,e #,eA e, A e, #, eC b tSelect(AeSAeS)Select(A A ) =e#, e 改造后文法不是LL(1)文法第

19、36页/共63页375.5 确定的自顶向下分析方法特征根据下一个(几个)输入符号为当前要处理 的非终结符选择产生式要求文法是LL(1)的 第一个L 从左到右扫描输入串 第二个L 生成的是最左推导 1 向前看一个输入符号(lookahead)第37页/共63页38无回溯的自顶向下分析程序预测分析程序的实现技术 1. 递归(下降)子程序 2. 表驱动分析程序第38页/共63页39例:递归下降子程序ParseFunction()BNF(Backus-Naur Form)描述program function_listfunction_list function function_list | func

20、tion FUNC identifier ( parameter_list ) statementvoid ParseFunction()MatchToken(T_FUNC);ParseIdentifier();MatchToken(T_LPAREN);ParseParameterList();MatchToken(T_RPAREN);ParseStatement();第39页/共63页40例:递归下降子程序ParseFunction()(续)void MatchToken(int expected)if (lookahead != expected) printf(syntax error

21、n);exit(0); else / if match, consume token and move onlookahead = yylex();/读入一个单词第40页/共63页41预测分析程序的实现表驱动预测分析程序模型 Input Input #总控程序总控程序预测分析表预测分析表stack第41页/共63页42预测分析表构造算法1.1.对文法G G的每个产生式 执行第二步 和第三步;2.2.对每个终结符a a FIRST(FIRST( ) ),把 加 至A,aA,a中, FIRST(FIRST( ) ),则对任何b b FOLLOW(A)FOLLOW(A) 把 加至A,bA,b中,A,

22、aA,a标上“出错标志”。可以证明,一个文法可以证明,一个文法G G的预测分析表不含多重入口,当且仅当该文法是的预测分析表不含多重入口,当且仅当该文法是LL(1)LL(1)的的第42页/共63页43 例:表驱动予测分析程序G E: (1) E TE (2) E +TE (3) E (4) T FT (5) T *FT (6) T (7) F (E) (8) F a 用预测分析表表示状态转换。第43页/共63页44 a + * ( ) # E (1) (1) E (2) (3) (3) T (4) (4) T (6) (5) (6) (6) F (8) (7)G E: (1) E TE (2)

23、E +TE (3) E (4) T FT (5) T *FT (6) T (7) F (E) (8) F a 预测分析表预测分析表第44页/共63页45表驱动预测分析程序分析算法 首先把#然后把文法开始符号推入栈;把第一个输入符号读进b;b; FLAGFLAG:=TRUE=TRUE; WHILE FLAG DOWHILE FLAG DO BEGIN BEGIN 把栈顶符号上托出去并放在中; IF X IF X Vt THEN IF X=b THEN Vt THEN IF X=b THEN 把下一个输入符号读进b b ELSE ERROR ELSE ERROR ELSE IF X= ELSE I

24、F X=# # THEN THEN IF b= IF b=# # THEN FLAG:=FALSE THEN FLAG:=FALSE ELSE ERROR ELSE ERROR ELSE IF ELSE IF X,b=X X,b=X X X1 1X X2 2.X.XK K THEN THEN 把X XK K,X X K-1K-1,.,X,.,X1 1一一推进栈 ELSE ELSEERRORERROR END OF WHILE; END OF WHILE; STOP/ STOP/* *分析成功,过程完毕* *第45页/共63页46分析输入串#a+a#的步骤栈内容 栈顶符号 当前输入 余留串 MX

25、,b 1 #E E a +a# E TE2 #ET T a +a# T FT3 #ETF F a +a# F a4 #ETa a a +a#5 # ET T + a# T 6 #E E + a# E +TE7 #ET+ + + a#8 # ET T a # T FT 9 #ETF F a # F a10 #ETa a a #11 #ET T # T 12 #E E # E 13 # # #第46页/共63页47LL(1)分析中的一种错误处理办法发现错误1栈顶的终结符与当前输入符不匹配2非终结符A于栈顶,面临的输入符为a,但分析表M的MA,a为空“应急”恢复策略跳过输入串中的一些符号直至遇到“同

26、步符号”为止。同步符号的选择1把FOLLOW(A)中的所有符号作为A的同步符号。跳过输入串中的一些符号直至遇到这些“同步符号”,把A从栈中弹出,可使分析继续2把FIRST(A)中的符号加到A的同步符号集,当FIRST(A)中的符号在输入中出现时,可根据A恢复分析第47页/共63页48review-parsingThe syntax analysis phase of a compiler verifies that the sequence of tokens returned from the scanner represent valid sentences in the grammar

27、of the programming language. There are two major parsing approaches: top-down and bottom-up. In top-down parsing, you start with the start symbol and apply the productions until you arrive at the desired string. In bottom-up parsing, you start with the string and reduce it to the start symbol by app

28、lying the productions backwards. 第48页/共63页49In 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 prints a leftmost derivat

29、ion of the sentence.A bottom-up parse 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. I

30、f you read from the bottom to top, the bottom-up parse prints out a rightmost derivation of the sentence.第49页/共63页50 lookahead symbol. The lookahead symbol is the next symbol coming up in the input. backtracking. Based on the information the parser currently has about the input, a decision is made t

31、o go with one particular production. If this choice leads to a dead end, the parser would have to backtrack to that decision point, moving backwards through the input, and start again making a different choice and so on until it either found the production that was the appropriate one or ran out of

32、choices.第50页/共63页51predictive parser and LL(1)grammarPredictive 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 being processed. To enable this

33、, the grammar must take a particular form. We call such a grammar 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.第51页/共63页52recursive-descentThe first technique for implementing a pr

34、edictive parser is called recursive-descent. A recursive descent parser consists of several small functions(procedures), one for each nonterminal in the grammar. As we parse a sentence, we call the functions (procedures) that correspond to the left side nonterminal of the productions we are applying

35、. If these productions are recursive, we end up calling the functions recursively.第52页/共63页53Table-driven LL(1) parsing In a recursive-descent parser, the production information is embedded in the individual parse functions for each nonterminal and the run-time execution stack is keeping track of ou

36、r progress through the parse. There is another method for implementing a predictive parser that uses a table to store that production along with an explicit stack to keep track of where we are in the parse.第53页/共63页54 How a table-driven predictive parser works We push the start symbol on the stack a

37、nd read the first input token. As the parser works through the input, there are the following possibilities 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 successfully2. If X = a and a != #: successful match, pop X

38、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 table 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 fro

39、m step 3 is blank, there has been a parse error第54页/共63页55The first set of a sequence of symbols u, written as First(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 , w

40、here v begins with someterminal, that terminal is in First(u). If u =* , then is in First(u ).第55页/共63页56The follow set 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 =

41、*uAv , where v begins with some terminal, that terminal is in Follow(A).第56页/共63页57Calculating first setTo calculate First(u) where u has the 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.

42、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 X1X2.Xn =* , add to the first set.第57页/共63页58Calculating follow sets. For each nonterminal in the grammar, do the following:1. Place# in F

43、ollow(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 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 (i.e. v is nullable), then ever

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论