设为首页收藏本站

新微赢技术网

 找回密码
 注册
搜索
热搜: 回贴
查看: 871|回复: 0
打印 上一主题 下一主题

为什么出现内存不可read

[复制链接]
跳转到指定楼层
1#
发表于 2009-11-5 00:29:48 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
这是个简单的某个文法的LL(1),可对 i+i*i# 这类匹配
为什么我输入有'i'的时候就回出现什么内存不可读


#include<string.h>
#include<stdlib.h>
#include<iostream.h>
char a[]={'E','e','T','t','F','A','M'};
char b[]={'i','+','-','*','/','(',')','#'};
char *f[7][8]={{"Te","0","0","0","0","Te","0","0"},{"0","ATe","ATe","0","0","0","k","k"},
{"Ft","0","0","0","0","Ft","0","0"},{"0","k","k","MFt","MFt","0","k","k"},
{"i","0","0","0","0","(E)","0","0"},{"0","+","-","0","0","0","0","0"},
{"0","0","0","*","/","0","0","0"}};
struct stack{
char s[20];
int t;};
typedef struct stack *pseqstack;
pseqstack create(void)
{ pseqstack pastack;
pastack=(pseqstack)malloc(sizeof(struct stack));
if(pastack==NULL)
cout<<"out of space"<<endl;
else pastack->t=-1;
return(pastack);
}

void push(pseqstack pastack,char x)
{ pastack->t=pastack->t+1;
int i=0;
i=pastack->t;
pastack->s[i]=x;
}
void pop(pseqstack pastack)
{if(pastack->t==-1)
cout<<"underflow"<<endl;
else
pastack->t=pastack->t-1;
}
char top(pseqstack pastack)
{return(pastack->s[pastack->t]);
}

void macth(pseqstack pastack,char *p)
{ int j=0,m=0,n=0,q=0;
char tp;
char temp[10];

for(;;)
{tp=top(pastack);
if(tp=='#')
{if(p[j]=='#')
cout<<"macth"<<endl;
else
cout<<"error"<<endl;
break;
}
if(tp=='i'||tp=='+'||tp=='-'||tp=='*'||tp=='/'||tp=='('||tp==')')
{

if(tp==p[j])
{pop(pastack);
j++;
continue;}
else
{cout<<"error"<<endl;
break;
}
}
for(m=0;m<7;m++)
{if(a[m]==tp)
break;}
for(n=0;n<8;n++)
{if(b[n]==p[j])
break;}
strcpy(temp,f[m][n]);
if(strcmp(temp,"k")==0)

{pop(pastack);
continue;
}
if(strcmp(temp,"0")==0)
{cout<<"error"<<endl;
break;
}
else
{for(n=0;temp[n]!='\0';n++)
{
q=q+1;
}
for(n=q-1;n>=0;n--)
{
push(pastack,temp[n]);
}
}

}

}
void main()
{ char ru[10];
char j;

pseqstack pk=create();

j='#';
push(pk,j);
j='E';
push(pk,j);
cout<<"shu ru zifu chuan: ";
cin>>ru;
macth(pk,ru);
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则

申请友链|小黑屋|最新主题|手机版|新微赢技术网 ( 苏ICP备08020429号 )  

GMT+8, 2024-11-19 15:36 , Processed in 0.069890 second(s), 9 queries , Gzip On, Memcache On.

Powered by xuexi

© 2001-2013 HaiAn.Com.Cn Inc. 寰耽

快速回复 返回顶部 返回列表