《编译原理编译原理编译原理 (37).ppt》由会员分享,可在线阅读,更多相关《编译原理编译原理编译原理 (37).ppt(5页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。
1、CompilerS1Follow Sets Given a nonterminal A,the set Follow(A),consisting of terminals and$,is defined as follows.1.If A is the start symbol,then$is in Follow(A).2.If there is a production B A,then First()is in Follow(A).3.If there is a production B A such that is in the First(),then Follow(A)contain
2、s Follow(B).oIf B A,Follow(A)contains Follow(B)CompilerS2Example 4.9First(exp)=(,numberFirst(addop)=+,-First(term)=(,numberFirst(mulop)=*First(factor)=(,number(1)exp exp addop term(2)exp term(3)addop +(4)addop -(5)term term mulop factor(6)term factor(7)mulop *(8)factor (exp)(9)factor numberFollow(ex
3、p)=Follow(addop)=Follow(term)=Follow(factor)=Follow(mulop)=,+,-(,number$,+,-,*(,number$,+,-,*,),),)CompilerS3Example 4.9xxexp exp addop term|termaddop +|-term term mulop factor|factormulop *factor (exp)|number(1)exp exp addop term(2)exp term(3)addop +(4)addop -(5)term term mulop factor(6)term factor
4、(7)mulop *(8)factor (exp)(9)factor numberRewrite each choice separately.Note,there is no -productions.CompilerS4Example 4.13xxFollow(statement)=Follow(if-stmt)=Follow(else-part)=Follow(exp)=(1)statement if-stmt(2)statement other(3)if-stmt if(exp)statement else-part(4)else-part else statement(5)else-
5、part (6)exp 0(7)exp 1First(statement)=if,otherFirst(if-stmt)=if First(else-part)=else,First(exp)=0,1$),else$,else,elseCompilerS5Example 4.10statement if-stmt|otherif-stmt if(exp)statement else-partelse-part else statement|exp 0|1(1)statement if-stmt(2)statement other(3)if-stmt if(exp)statement else-part(4)else-part else statement(5)else-part (6)exp 0(7)exp 1