?? csshi.h
字號:
{
csshi::follow[j][3+followchang]=csshi::first[i][2+k];
csshi::follow[j][2]++;
// cout<<"添加一個"<<csshi::follow[i][2+k]<<"給"<<c1<<endl;
followchang++;
}
}
}
}
void csshi::getfollow()
{
shuzu Unsz,szR;
//..........................................................
//get the unsame chars's number
initshuzu(Unsz);
Unsz=csshi::getunEndch();
csshi::intunsamech=Unsz.chang;
//..........................................................
initshuzu(szR);
//把follow集合這個2維數組的每行左段標示對應的元素
for (int i=1;i<=csshi::intunsamech;i++)
{
csshi::follow[i][1]=Unsz.ch[i];
csshi::follow[i][2]='a';
}
//主程序算法開始:
int intunsame;
int tempf;char tempc;//標記要求誰的follow的集合
csshi::inputfollow('#',1);
for(int xh=1;xh<=csshi::intunsamech;xh++)
{ intunsame=0;
while (intunsame<=csshi::intunsamech)
{
intunsame++;
for (int i=1;i<=csshi::intunsamech;i++)
if (csshi::follow[i][1]==Unsz.ch[intunsame])
{ //標記要求誰的follow集合
tempc=csshi::follow[i][1];
tempf=i;
}
for (i=1;i<=csshi::intpro;i++)
{
szR=csshi::getrch(i);
for (int j=2;j<=szR.chang+1;j++)
if (szR.ch[j]==tempc)//如果找到右部有要求的標記字符
{
bool pd=true;
int followch=j;
while (pd)
{
pd=false;
if (followch==szR.chang+1)//如果找到的字符是右部最后一個字符
csshi::copyfollow(csshi::prochar[i][1],tempc);
else if (szR.ch[followch+1]>'Z' || szR.ch[followch+1]<'A')
csshi::inputfollow(szR.ch[followch+1],tempf);
else//后面一個字符是非終結符
{
csshi::firsttofollow(szR.ch[followch+1],tempc);
if(csshi::infirst(szR.ch[followch+1]))
{
pd=true;followch++;
}
}//else//后面一個字符是非終結符
}//while (pd)
}//if (szR.ch[j]==tempc)//如果找到右部有要求的標記字符
}//for (i=1;i<=csshi::intpro;i++)
}//while(intunsame<=csshi::intunsamech)
}//for(xh)
}
void csshi::outfollow()
{
cout<<".............................................................."<<endl;
for (int i=1;i<=csshi::intunsamech;i++)
{
cout<<csshi::follow[i][1]<<"'s follow:";
if (csshi::follow[i][2]=='a')
cout<<"empty";
else
{
bool pd=true;
char c='a';int j=3;
while(pd)
{
if (c==csshi::follow[i][2])
pd=false;
else
{
cout<<csshi::follow[i][j];
j=j+1;
c++;
}
}
}
cout<<endl<<csshi::follow[i][1]<<"'s number:"<<int(csshi::follow[i][2]-'a');
cout<<endl;
}
cout<<".............................................................."<<endl;
}
void csshi::inputselect(char c,int i)
{
int selectchang=int(csshi::select[i][1]-'a');
bool pd=true;
for (int j=2;j<=1+selectchang;j++)
if (csshi::select[i][j]==c)
pd=false;
if(pd)
{
csshi::select[i][2+selectchang]=c;
csshi::select[i][1]++;
// cout<<"添加一個"<<c<<"給第"<<i<<"條產生式的select集合"<<endl;
}
}
void csshi::firsttoselect(char cfs,int temps)
{
int selectchang=int(csshi::select[temps][1]-'a');
for (int i=1;i<=csshi::intunsamech;i++)
if (csshi::first[i][1]==cfs)
{
int firstchang=int(csshi::first[i][2]-'a');
for (int k=1;k<=firstchang;k++)
{
bool pd=true;
for (int tempi=2;tempi<=1+selectchang;tempi++)
if (csshi::select[temps][tempi]==csshi::first[i][2+k])
pd=false;
if(pd && csshi::first[i][2+k]!='&')
{
csshi::select[temps][2+selectchang]=csshi::first[i][2+k];
csshi::select[temps][1]++;
// cout<<"添加一個"<<csshi::first[i][2+k]<<"給第"<<temps<<"條產生式的select集合"<<endl;
selectchang++;
}
}
}//if (csshi::first[i][1]==cfs)
}
void csshi::followtoselect(char c,int temps)
{
int selectchang=int(csshi::select[temps][1]-'a');
for (int i=1;i<=csshi::intunsamech;i++)
if (csshi::follow[i][1]==c)
{
int followchang=int(csshi::follow[i][2]-'a');
for (int k=1;k<=followchang;k++)
{
bool pd=true;
for (int tempi=2;tempi<=1+selectchang;tempi++)
if (csshi::select[temps][tempi]==csshi::follow[i][2+k])
pd=false;
if(pd)
{
csshi::select[temps][2+selectchang]=csshi::follow[i][2+k];
csshi::select[temps][1]++;
// cout<<"添加一個"<<csshi::follow[i][2+k]<<"給"<<c1<<endl;
selectchang++;
}//if(pd && csshi::first[i][2+k]!='&')
}//for (int k=1;k<=firstchang;k++)
}//if (csshi::first[i][1]==cfs)
}
void csshi::getselect()//格式如:E->+F#: a+
/////////////////////////a代表數量,只有一個元素+
{
//初始化select集合的元素個數
for (int i=1;i<=csshi::intpro;i++)
csshi::select[i][1]='a';
//主程序開始:
shuzu szR;
initshuzu(szR);
for (i=1;i<=csshi::intpro;i++)
{
szR=csshi::getrch(i);
bool pd=true;
int chint=2;
while(pd)
{
pd=false;
if (szR.ch[chint]=='&')
csshi::followtoselect(szR.ch[1],i);
else if (szR.ch[chint]>'Z' || szR.ch[chint]<'A')
csshi::inputselect(szR.ch[chint],i);
else
{
csshi::firsttoselect(szR.ch[chint],i);
if (csshi::infirst(szR.ch[chint]))
if (chint==szR.chang+1)
csshi::followtoselect(szR.ch[1],i);
else
{
pd=true;chint++;
}//else
}//else
}//while(pd)
}//for (i=1;i<=csshi::intpro;i++)
}//end fuction csshi::getselect()
void csshi::outselect()
{
cout<<".............................................................."<<endl;
for (int i=1;i<=csshi::intpro;i++)
{
cout<<"第"<<i<<"條產生式的select集合是";
if (csshi::select[i][1]=='a')
cout<<"empty";
else
{
bool pd=true;
char c='a';int j=2;
while(pd)
{
if (c==csshi::select[i][1])
pd=false;
else
{
cout<<csshi::select[i][j];
j=j+1;
c++;
}
}
}
cout<<endl<<"第"<<i<<"條產生式的select集合元素個數是:"<<int(csshi::select[i][1]-'a');
cout<<endl;
}
cout<<".............................................................."<<endl;
}
void csshi::getchart()
{
//得到非終結字符與終結字符
int UncharInt,charInt;
shuzu szU,sz;
initshuzu(szU);
initshuzu(sz);
szU=csshi::getunEndch();
UncharInt=szU.chang;
sz=csshi::getEndch();
charInt=sz.chang;
//...................................................
for (int i=1;i<=UncharInt+1;i++)
for (int j=1;j<=charInt+2;j++)
csshi::chart[i][j]=' ';
//初始化行
for (i=2;i<=UncharInt+1;i++)
csshi::chart[i][1]=szU.ch[i-1];
//初始化列
for (int j=2;j<=charInt+1;j++)
csshi::chart[1][j]=sz.ch[j-1];
csshi::chart[1][charInt+2]='#';
//...................................................
//主程序開始:
for (i=1;i<=csshi::intpro;i++)
{
int selectchang=int(csshi::select[i][1]-'a');
for (j=2;j<=selectchang+1;j++)
for (int k=2;k<=UncharInt+1;k++)
if (csshi::prochar[i][1]==csshi::chart[k][1])//find the hang
for (int z=2;z<=charInt+2;z++)
if (csshi::select[i][j]==csshi::chart[1][z])//find the lie
csshi::chart[k][z]=char(i+48);
}
}
void csshi::outchart()
{
//得到非終結字符與終結字符
int UncharInt,charInt;
shuzu szU,sz;
initshuzu(szU);
initshuzu(sz);
szU=csshi::getunEndch();
UncharInt=szU.chang;
sz=csshi::getEndch();
charInt=sz.chang;
//..........................................
cout<<"預測分析表:"<<endl;
cout<<"------------------------"<<endl;
for (int i=1;i<=UncharInt+1;i++)
{ for (int j=1;j<=charInt+2;j++)
cout<<csshi::chart[i][j]<<" ";
cout<<endl;
}
cout<<"------------------------"<<endl;
}
void csshi::mainfuction()
{
//初始化
stack st;
initstack(st);
push(st,csshi::bgchar);
cout<<"please input the string,end it by input '#':";
bool pd=true;int x=1;
while (pd)
{
cin>>csshi::string[x];
if (string[x]=='#')
pd=false;
else
x++;
}
cout<<"序列號"<<" "<<"字符棧"<<" "<<"剩余棧"<<" "<<"動作"<<endl;
//主程序
int i=1;//標記第幾行
pd=true;//判斷是否循環結束
int otherint=1;//標記去掉幾個字符
int stackint=1;//標示去掉了幾個分析棧中的元素
while (pd)
{
if (i<10)
cout<<" "<<i<<" ";
else
cout<<" "<<i<<" ";
//分析棧
stackint=st.chang;
for (int sti=1;sti<=st.chang+1;sti++)
cout<<st.ch[sti];
for (int temp=1;temp<=16-st.chang;temp++)
cout<<" ";
//字符串棧
int str=otherint;bool pd1=true;int temp1=0;
while (pd1)
{
cout<<csshi::string[str];
if (csshi::string[str]=='#')
pd1=false;
str++;
temp1++;
}
for(temp=1;temp<=17-temp1;temp++)
cout<<" ";
//...........................................................
if(st.ch[st.chang+1]==csshi::string[otherint])
if (st.ch[st.chang+1]=='#')
{ cout<<"接受"<<endl;
pd=false;
}
else
{
pop(st);
otherint++;
cout<<"匹配"<<endl;
}
else
{
//從select集合中找對應的產生式
int UncharInt,charInt;
shuzu szU,sz;
initshuzu(szU);
initshuzu(sz);
szU=csshi::getunEndch();
UncharInt=szU.chang;
sz=csshi::getEndch();
charInt=sz.chang;
bool pd2=false;
char c=st.ch[stackint+1];
for (int x1=2;x1<=UncharInt+1;x1++)
for (int x2=2;x2<=charInt+2;x2++)
if (c==csshi::chart[x1][1] && csshi::string[otherint]==csshi::chart[1][x2])
{
pd2=true;
if (csshi::chart[x1][x2]==' ')
{
cout<<"報錯"<<endl;
pd=false;
}
else
{
cout<<csshi::chart[x1][x2]<<endl;
pop(st);
int chanshengshi=int(csshi::chart[x1][x2])-48;
shuzu szR;
initshuzu(szR);
szR=csshi::getrch(chanshengshi);
for (int pushint=szR.chang+1;pushint>1;pushint--)
push(st,szR.ch[pushint]);
}
}//if
if (!pd2)
{
cout<<"報錯"<<endl;
pd=false;
}
}
i++;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -