读书人

自己的词法分析器有错帮忙看一下,

发布时间: 2012-03-28 15:40:03 作者: rapoo

自己的词法分析器,有错,帮忙看一下
#include<iostream.h>
#include<string.h>
#define maxline 204800//源程序的最大长度
#define max 2000

#define m 2
char p[maxline]=" ";
char taken[maxline]="";
char b[max][100];
char con[max][100];
int point=0;
char a,s;
int i;
//bool letter();
char c[m][10]={ "else", "if"
};


char Getchar(){ //获取单个字符
if(point<maxline && p[point]!='\0')
{
a=p[point++];
return a ;
}
else
return 0;
}
void getbe(){ //去除空格

while(a==' ')
{

Getchar();

}
}
bool letter(){
char ch;
ch=Getchar();
if((ch>='a')&&(ch<='z')||(ch>='A')&&(ch<='Z'))
return true;
else
return false;


}


bool digit(){

char ch;
ch=Getchar();
if((ch>='0')&&(ch<='9'))
return true;
else return false;
}
void concatenation(char *str){

for(i=0;str[i]!='/0';i++)
str[i]=a;
str[i+1]='/0';



}
void retract(){

point--;


}
int reserve(char *str){

for(i=0;i<m;++i){
if(0==strcmp(c[i],str))
return i+1;


}
return 0;
}
int buildid(char *str){

for(i=0;i<max;++i)
if(0==strcmp(b[i],str))
return i;


strcpy(b[i],str);
return i;






}
int buildconst(char *str){

for(i=0;i<max;++i)
if(0==strcmp(con[i],str))
return i;

strcpy(con[i],str);
return i;



}
void main(){
int q;

cout<<"输入程序"<<endl;
cin.get(p,maxline);
a=Getchar();
getbe();
while(a!='/0'){
switch(a){
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
while(letter()||digit()){
concatenation(taken);
s=Getchar();
}
retract();
q=reserve(taken);
if(q==0){
buildid(taken);
cout<<" "<<b[i]<<endl;

}
else
cout<<c[i]<<" "<<endl;
break;
case'0':
case'1':
case'2':
case'3':
case'4':
case'5':
case'6':
case'7':
case'8':
case'9':
while(digit())
{ concatenation(taken);
Getchar();

}
retract();
buildconst(taken);
cout<<con[i]<<" "<<endl;
break;
case'+':
cout<<"+"<<" "<<endl;
break;
case'-':
cout<<"-"<<" "<<endl;
break;
case'*':
cout<<"*"<<" "<<endl;
break;
case'<':
Getchar();
if(a=='=')
cout<<"relop"<<"LE"<<endl;
else
{ retract();
cout<<"relop"<<"LT"<<endl;


}
break;
case'=':
Getchar();
if(a=='=')
cout<<"relop"<<"EQ"<<endl;
else
{ retract();
cout<<"="<<" "<<endl;

}
break;
case';':
cout<<";"<<endl;


break;
default:
cout<<"输入字符有错误"<<endl;


}
}

}



[解决办法]
3个'/0'要改为'\0'。
Getchar的使用比较混乱,既有通过全局变量a,又有通过返回值。调用的时机也有问题。
一般通过返回值来使用Getchar,而不用全局变量。
以下按通过全局变量a使用Getchar的方式修改一部分供参考:

C/C++ code
bool letter(){//char ch;//ch=Getchar();//if((ch>='a')&&(ch<='z')||(ch>='A')&&(ch<='Z'))if((a>='a')&&(a<='z')||(a>='A')&&(a<='Z'))return true;else  return false;}bool digit(){//char ch;//ch=Getchar();//if((ch>='0')&&(ch<='9'))if((a>='0')&&(a<='9'))return true;else return false;}void concatenation(char *str){  for(i=0;str[i]!='\0';i++); //  for(i=0;str[i]!='\0';i++)  str[i]=a;  str[i+1]='\0';}int buildid(char *str){  for(i=0;i<max&&b[i][0];++i) //for(i=0;i<max;++i)  if(0==strcmp(b[i],str))    return i;  strcpy(b[i],str);  return i;}switch(a){case 'a':case 'b':case 'c':case 'd':case 'e':case 'f':case 'g':case 'h':case 'i':case 'j':case 'k':case 'l':case 'm':case 'n':case 'o':case 'p':case 'q':case 'r':case 's':case 't':case 'u':case 'v':case 'w':case 'x':case 'y':case 'z'://还要加上case 'A':到case 'Z':while(letter()||digit()){concatenation(taken);//s=Getchar();Getchar();} 

读书人网 >C++

热点推荐