程序执行一步后就出现“应用程序错误”。

时间:2008-06-12 14:10:48   来源:论坛整理  作者:  编辑:chinaitzhe
编译和构建都无错,但程序执行时只输出一步,之后就出现应用程序错误的框,
写的是:0x0040a0b5指令引用"0xdddddddc"内存,该内存不能read.
是否跟程序是用string编的有关?
网友回复:假如说是CString我还有点信
string不会。

贴代码
网友回复:非法指针。。
网友回复:呵呵,代码非常长,都没人愿意帮我看。我贴了?做好预备!
#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;
}
string shuchuf(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 inx,ofstream outx)
{ //1
int gg=1;
Scanner(inx,outx);
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=top ;
string lin=zhuanint(zhi);
state[top]=lin;
stack[top]=fruit;
yuzhan[top]=zhen;

Scanner(inx,outx);
}//9
} //2
} //1
//主函数=
void main()
{
stack[top]='#';
//strcpy(state[top],"0");
//strcpy(yuzhan[top],"_");
state[0]="0";
yuzhan[0]="_";
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();
//fu.close();
cout < <std::left < <setw(15) < <"状态栈" < <setw(15) < <"符号栈" < <setw(15) < <"语义栈" < <setw(15) < <"动作说明" < <endl;
cout < <"-------" < <endl;
fenxi(infile,eryuan);
infile.close();
eryuan.close();

}



网友回复:非法指针?我是用一个int标记string数组的顶部,通过改变int更改和输出string数组,
这样不行吗?
网友回复:
C/C code





Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/





#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; 

} 

string shuchuf(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 inx,ofstream outx) 

{ //1    

          int gg=1; 

  Scanner(inx,outx); 

          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=top  ; 

      string lin=zhuanint(zhi); 

      state[top]=lin; 

      stack[top]=fruit; 

              yuzhan[top]=zhen; 



      Scanner(inx,outx); 

      }//9 

    } //2 

}  //1 

//主函数= 

void main() 

{ 

    stack[top]='#'; 

    //strcpy(state[top],"0"); 

//strcpy(yuzhan[top],"_"); 

state[0]="0"; 

    yuzhan[0]="_"; 

    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(); 

    //fu.close(); 

    cout < <std::left < <setw(15) < <"状态栈" < <setw(15) < <"符号栈" < <setw(15) < <"语义栈" < <setw(15) < <"动作说明" < <endl; 

    cout < <"-------" < <endl;  

    fenxi(infile,eryuan); 

    infile.close(); 

    eryuan.close(); 



} 








网友回复:这样也舒适点啊!看着!看你刚才的,你自己就知道不会有人看的!这样大家才更有爱好帮你看啊!写程序真的要美观!利于检查,还不至于写好了自己也不想看了啊!上面只是把你的代码重贴了上去!看看再说!呵呵!
网友回复:哈哈,确实是美观大方多了,谢啦!
网友回复:编译过程中的问题
void Scanner(ifstream inx,ofstream outx)//
void fenxi(ifstream inx,ofstream outx)
这两个函数参数都改成引用类型
网友回复:已经改了,但执行后就变成死循环了。
网友回复:帮你顶吧,这样的代码我看了头晕。
网友回复: while(gg=1)
这句

网友回复:
引用 11 楼 zbing0203 的回复:
while(gg=1)
这句

哈哈,太感谢啦,就是这一句害的我折腾了一天。
这个帖子分不高,你再在我上两个跟着个程序相关的帖子里留下言吧,
那两个帖子分高,我都给你。
网友回复:LZ太客气了
这种错误 其实跟踪调试一下就能找到的
网友回复:
引用 13 楼 zbing0203 的回复:
LZ太客气了
这种错误 其实跟踪调试一下就能找到的


呵呵,应该的。
关键字:程序,执行,一步,出现,应用程序,

文章评论

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