?? whiledlg.cpp
字號:
fstream fout;
content[0]='\0';
m_Input.GetWindowText(STR);
count=STR.GetLength();
for(int i=0;i<count;i++)
content[i]=STR.GetAt(i);
content[count]='\0';
def_filepath="save.txt";
fout.open(def_filepath,ios::out);
fout<<content;
fout.close();
f_c.open("save.txt");
f_c.get(c_c);
if(c_c==EOF) isSaved=FALSE;
else isSaved=TRUE;
f_c.close();
Translate();
}
int CWhileDlg::Translate()
{
if(isSaved==FALSE)
{
MessageBox("Please input a while sentence!");
return 0;
}
else
{
Init();
Main();
Dsp();
return 0;
}
}
int CWhileDlg::Main()
{
input.open("save.txt");
output.open("output.txt");
Print();
int check,over=0;
int m,k;
char chr;
if(input.fail())
{
output<<"Can not open the input file!\r"<<endl;
input.close();
return 0;
}
if(output.fail())
{
output<<"Can not open the output file!\r"<<endl;
output.close();
return 0;
}
for(m=0;m<MAXBUF;m++)
for(k=0;k<buf;k++)
token[m].name[k]='\0';
initlab();
input.get(sym);
while(sym!=EOF)
{
chr=Getsymbol();
if(chr==' ') continue;
else
{
S[num]=chr;
num++;
}
}
input.close();
input.open("save.txt");
S[num++]='#';
output<<"The while sentence is:\r"<<endl;
/*for(m=0;m<t;m++)
{
k=0;
while(token[m].name[k]!='\0')
{
output<<token[m].name[k];
k++;
}
output<<" ";
}*/
input.get(sym);
while(sym!=EOF)
{
output<<sym;
input.get(sym);
}
output<<"\r"<<endl;
for(check=0;check<num;check++)
{
if(S[check]=='i'&&S[check+1]=='i')
{
over=-1; //出錯標志
break;
}
}
/*for(m=0;m<num;m++)
{
cout<<S[m];
}*/
output<<"\r"<<endl;
ana();
if(over==-1||Check!=1)
{
output<<"Your input does not tally with the grammar!\r"<<endl;
input.close();
output.close();
return -1;
}
if(Check==1)
{
output<<"Parsing completed!";
}
output<<"\r"<<endl;
input.close();
output.close();
return 0;
}
void CWhileDlg::ana()
{
i=0;
int k=0;
status.push(0);
symbol.push('#');
while(S[i]!='#'&&symbol.top()!='S')
{
if(k==0)
{
k=Mt::meet(status.top(),S[i]);
continue;
}
if(k==1)
{
k=Mt::meet(status.top(),symbol.top());
continue;
}
if(k==-1)
{
Check=-1;
break;
}
if(k==2)
{
Check=1;
break;
}
}
k=0;
while(S[i]=='#')
{
if(symbol.top()=='S')
{
if(Mt::meet(status.top(),S[i])==2)
{
ONE();
Check=1;
break;
}
}
if(symbol.top()!='S'&&(Mt::meet(status.top(),symbol.top())==1||Mt::meet(status.top(),symbol.top()))==0)
continue;
else
if(Mt::meet(status.top(),symbol.top())==-1)
{
Check=-1;
break;
}
}
}
void CWhileDlg::Print()
{
output<<"The Grammar of a while sentence:\r"<<endl;
output<<"******************************************\r"<<endl;
output<<"* (1) S --> while(A) B *\r"<<endl;
output<<"* (2) A --> N rop N *\r"<<endl;
output<<"* (3) B --> N = N op N *\r"<<endl;
output<<"* (4) N --> id *\r"<<endl;
output<<"******************************************\r"<<endl;
}
void CWhileDlg::initlab()
{
int m,k;
for(m=0;m<n_lab;m++)
for(k=0;k<Act;k++)
{
lable[m].Action[k]=-1;
lable[m].lab=0; //lab=0若為1表示規(guī)約
}
for(m=0;m<n_lab;m++)
for(k=0;k<Goto;k++)
{
lable[m].Action[k]=-1;
lable[m].lab=0;
lable[m].num=0;
lable[m].status=0;
}
for(m=0;m<n_lab;m++)
{
lable[m].chr[0]='w';
lable[m].chr[1]='(';
lable[m].chr[2]=')';
lable[m].chr[3]='r';
lable[m].chr[4]='=';
lable[m].chr[5]='o';
lable[m].chr[6]='i';
lable[m].chr[7]='#';
lable[m].chr[8]='S';
lable[m].chr[9]='A';
lable[m].chr[10]='B';
lable[m].chr[11]='N';
}
lable[0].Action[0]=2;
lable[0].Goto[0]=1;
lable[1].Action[7]=-2; //acc
lable[2].Action[1]=3;
lable[3].Action[6]=6;
lable[3].Goto[1]=4;
lable[3].Goto[3]=5;
lable[4].Action[2]=7;
lable[5].Action[3]=8;
lable[6].lab=1;
lable[6].num=4;
lable[7].Action[6]=6;
lable[7].Goto[2]=9;
lable[7].Goto[3]=10;
lable[8].Action[6]=6;
lable[8].Goto[3]=11;
lable[9].lab=1;
lable[9].num=1;
lable[10].Action[4]=12;
lable[11].lab=1;
lable[11].num=2;
lable[12].Action[6]=6;
lable[12].Goto[3]=13;
lable[13].Action[5]=14;
lable[14].Action[6]=6;
lable[14].Goto[3]=15;
lable[15].lab=1;
lable[15].num=3;
}
char CWhileDlg::Getsymbol()
{
i=0;
while(sym!=EOF)
{
if(sym=='_'||(sym>='a'&&sym<='z')||(sym>='A'&&sym<='Z'))
{
int h1=0,h2=0,h3=0;
while(sym=='_'||(sym>='a'&&sym<='z')||(sym>='A'&&sym<='Z')||(sym>='0'&&sym<='9'))
{
token[t].name[i++]=sym;
input.get(sym);
}
token[t].num=i;
h1=strcmp(token[t].name,wh);
h2=strcmp(token[t].name,rop);
h3=strcmp(token[t].name,op);
t++;
i=0;
if(!h1)
return 'w'; //識別了while
else
if(!h2) return 'r'; //識別了rop
else
if(!h3) return 'o'; //識別了op
else
return 'i';
} //已經(jīng)取了下一個字符
else
if(sym>='0'&&sym<='9')
{
while(sym>='0'&&sym<='9')
{
token[t].name[i++]=sym;
input.get(sym);
}
token[t].num=i;
t++;
return 'i'; //數(shù)字
}
else
if(sym=='(')
{
token[t].name[0]=sym;
token[t].num=0;
t++;
input.get(sym);
return '(';
}
else
if(sym==')')
{
token[t].name[0]=sym;
token[t].num=0;
t++;
input.get(sym);
return ')';
}
else
if(sym=='=')
{
token[t].name[0]=sym;
token[t].num=0;
t++;
input.get(sym);
return '=';
}
else
if(sym==' ')
{
input.get(sym);
return ' ';
}
else
{
input.get(sym);
return 'X';
}
}
return 0;
}
void CWhileDlg::ONE()
{
output<<"The output is:\r"<<endl;
output<<"100 A := ";
NN::N(1);
output<<" rop ";
NN::N(2);
output<<"\r"<<endl;
TWO();
}
void CWhileDlg::TWO()
{
output<<"101 if(A) goto 103\r"<<endl;
output<<"102 goto 105\r"<<endl;
THREE();
output<<"105 exit\r"<<endl;
}
void CWhileDlg::THREE()
{
cout<<"103 ";
output<<"103 ";
NN::N(3);
cout<<" := ";
output<<" := ";
NN::N(4);
cout<<" op ";
output<<" op ";
NN::N(5);
output<<"\r"<<endl;
output<<"104 goto 100\r"<<endl;
}
void CWhileDlg::OnTr()
{
// TODO: Add your control notification handler code here
SaveTr();
char *chrr;
chrr=new char[3];
chrr[0]='\r'; chrr[1]='\n'; chrr[2]='\0';
m_Output.SetSel(0,-1);
m_Output.Clear();
ifstream put("output.txt");
put.get(sym);
while(sym==' ')
{
put.get(sym);
}
while(sym!=EOF)
{
if(sym=='\r') m_Output.ReplaceSel(chrr);
else m_Output.ReplaceSel(&sym);
put.get(sym);
}
}
void CWhileDlg::Init()
{
num=0;
i=0;
t=0;
while(!status.empty()) status.pop();
while(!symbol.empty()) symbol.pop();
}
void CWhileDlg::OnExit()
{
// TODO: Add your control notification handler code here
OnOK();
}
void CWhileDlg::OnClear()
{
// TODO: Add your control notification handler code here
m_Input.SetSel(0,-1);
m_Input.Clear();
}
void CWhileDlg::Dsp()
{
char *enter;
enter=new char[3];
enter[0]='\r'; enter[1]='\n'; enter[2]='\0';
m_Edit.SetSel(0,-1);
m_Edit.Clear();
m_Edit.ReplaceSel(" |______________Action_______________|______Goto_____|");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel("狀態(tài)|while| ( | ) |rop| = | op | id | # | S | A | B | N |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 0 | S2 | | | | | | | | 1 | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 1 | | | | | | | |acc| | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 2 | | S3| | | | | | | | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 3 | | | | | | | S6 | | | 4 | | 5 |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 4 | | | S7| | | | | | | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 5 | | | | S8| | | | | | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 6 | r4 | r4| r4| r4| r4| r4 | r4 | r4| | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 7 | | | | | | | S6 | | | | 9 | 10|");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 8 | | | | | | | S6 | | | | | 11|");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 9 | r1 | r1| r1| r1| r1| r1 | r1 | r1| | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 10 | | | | |S12| | | | | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 11 | r2 | r2| r2| r2| r2| r2 | r2 | r2| | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 12 | | | | | | | S6 | | | | | 13|");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 13 | | | | | | S14| | | | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 14 | | | | | | | S6 | | | | | 15|");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel(" 15 | r3 | r3| r3| r3| r3| r3 | r3 | r3| | | | |");
m_Edit.ReplaceSel(enter);
m_Edit.ReplaceSel("  ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄ ̄");
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -