为什么这个程序只能运行一步,希望高手帮帮忙!
时间:2008-06-12 13:11:22
来源:论坛整理 作者: 编辑:chinaitzhe
#include <iostream>
#include <string>
#include <iomanip>
#include <sstream>
#include <fstream>
//#include <stdlib>
using namespace std;
string stack[20]; //符号栈
string state[20]; //状态栈
string fruit; //当前字符
string zhen; //当前语义
string yuzhan[20]; //语义栈
string siyuan="= *";
int siyyuan=1;
int top=0; //栈顶标志
int analtable[17][11]= //分析表
{
{2,-1,-1,-1,-1,-1,-1,1,-1,-1,-1},
{-1,-1,-1,-1,-1,-1,100,-1,-1,-1,-1},
{-1,3,-1,-1,-1,-1,-1,-1,-1,-1,-1},
{8,-1,-1,-1,7,-1,-1,-1,4,5,6},
{-1,-1,9,-1,-1,-1,101,-1,-1,-1,-1},
{-1,-1,103,10,-1,103,103,-1,-1,-1,-1},
{-1,-1,105,105,-1,105,105,-1,-1,-1,-1},
{8,-1,-1,-1,7,-1,-1,-1,13,5,6},
{-1,-1,107,107,-1,107,107,-1,-1,-1,-1},
{8,-1,-1,-1,7,-1,-1,-1,-1,11,6},
{8,-1,-1,-1,7,-1,-1,-1,-1,-1,12},
{-1,-1,102,10,-1,102,102,-1-1,-1,-1},
{-1,-1,104,104,-1,104,104,-1,-1,-1,-1},
{-1,-1,15,-1,-1,14,-1,-1,-1,-1,-1},
{-1,-1,106,106,-1,106,106,-1,-1,-1,-1},
{8,-1,-1,-1,7,-1,-1,-1,-1,16,6},
{-1,-1,102,-1,-1,102,102,-1,-1,-1,-1}
}; //-1表示出错,100表示接受,>100表示规约, <100表示移进.
string analsck="i= *()#AETF"; //分析表的符号(从0开始)
const int len=8; //文法数组长度
string wenfa[len]={"S","A ","E ","E","T ","T","F ","F"}; //文法左部(0已做掉)
//词法分析
int isidnumber(char x[]) //识别字母
{
int i=0;
char str[20];
ifstream infu("fuhao.txt",ios::in); //以读的方式打开id文件(符号表)
while(infu>>i>>str) //搜寻
{
if(strcmp(str,x)==0)
return i;
}
infu.close(); //关闭文件
ofstream outfu("fuhao.txt",ios::app); //以末尾补充写的入的方式打开id文件
outfu < < i < <'\t' < <x < <'\n';
outfu.close(); //关闭
return i;
}
string zhuanstr(char x[])
{
int i=0;
ostringstream Output;
while(x[i]!='\0')
{
char a=x[i];
Output < <a;
i ;
}
string s=Output.str();
return s;
}
string zhuanint (int x)
{
ostringstream Output;
Output < <x;
string s=Output.str();
return s;
}
//两个输出栈的函数string shuchu(string x[])
{
string chu;
for(int i=0;i <=top;i )
{
chu =x[i];
}
return chu;
}
void Scanner(ifstream inx,ofstream outx) //识别单词(要分析的,要写的二原式)
{
static char s='\0';
if(s=='\0')
s=inx.get();
if(s!=EOF) //文件没结束
{ //
if((s>='a'&&s <='z') | |(s>='A'&&s <='Z')) //第一个字符为字母
{ // int i=0;
char linshi[10]; //暂存分析过的字母
while((s>='a'&&s <='z') | |(s>='A'&&s <='Z') | |(s>='0'&&s <='9')) //第二个以后为字母或数字
{
linshi[i]=s;
i ;
s=inx.get();
}
linshi[i]='\0'; //上条件不符合时字符结束
outx < <"(i," < <isidnumber(linshi) < <")" < <'\n';
string linshii=zhuanstr(linshi);
zhen.assign(linshii,0,1); //???????????????关于语义栈
fruit.assign("i"); //用i表示字符赋给fruit
} //
else if((s==' ') | |(s=='*') | |(s=='(') | |(s==')') | |(s=='='))
{ // char linshi[10];
outx < <"(" < <s < <"," < <"_)" < <'\n';
linshi[0]=s;
linshi[1]='\0';
zhen.assign("_"); //关于语义栈
string ll=zhuanstr(linshi);
fruit.assign(ll,0,1);
s=inx.get();
// cout < <s;
} //
} // else
exit(0);
}
//取分析表元素函数
int hezo(int x,int y)
{
return analtable[x][y];
}
//两个转化函数
//分析函数=
void fenxi(ifstream infile,ofstream eryuan)
{ //1
int gg=1;
Scanner(infile,eryuan);
while(gg=1)
{//2
string ding=state[top]; //取状态栈顶
int heng=atoi(state[top].c_str()); //取横坐标
//string dang=fruit;
int zong=analsck.find(fruit[0],0);
int zhi=hezo(heng,zong);
if(zhi==100)
{ //3
string qian=shuchu(state);
string zhong=shuchu(stack);
string hou=shuchu(yuzhan);
cout < <setw(15) < <qian < <setw(15) < <zhong < <setw(15) < <hou < <setw(15) < <"100,分析成功!" < <endl;
gg=0;
}// 3
if(zhi>100)
{//4
int lin=zhi-100; //文法序号
string llin=wenfa[lin]; //得左部
int chang=llin.length(); //左部长度
string qian=shuchu(state);
string zhong=shuchu(stack);
string hou=shuchu(yuzhan);
cout < <setw(15) < <qian < <setw(15) < <zhong < <setw(15) < <hou < <"r" < <lin < <":" < <"用" < <lin < <"式归约";
top=top-chang; //规约
stack[top 1]=llin[0]; //左部入符号栈
int henggo=atoi(state[top].c_str()); //goto横坐标
int zonggo=analsck.find(llin[0],0); //goto纵
int zhigo=hezo(henggo,zonggo); //goto值
string zzhigo=zhuanint(zhigo); //goto值转string
cout < <"且GOTO(" < <henggo < <"," < <zonggo < <")=" < <zhigo < <"入栈" < <endl;
state[top 1]=zzhigo; //新状态入栈
if(chang>1)//语义
{ //5
if(lin==1)//写=进四元式
{ //6
ofstream xie("siyuanshi.txt",ios::app);
xie < <"(" < <siyuan[0] < <"," < <yuzhan[top 3] < <"," < <"_," < <yuzhan[top 1] < <")\n";
xie.close();
top ;
} //6
if(lin==2)
{ //7
ofstream xie("siyuanshi.txt",ios::app);
//string tt=zhuanint(siyuan);
xie < <"(" < <siyuan[1] < <"," < <yuzhan[top 1] < <"," < <yuzhan[top 3] < <",W" < <siyyuan < <")\n";
xie.close();
top ;
yuzhan[top] ="W" siyyuan;
siyyuan ;
} //7
if(lin==4)
{ //8
ofstream xie("siyuanshi.txt",ios::app);
string tt=zhuanint(siyyuan);
xie < <"(" < <siyuan[2] < <"," < <yuzhan[top 1] < <"," < <yuzhan[top 3] < <",W" < <siyyuan < <")\n";
xie.close();
top ;
yuzhan[top] ="W" siyyuan;
siyyuan ;
} //8
} //5
else top ;
} //4
if(zhi <100) //移进
{//9
string qian=shuchu(state);
string zhong=shuchu(stack);
string hou=shuchu(yuzhan);
cout < <setw(15) < <qian < <setw(15) < <zhong < <setw(15) < <hou < <setw(15) < <"ACTION[" < <ding < <"," < <fruit < <"]=s" < <zhi < <"," < <"新状态入栈" < <endl;
top ;
string lin=zhuanint(zhi);
state[top]=lin;
stack[top]=fruit;
yuzhan[top]=zhen;
Scanner(infile,eryuan);
}//9
} //2
} //1
//主函数
void main()
{
stack[top]='#';
//strcpy(state[top],"0");
//strcpy(yuzhan[top],"_");
state[top]="0";
yuzhan[top]="_";
char filename[30];
cout < <"输入要分析的文件名";
cin>>filename;
ifstream infile(filename,ios::in);
ofstream eryuan("eryuanshi.txt",ios::out);
ofstream xie("siyuanshi.txt",ios::out);
//ofstream fu("fuhao.txt",ios::out);
//siyuanshi.close();
eryuan.close();
//fu.close();
cout < <std::left < <setw(15) < <"状态栈" < <setw(15) < <"符号栈" < <setw(15) < <"语义栈" < <setw(15) < <"动作说明" < <endl;
cout < <"--------" < <endl;
fenxi(infile,eryuan);
}
网友回复:楼主 我是jf来的
网友回复:void Scanner(ifstream inx,ofstream outx)
fstream 是不答应拷贝构造的,你怎么通过编译的?
关键字:程序,只能,运行,一步,希望,
上一篇:希奇的C问题
下一篇:下面没有链接了











文章评论
共有 0 位网友发表了评论 此处只显示部分留言 点击查看完整评论页面