📄 letax(cifa).c
字号:
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void init()
{
char *key[]={" ","auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"};
char *limit[]={" ","(",")","[","]","->",".","!","++","--","&","~",
"*","/","%","+","-","<<",">>","<","<=",">",">=","==","!=","&&","||",
"=","+=","-=","*=","/=",",",";","{","}","#","_","'"};
FILE *fp;
int i;
char c;
fp=fopen("k.txt","w");
if (fp!=NULL)
{
for(i=1;i<=32;i++)
fprintf(fp,"%s\n",key[i]);
fclose(fp);
}else printf("Error: CAN NOT OPEN K.TXT");
fp=fopen("l.txt","w");
if(fp!=NULL)
{
for(i=1;i<=38;i++)
fprintf(fp,"%s\n",limit[i]);
c='"';
fprintf(fp,"%c\n",c);
fclose(fp);
} /*初始化运算、限界符表*/
else printf("Error :CAN NOT OPEN l.exe");
fp=fopen("i.txt","w");
fclose(fp); /*初始化标识符表*/
fp=fopen("c.txt","w");
fclose(fp); /*初始化常数表*/
fp=fopen("output.txt","w");
fclose(fp); /*初始化输出文件*/
}
/*******************************************
十进制转二进制函数
********************************************/
char * dtb(char *buf)
{
int temp[20];
char *binary;
int value=0,i=0,j;
for(i=0;buf[i]!='\0';i++)
value=value*10+(buf[i]-0x30);
if(value==0)
{
binary=malloc(2*sizeof(char));
binary[0]='0';
binary[1]='\0';
return(binary);
}
i=0;
while(value!=0)
{
temp[i++]=value%2;
value/=2;
}
temp[i]='\0';
binary=malloc((i+1)*sizeof(char));
for(j=0;j<=i-1;j++)
binary[j]=(char)(temp[i-j-1]+48);
binary[i]='\0';
return(binary);
}
int find(char *buf,int type,int command)
{
int number=0;
FILE *fp;
char c;
char temp[30];
int i=0;
switch(type)
{
case 1: fp=fopen("k.txt","r");break;
case 2: fp=fopen("i.txt","r");break;
case 3: fp=fopen("c.txt","r");break;
case 4: fp=fopen("l.txt","r");
}
c=fgetc(fp);
while(c!=EOF)
{
while(c!='\n')
{
temp[i++]=c;
c=fgetc(fp);
}
temp[i]='\0';
i=0;
number++;
if(strcmp(temp,buf)==0)
{
fclose(fp);
return(number); /*若找到,返回在相应表中的序号*/
}
else
c=fgetc(fp);
}
if(command==1)
{
fclose(fp);
return(0);
}
fprintf(fp,"%s\n",buf);
fclose(fp);
return(number+1);
}
void cs_manage(char *buffer)
{
FILE *fp;
char *pointer;
int result;
pointer=dtb(buffer);
result=find(pointer,3,2);
fp=fopen("output.txt","a");
fprintf(fp,"%s\t\t\t3\t\t\t\n",buffer,result);
fclose(fp);
}
void ch_manage(char *buffer)
{
FILE *fp;
int result;
result=find(buffer,1,1);
fp=fopen("output.txt","a");
if(result!=0)
fprintf(fp,"%s\t\t\t1\t\t\t\n",buffer,result);
else
{
result=find(buffer,2,2);
fprintf(fp,"%s\t\t\t2\t\t\t\n",buffer,result);
}
fclose(fp);
}
void er_manage(char error,int lineno)
{
printf("\nerror: %c ,line %d",error,lineno);
}
void scanner()
{
FILE *fpin,*fpout;
char filename[20];
char ch;
int i=0,line=1;
int count,result,errorno=0;
char array[30];
char *word;
printf("the file name:");
scanf("%s",filename);
if((fpin=fopen(filename,"r"))==NULL)
{
printf("cannot open file");
return;
}
ch=fgetc(fpin);
while(ch!=EOF)
{
i=0;
if(isalpha(ch)||(ch=='_')) /*如果是字母开始 ,处理字符关键字或者标识符*/
{
while(isalnum(ch)||(ch=='_'))
{
array[i++]=ch;
ch=fgetc(fpin);
}
word=(char *)malloc((i+1)*sizeof(char));
memcpy(word,array,i);
word[i]='\0';
ch_manage(word);
if(ch!=EOF)
fseek(fpin,-1L,SEEK_CUR);
}
else if(isdigit(ch))
{
while(isdigit(ch))
{
array[i++]=ch;
ch=fgetc(fpin);
}
word=(char *)malloc((i+1)*sizeof(char));
memcpy(word,array,i);
word[i]='\0';
cs_manage(word);
if(ch!=EOF)
fseek(fpin,-1L,SEEK_CUR);
}
else if(isspace(ch)) ;
else if(ch=='\n') line++;
else if(ch=='/')
{
ch=fgetc(fpin);
if(ch=='=')
{
fpout=fopen("output.txt","a");
fprintf(fpout,"/=\t\t\t4\t\t\t\n");
fclose(fpout);
}
else if(ch!='*')
{
fpout=fopen("output.txt","a");
fprintf(fpout,"/\t\t\t4\t\t\t\n");
fclose(fpout);
fseek(fpin,-1L,SEEK_CUR);
}
else if(ch=='*')
{
count=0;
ch=fgetc(fpin);
while(count!=2)
{
count=0;
while(ch!='*')
ch=fgetc(fpin);
count++;
ch=fgetc(fpin);
if(ch=='/')
count++;
else
ch=fgetc(fpin);
}
}
}
else if(ch=='"')
{
fpout=fopen("output.txt","a");
fprintf(fpout,"%c\t\t\t4\t\t\t\n",ch);
ch=fgetc(fpin);
while(ch!='"')
ch=fgetc(fpin);
fprintf(fpout,"%c\t\t\t4\t\t\t\n",ch);
fclose(fpout);
}
else
{
array[0]=ch;
ch=fgetc(fpin);
if(ch!=EOF)
{
array[1]=ch;
word=(char *)malloc(3*sizeof(char));
memcpy(word,array,2);
word[2]='\0';
result=find(word,4,1);
if(result==0)
{
word=(char *)malloc(2*sizeof(char));
memcpy(word,array,1);
word[1]='\0';
result=find(word,4,1);
if(result==0)
{
er_manage(array[0],line);
errorno++;
fseek(fpin,-1L,SEEK_CUR);
}
else
{ /*若为单字符运算、限界符,写入输出文件并将扫描文件指针回退一个字符*/
fpout=fopen("output.txt","a");
fprintf(fpout,"%s\t\t\t4\t\t\t\t\n",word,result);
fclose(fpout);
fseek(fpin,-1L,SEEK_CUR);
}
}
else
{ /*若为双字符运算、限界符,写输出文件*/
fpout=fopen("output.txt","a");
fprintf(fpout,"%s\t\t\t4\t\t\t\n",word,result);
fclose(fpout);
}
}
else
{ /*若读入的下一个字符为文件结束符*/
word=(char *)malloc(2*sizeof(char));
memcpy(word,array,1);
word[1]='\0';
result=find(word,4,1); /*只考虑是否为单字符运算、限界符*/
if(result==0)
er_manage(array[0],line);
else
{ /*若是,写输出文件*/
fpout=fopen("output.txt","a");
fprintf(fpout,"%s\t\t\t4\t\t\t\n",word,result);
fclose(fpout);
}
}
}
ch=fgetc(fpin);
}
fclose(fpin);
printf("\nThere are %d error(s).\n",errorno); /*报告错误字符个数*/
}
/*******************************************
主函数
********************************************/
main()
{
char ch;
char st[10];
FILE *fp=NULL;
while(1)
{
init();
scanner();
fp=fopen("output.txt","r");
if (fp!=NULL)
{
while((ch=fgetc(fp))!=EOF)
printf("%c",ch);
}
fclose(fp);
printf("Enter q/Q to quit:");
scanf("%s",st);
if (st[0]=='q'||st[0]=='Q') break;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -