c语言编写的词法分析程序.docx
《c语言编写的词法分析程序.docx》由会员分享,可在线阅读,更多相关《c语言编写的词法分析程序.docx(17页珍藏版)》请在冰豆网上搜索。
![c语言编写的词法分析程序.docx](https://file1.bdocx.com/fileroot1/2022-11/20/aa82e4f2-9a69-4b3a-90ed-e9234261f4c0/aa82e4f2-9a69-4b3a-90ed-e9234261f4c01.gif)
c语言编写的词法分析程序
#include
#include
#include
#include
#defineSPACE0x20//空格键
#defineCHANGE'c'//转义字符
#defineSTRING'S'//引号里的字符串
#defineBOUNDARY'B'//界符
#defineOPERATION'O'//运算符
#defineWORD'I'//标识符
#defineKEY_WORD'K'//关键字
#defineDIGIT'n'//数字,包括小数
#defineDOTH'd'//头文件,“.h”文件
#defineINT't'
#defineVOID'v'
#defineIF'f'
#defineELSE'e'
#defineWHILE'w'
#defineFOR'r'
#defineMAIN'm'
#definePRINTF'p'
#defineINCLUDE'u'
#defineFLOAT'l'
#defineKEYNUM10
#defineBOUNDNUM8
#defineOPERNUM7
#defineVALUENUM100
/************************************************************************/
/*datatype*/
/************************************************************************/
structbianliang
{
intid;
charname[20];
intvalue;
};
structchangliang
{
intdata;
};
charKeyWord[KEYNUM][8]={"int","void","if","else","while","for","main","printf","include","float"};
charBoundary[BOUNDNUM]={'"',';',',','(',')','#','{','}'};
charOperation[OPERNUM]={'+','-','*','/','=','<','>'};
charLogogram[KEYNUM]={INT,VOID,IF,ELSE,WHILE,FOR,MAIN,PRINTF,INCLUDE,FLOAT};
charValue[VALUENUM][8];
intValue_n=0;
intErrorLine=0;
intQuotationCount=0;//计算引号的数量
intQuotationFlag=0;//引号数量为奇数时为1,偶数时为0
charLastOpera=0;//用于判断"=="号存前一个"="号
/************************************************************************/
/*functiondescribe*/
/************************************************************************/
intisKeyWord(char*s)//是关键字返回1,否则返回0
{
inti;
for(i=0;i{
if(strcmp(KeyWord[i],s)==0)
{
returni+1;
}
}
return0;
}
intisBoundary(charch)//是界符
{
inti;
for(i=0;i{
if(Boundary[i]==ch)
{
returni+1;
}
}
return0;
}
intisOperation(charch)//是运算符
{
inti;
for(i=0;i{
if(Operation[i]==ch)
{
returni+1;
}
}
return0;
}
intisDigit(char*s)//是数字
{
inti,l=strlen(s);
for(i=0;i{
if(!
isdigit(s[i]))
{
break;
}
}
if(i>=l)
{
return1;
}
else
{
if('.'==s[i])
{
i++;
for(;i{
if(!
isdigit(s[i]))
{
break;
}
}
}
else
{
return0;
}
if(i>=l)
return1;
else
return0;
}
}
intisDotH(char*s)//是头文件
{
inti,l=strlen(s);
for(i=0;i{
if(!
isalpha(s[i]))
{
break;
}
}
if(i>=l)
{
return0;
}
else
{
if('.'==s[i])
{
i++;
if(s[i]=='h')
return1;
else
return0;
}
else
return0;
}
}
intisWord(char*s)//是标识符
{
inti=0,l=strlen(s);
if(isalpha(s[i]))
{
i++;
for(;i{
if(!
isalnum(s[i]))
{
break;
}
}
}
if(i>=l)
{
return1;
}
else
return0;
}
voidisWhat(int*i,char*str,FILE*fileOutput,FILE*fileOutput2)//判断字符串,并把相应类型存入文件
{
intn;
structbianliangtemp;
structchangliangt;
FILE*file=fopen("num.txt","ab");
if(1!
=*i)
{
str[--(*i)]='\0';
if(n=isKeyWord(str))
{
fputs(str,fileOutput);
fputc('\t',fileOutput);
fputc(KEY_WORD,fileOutput);
fputc('\t',fileOutput);
if(isKeyWord(str)>9)
fputc(isKeyWord(str)+'a'-10,fileOutput);
else
fputc(isKeyWord(str)+'0',fileOutput);
fputc('\t',fileOutput);
fputc(Logogram[n-1],fileOutput);
fputc('\n',fileOutput);
}
elseif(isWord(str))
{
fputs(str,fileOutput);
fputc('\t',fileOutput);
fputc(WORD,fileOutput);
fputc('\t',fileOutput);
for(n=0;n{
if(0==strcmp(Value[n],str))
break;
}
if(n==Value_n)
{
n=++Value_n;
strcpy(Value[n-1],str);
temp.id=n-1;
strcpy(temp.name,str);
temp.value=0;
fwrite(&temp,sizeof(structbianliang),1,fileOutput2);
fputc('0'+n,fileOutput);
}
else
{
fputc('0'+n+1,fileOutput);
}
fputc('\t',fileOutput);
fputc('i',fileOutput);
fputc('\n',fileOutput);
}
elseif(isDotH(str))
{
fputs(str,fileOutput);
fputc('\t',fileOutput);
fputc('\t',fileOutput);
fputc('\t',fileOutput);
fputc(DOTH,fileOutput);
fputc('\n',fileOutput);
}
elseif(isDigit(str))
{
fputs(str,fileOutput);
fputc('\t',fileOutput);
fputc('\t',fileOutput);
fputc('\t',fileOutput);
fputc(DIGIT,fileOutput);
fputc('\n',fileOutput);
t.data=atoi(str);
fwrite(&t,sizeof(structchangliang),1,file);
}
(*i)=0;
}
fclose(file);
}
voidwriteMessage(FILE*f)
{
fputs("字符\t类型\t附加值\t缩写\n",f);
}
voiderror()
{
printf("%dlineerror!
\n",ErrorLine);
exit
(1);
}
/************************************************************************/
/*main*/
/************************************************************************/
voidmain()
{
FILE*fileInput;
FILE*fileOutput;
FILE*fileOutput2;
charch,qtemp;
charstr[20];
inti;
intflag=0;
fileInput=fopen("1.cpp","r");
fileOutput=fopen("object.txt","w");
fileOutput2=fopen("variable.txt","w");
writeMessage(fileOutput);
if(NULL!
=fileInput)
{
i=0;
do
{
ch=fgetc(fileInput);
s