读书人

各位快来看!

发布时间: 2012-03-17 19:06:28 作者: rapoo

各位大虾快来看!!!!!!!!!!
#include "stdafx.h "
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <malloc.h>
#include <ctype.h>
#include <conio.h>
#define NULL 0
FILE *fp;
char ch;
char *keyword[8]={ "do ", "begin ", "else ", "end ", "if ", "then ", "var ", "while "};
char *operatornum[4]={ "+ ", "- ", "* ", "/ "};
char *comparison[6]={ " < ", " <= ", "= ", "> ", "> = ", " <> "};
char *interpunction[6]={ ", ", "; ", ":= ", ". ", "( ", ") "};
//////////////////////////////////////////////////////////////////////////////////////////
bool search(char searchstr[],int wordtype)
{
int i;
switch (wordtype)
{
case 1:for(i=0;i <=7;i++)
{
if(strcmp(keyword[i],searchstr)==0)
return(true);

}
case 2:{
for(i=0;i <=3;i++)
{
if(strcmp(operatornum[i],searchstr)==0)
return(true);
}
break;
}
case 3: for(i=0;i <=5;i++)
{
if(strcmp(comparison[i],searchstr)==0)
return(true);
}
case 4: for(i=0;i <=5;i++)
{
if(strcmp(interpunction[i],searchstr)==0)
return(true);
}


}
return(false);
}


///////////////////////////////////////////////////////////////////////////////////////////

char letterprocess (char ch)//字母处理函数
{
int i=-1;
char letter[20];
while (isalnum(ch)!=0)
{
letter[++i]=ch;
ch=fgetc(fp);
};
letter[i+1]= '\0 ';
if (search(letter,1))
{
printf( " <%s,-> \n ",letter);
//strcat(letter, "\n ");
//fputs( ' < ' letter '> \n ',outp);
}
else
{
printf( " <indentifier,%s> \n ",letter);
//strcat(letter, "\n ");
//fputs(letter,outp);
}
return(ch);
}
///////////////////////////////////////////////////////////////////////////////////////////

char numberprocess(char ch)//数字处理程序
{
int i=-1;
char num[20];
while (isdigit(ch)!=0)
{
num[++i]=ch;
ch=fgetc(fp);
}
if(isalpha(ch)!=0)
{
while(isspace(ch)==0)


{
num[++i]=ch;
ch=fgetc(fp);
}
num[i+1]= '\0 ';
printf( "错误!非法标识符:%s\n ",num);
goto u;
}
num[i+1]= '\0 ';
printf( " <num,%s> \n ",num);
//strcat(num, "\n ");
//fputs(num,outp);
u: return(ch);
}

//////////////////////////////////////////////////////////////////////////////////////////////

char otherprocess(char ch)
{
int i=-1;
char other[20];
if (isspace(ch)!=0)
{
ch=fgetc(fp);
goto u;
}
while ((isspace(ch)==0)&&(isalnum(ch)==0))
{
other[++i]=ch;
ch=fgetc(fp);
}
other[i+1]= '\0 ';
if (search(other,2))
printf( " <relop,%s> \n ",other);
else
if (search(other,3))
printf( " <%s,-> \n ",other);
else
if (search(other,4))
printf( " <%s,-> \n ",other);
else
printf( "错误!非法字符:%s\n ",other);
u: return (ch);
}


/////////////////////////////////////////////////////////////////////////////////////////////
void main ()
{
char str,c;
printf( "**********************************词法分析器************************************\n ");
//outp=fopen( "二元式表.txt ", "w ");
if ((fp=fopen( "源程序.txt ", "r "))==NULL)
printf( "源程序无法打开!\n ");
else
{
str =fgetc(fp);
while (str!=EOF)
{
if (isalpha(str)!=0)
str=letterprocess(str);
else
{
if (isdigit(str)!=0)
str=numberprocess(str);
else
str=otherprocess(str);
}

};
printf( "词法分析结束,谢谢使用!\n ");
printf( "点任意键退出!\n ");
}
c=getch();
}


怎么总是有
Cannot open include file: 'stdafx.h ': No such file or directory
错误


[解决办法]
在project -- settings -- C++ -- Category -- Precompiled Header里,
选不使用预编译头
[解决办法]
去掉 'stdafx.h '
[解决办法]
如果不是 VC,
那么去掉这个 #include "stdafx.h "
[解决办法]
如果是VC,
那么这个文件在建立工程的时候回自动生成 ~



或者也可以不使用它:
在 project settings 中选择不使用 预编译头
[解决办法]
BCB去掉 #include "stdafx.h "可以编译
[解决办法]
如果是在VC6下,可以这样:
build-> clean
然后再编译就不会有问题了

读书人网 >C++

热点推荐