您好,欢迎访问三七文档
/***********************************************************Copyright(c)2004,RM.RYT.Allrightsreserved.DevelopedinMicrosoftVisualC++6.0EnterpriseEdition.***********************************************************/#includeiostream.h#includestdio.h#includestring.h#includeconio.h#includestdlib.h#includectype.h///////////////////////////////////////////////////¶¨Òå½áµãÀàtemplateclassElemclasslink{public:link*next;Elemelement;intsign;link(constElem&elementValue,link*nextValue=NULL){element=elementValue;next=nextValue;sign=-1;}link(link*nextValue=NUll){next=nextValue;sign=-1;}};///////////////////////////////////////////////////¶¨Òå¶ÑÕ»ÀàtemplateclassElemclassstack{private:linkElem*top;intsize;public:stack(intsz=DefaultSize){top=NULL;size=0;}~stack(){clear();}voidclear(){while(top!=NULL){linkElem*temp=top;top=top-next;size=0;deletetemp;}}//ÈëÕ»º¯Êýboolpush(constElem&item){top=newlinkElem(item,top);size++;returntrue;}//³öÕ»º¯ÊýElempop(){Elemitem;if(size==0)returnfalse;item=top-element;linkElem*temp=top-next;deletetop;top=temp;size--;returnitem;}//»ñÈ¡Õ»¶¥ÔªËصÄÖµElemGetTop()const{Elemitem;if(size==0)returnfalse;item=top-element;returnitem;}//»ñÈ¡¶ÑÕ»µÄÉî¶Èintlength()const{returnsize;}//ÅжÏÕ»ÊÇ·ñΪ¿ÕboolIsEmpty()const{returntop==NULL;}//»ñÈ¡±ê־λintGetSign()const{returntop-sign;}//ÉèÖñê־λvoidSetSign(ints){top-sign=s;}};////////////////////////////////////¶¨Òå²úÉúʽµÄÊý¾Ý½á¹¹classProduction{public:charFrom;charTo[20];};////////////////////////////////////¶¨ÒåÊäÈëÎÄ·¨Êý¾Ý½á¹¹classGrammar{private:charstartState;Productionpro[30];intNumOfGra;public:Grammar(){NumOfGra=0;}~Grammar(){}//¶¨ÒåÖØÔØÔËËã·û==booloperator==(Grammarg){if(g.NumOfGra==NumOfGra){boolflag=true;Productionp;for(inti=0;iNumOfGra;i++){p=g.GetProduction(i);if(!IsIn(p)){flag=false;break;}}returnflag;}elsereturnfalse;}//»ñÈ¡chÔÚÊý×éÖеÄλÖÃintGetPos(char*str,charch){intlen=strlen(str);for(inti=0;ilen;i++){if(ch==str[i])break;}returni;}//»ñÈ¡Ò»¸ö²úÉúʽµÄλÖÃintGetPos(Productionp){for(inti=0;iNumOfGra;i++){if((p.From==pro[i].From)&&(strcmp(p.To,pro[i].To)==0))break;}returni;}//»ñÈ¡ÓëchÏàͬÎÄ·¨×󲼵IJúÉúʽintGetProDuctionOF(charch){for(inti=0;iNumOfGra;i++){if(ch==pro[i].From){break;}}returni;}//²åÈëÒ»ÌõÎÄ·¨boolInsert(charfr,char*temp){NumOfGra++;pro[NumOfGra-1].From=fr;strcpy(pro[NumOfGra-1].To,temp);returntrue;}//ÔÚpos´¦²åÈëÒ»ÌõÎÄ·¨boolInsert(intpos,charfr,char*temp){for(inti=NumOfGra;ipos;i--){pro[i]=pro[i-1];}pro[pos].From=fr;strcpy(pro[pos].To,temp);NumOfGra++;returntrue;}//ÉèÖúͻñÈ¡¿ªÊ¼×´Ì¬boolSetStartState(charch){startState=ch;returntrue;}//»ñÈ¡ÎÄ·¨¿ªÊ¼×´Ì¬charGetstartState(){returnstartState;}//»ñÈ¡¿ªÊ¼µÄ²úÉúʽProductionGetStartProduction(){for(inti=0;iNumOfGra;i++){if((pro[i].From==startState)&&(GetPos(pro[i].To,'.')==0)){break;}}returnpro[i];}//ÎÄ·¨ÊǶ¼Îª¿ÕboolIsEmpty(){if(NumOfGra==0)returntrue;elsereturnfalse;}//ɾ³ýÒ»ÌõÎÄ·¨boolDelete(charfr,char*temp){for(inti=0;iNumOfGra;i++){if((pro[i].From==fr)&&(strcmp(pro[i].To,temp)==0)){pro[i]=pro[NumOfGra-1];NumOfGra--;}}returntrue;}//ɾ³ýÒ»¸öÎÄ·¨boolDelete(intpos){pro[pos]=pro[NumOfGra-1];NumOfGra--;returntrue;}//»ñÈ¡ÎÄ·¨µÄÌõÊýintGetNumOfGra(){returnNumOfGra;}//»ñÈ¡Ò»ÌõÎÄ·¨ProductionGetProduction(intpos){returnpro[pos];}//ÅжϲúÉúʽÊÇ·ñÒѾÔÚÎÄ·¨ÖÐboolIsIn(Productionp){for(inti=0;iNumOfGra;i++){if((pro[i].From==p.From)&&(strcmp(pro[i].To,p.To)==0)){returntrue;}}returnfalse;}};/////////////////////////////////////////////////classTableElem{public:charch;intstate;};///////////////////////////////////////////////////¶¨ÒåÏîÄ¿¼¯µÄÀàclassItem{private:intNumOfItems;ProductionStartProduction;Grammargr[20];public:Item(){NumOfItems=0;}~Item(){}//ÉèÖóõʼ״̬µÄ²úÉúʽvoidSetStartProduction(Productionp){StartProduction=p;}//»ñÈ¡³õʼ״̬µÄ²úÉúʽProductionGetStartProduction(){returnStartProduction;}//»ñÈ¡ÏîÄ¿¼¯µÄÊýÄ¿intGetNumOfItems(){returnNumOfItems;}//»ñÈ¡Ò»¸öÏîÄ¿¼¯GrammarGetItem(intpos){returngr[pos];}//²åÈëÒ»¸öÏîÄ¿¼¯boolInsert(Grammarg){gr[NumOfItems]=g;NumOfItems++;returntrue;}//ÔÚpos´¦²åÈëÒ»¸öÏîÄ¿¼¯boolInsert(intpos,Grammarg){for(inti=NumOfItems;ipos;i--){gr[i]=gr[i-1];}gr[pos]=g;NumOfItems++;returntrue;}//ɾ³ýÒ»¸öÏîÄ¿¼¯boolDelete(Grammarg){for(inti=0;iNumOfItems;i++){if(g==gr[i]){gr[i]=gr[NumOfItems-1];NumOfItems--;}}returntrue;}//ɾ³ýÒ»¸öÏîÄ¿¼¯boolDelete(intpos){gr[pos]=gr[NumOfItems-1];NumOfItems--;returntrue;}//ÅжÏÏîÄ¿¼¯ÊÇ·ñÒѾÔÚÏîÄ¿¼¯×åÖÐboolIsIn(Grammarg){for(inti=0;iNumOfItems;i++){if(gr[i]==g){returntrue;}}returnfalse;}//»ñÈ¡¿ªÊ¼µÄÏîÄ¿¼¯GrammarGetStartItem(){returngr[0];}//»ñÈ¡ÏîÄ¿¼¯µÄλÖÃintGetPos(Grammarg){for(inti=0;iNumOfItems;i++){if(gr[i]==g){break;}}returni;}};///////////////////////////////////////////////////ÎÄ·¨·ÖÎö±íµÄÊý¾Ý½á¹¹classParseTable{private:introw;intcol_n;intcol_t;charInputSymbol[20];charNonTerminal[20];TableElem**action;int**goTo;public://¹¹Ô캯ÊýParseTable(char*in,char*Non,intStateCount){inti,j;row=StateCount;col_t=strlen(in);col_n=strlen(Non);strcpy(InputSymbol,in);strcpy(NonTerminal,Non);//actionaction=(TableElem**)(newTableElem*[c
本文标题:SLR(1)分析表
链接地址:https://www.777doc.com/doc-5310251 .html