C词法分析请教高手,帮着改改(用简单的C知识,学的很少)
#include <stdio.h>#include <stdlib.h>
#include <string.h>
char c,a[30],b[30];
int i=0,count0=0,count1=0,count2=0,count3=0,count5=0;
bool flag=false;
FILE *fp1,*fp2;
char *key[]={" ","auto","bool","break","case","char","complex","const","continue","default","restrict","do","double",
"else","enum","extern","float","for","goto","if","imaginary","inline","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"}; /*C语言所有关键字,共37个*/
char *oper[]={" ","!","%","^","&","*","-","+","=","~","|",".",
"<",">","/","?",":","+=","-=","*=","/=","%=","<<==",">>==","|=","->","++",
"--","<<",">>","<=",">=","==","!=","&&","||"};/*运算符*/
char *sep[]={" ","#","(",")","[","]","\'",";",":","\"","{","}",",","\\"," "};/*分隔符*/
/************************
是否为保留字
************************/
int iskey(char *a){
for(i=1;i<37;i++)
if(strcmp(a,key[i])==0){
fprintf(fp2,"0\t\t\t%d\t\t\t%s\n",i,a);
return 1;
}
return 0;
}
/**************************
是否为运算符
***************************/
void isoper(char *a){
for(i=1;i<35;i++)
if(strcmp(a,oper[i])==0){
fprintf(fp2,"2\t\t\t%d\t\t\t%s\n",i,a);
}
}
/************************
是否为分隔符
************************/
void issep(char *a){
for(i=1;i<14;i++)
if(strcmp(a,sep[i])==0){
fprintf(fp2,"1\t\t\t%d\t\t\t%s\n",i,a);
}
}
/*是否为标识符*/
void issyb(char *a){
FILE *fp0;
int k;
k=strlen(a);
if(((a[0]>='A')&&(a[0]<='Z'))||((a[0]>='a')&&(a[0]<='z'))||(a[0]=='_'))
for(i=1;i<k-1;i++){
if((a[i]<48||a[i]>122)||(a[i]>57&&a[i]<65)||(a[i]>90&&a[i]<97)&&
(a[i]!='_')) ;
else flag=true;}
if(flag=true){
count5++;
fp0=fopen("标识符.txt","a");
fprintf(fp0,"%s\t\t\t3\t\t\t%d\n",a,count5);
fclose(fp0);
fprintf(fp2,"3\t\t\t%d\t\t\t%s\n",count5,a);
}
}
/**********************
是否为整型或浮点型常量
***********************/
void isdigit(char *a){
FILE *fp6;
if((a[0]>='0')&&(a[0]<='9')){
for(i=0;i<100;i++){
if(a[i]=='.') flag=true;}
if(flag==true){ /*浮点型常量输出*/
fp6=fopen("常量.txt","a");
count1++;
fprintf(fp6,"%s\t\t\t4\t1\t\t%d\n",a,count1);
fclose(fp6);
fprintf(fp2,"4\t\t\t1\t%d\t\t%s\n",count1,a);
}
else{ /*整型常量*/
fp6=fopen("常量.txt","a");
count0++;
fprintf(fp6,"%s\t\t\t4\t0\t\t%d\n",a,count0);
fclose(fp6);
fprintf(fp2,"4\t\t\t0\t%d\t\t%s\n",count0,a);
}
}
}
/***********************************
字符、字符串常量判断
************************************/
void ischar(char *a){
FILE *fp6;
int k;
k=strlen(a);
if(((a[1]>='A')&&(a[1]<='Z'))||((a[1]>='a')&&(a[1]<='z'))||(a[1]=='_')){
fp6=fopen("常量.txt","a");
if((a[0]=='\'')&&(a[2]=='\'')){ /*是字符常量*/
count2++;
fprintf(fp6,"%s\t\t\t4\t2\t\t%d\n",a,count2);
fprintf(fp2,"4\t\t\t2\t%d\t\t%s\n",count2,a);
}
else
if((a[0]=='\"')&&(a[k-1]=='\"')){ /*是字符串常量*/
count3++;
fprintf(fp6,"%s\t\t\t4\t3\t\t%d\n",a,count3);
fprintf(fp2,"4\t\t\t3\t%d\t\t%s\n",count3,a);
}
fclose(fp6);
}
}
/******************************
分割单词
******************************/
void cut(){
int j=0;
while(!feof(fp1)){
do{
a[i++]=fgetc(fp1);
}while(a[i-1]!='\0'&& a[i-1]!='\n'&&a[i-1]!=' '&& a[i-1]!=EOF);
a[i-1]=0;
j=iskey(a);
isoper(a);
issep(a);
if(j==0){
issyb(a);}
j=0;
isdigit(a);
ischar(a);
if(a[i]=='\0'||a[i]=='\n'||a[i]==' ')
i=0;
}
fclose(fp1);
}
/*各表初始化程序*/
void init()
{
FILE *fp;
int i;
char c;
fp=fopen("保留字.txt","w"); /*初始化保留字表*/
for(i=1;i<=37;i++){
if(i<=9)
fprintf(fp,"%-20s 0 0%d\n",key[i],i);
else
fprintf(fp,"%-20s 0 %2d\n",key[i],i);
}
fclose(fp);
fp=fopen("分隔符.txt","w"); /*初始化限制符表*/
for(i=1;i<=13;i++){
if(i<=9)
fprintf(fp,"%-20s 1 0%d\n",sep[i],i);
else
fprintf(fp,"%-20s 1 %2d\n",sep[i],i);
}
fclose(fp);
fp=fopen("运算符.txt","w"); /*初始化运算符表*/
for(i=1;i<=35;i++){
if(i<=9)
fprintf(fp,"%-20s 2 0%d\n",oper[i],i);
else
fprintf(fp,"%-20s 2 %2d\n",oper[i],i);
}
fclose(fp);
c='"';
fprintf(fp,"%c\n",c);
fclose(fp);
fp=fopen("标识符.txt","w"); /*初始化标识符表*/
fclose(fp);
fp=fopen("常量.txt","w"); /*初始化常数表*/
fclose(fp);
fp=fopen("output.txt","w"); /*初始化输出文件*/
fclose(fp);
}
void main()
{
init();
fp1=fopen("input.txt","r");
fp2=fopen("output.txt","w");
cut();
fclose(fp1);
fclose(fp2);
}
[ 本帖最后由 shiyuanren 于 2009-8-16 18:32 编辑 ]