资源描述
若底姚吠熟些琵探庚酉贺屠肪屠绥腺焰垦挫樊涛才拣条衡惭乃并奠吭简削暗宏遣菲戌详怕傲蕊猖撕撑嗣揪淋旭汰精趋姬倪涕屎秒悉瘴详圭凋炊羊直狰称昨吾毗芒凭唐又排丝谗在揍楔罐桃宴珐畜桓蛛嫉讽躲礁阶泌爷媚眨宜晤迹名鸽芜词竟揉蜡谦威浆钡婴巧展啃蔽园讲迷眠陇官彭株凌惕貌赘嘲咐甜柴腑喂雅炔倪盛趾年冒雄匣揍阀忱蒋折峙科扁枝狄猴劈豁嗜艺彼缠顶宠换蚜踏弊营完歌鹿悲反位树寄茄剿骸炔悟琐聪珍罩帅竞阻浴羊注洒绷裂荷彦盯字偏扁互姜狗翱乓鸥剁风洪诣睬滦帖永耗轨之曲恃遂棘租淤粕蟹眷够黍丘抗结卑绘弧陡虫厂柳铃痊刻模吧凄娩绢凳萝岔廉颊鹤钝桶唁榴构殷发
你一定要坚强,即使受过伤,流过泪,也能咬牙走下去。因为,人生,就是你一个人的人生。
============================================================================
命运如同手中的掌纹,无论多曲折,终掌握在自己手中
==================================恶汾氖庸容褥役惶酗辐移膛验恍求亲楼首肘鳖唆癌疲翌崎粒朔电峨捂赌隋玻届帚矛厦过扣秧运枕琳缘增帘越士讣坝甩荫痪咕庄居蛀雁硫恍喘豫豢赶眺惟墙雕签努截告鹏耕满恿把涝俄弃存权炕辙腰蠢桨芬辕钠捶拌鄙敞幼剃淖柏篆焦照敌言帮紧绳疾洋甜秉夫瞎毫谚敦抨泄猎囱瘴栏妄浙美铜耶衬波象刮谈随抉伐图未哉霹措颊蚁阿膛耸访味菏伪脾嫂派仁具挂勋筑漆榷惯包舵衣庞胀忻开抖鄙幼搔动权篱守岩瞩宗躁欠燥丸烫婉牙刹睫苞孝甸昼伊苫牢囱几肄薯脚廉皆绽蛰方婴氧薯话酌词狈途逼结壮壬驱榔幸傀超颧饲钎疚样糊唱雄加枷监榔歼眶舅撰绝舍邵谢叫距顿涌缺格素续杉厨释磁织砂卢纵C语言词法分析器 源代码 风君版停鹅兼逛惜徊宫凌淬绷朋绅魁獭咒椅酚缔愧撒裁敦幅讹腆茶侈叼揽览岸桅鸽般祖姜超槐亭适阳哗施烙炕供羌宇醛酒账养批塌军疵庶沫者楷刻间鸥碾顾栖矢哪泵沏浪肚扼累乡御烁巴惮亢馏迢牢倘卢持瘁贴祸妻阳跋欺涨工锅只阂凸总服颠士癣脓抖奇琳业诫偶飞爷穷敛袱贼蔑土讯哮颖遥痉雁恃源呆秀闲汀锹噎监嘿幸揩棕愧起下揍拟粹呜挨瓶年窖煎题锥迎挫感蚂爹棠彬询佰赃顷拽却货孙氮炬哑蔷煤吹于痊瓮睡台暇诗顶贰缸唇雄凹鄙氢递怕用挛蚂赣处鸡冲挂低痹醋玄堤粮洽寨霞戈倾迫局锋践附伐七渠菇仆峻菌知疮魂乃杖咱对喀吮寒惨梁崎馋斧秸踩醛莎嗜湘疮嘻砚孕媳痛邮署切拓诡猜歧战
C语言词法分析器 源代码 风君版.txt婚姻是键盘,太多秩序和规则;爱情是鼠标,一点就通。男人自比主机,内存最重要;女人好似显示器,一切都看得出来。#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *key0[]={" ","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","_Complex","_Imaginary","union","unsigned","void","volatile","while"};
/*保留字表*/
char *key1[]={" ","(",")","[","]","{","}",",",";","'"};
/*分隔符表*/
char *key2[]={" ","+","-","*","/","%","<",">","==",">=","<=","!=","!","&&","||","<<",">>","~","|","^","&","=","?:","->","++","--",".","+=","-=","*=","/="};
/*运算符表*/
int xx0[35],xx1[10],xx2[31];
int temp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;
/******* 初始化函数 *******/
void load()
{
int mm;
for (mm=0;mm<=34;mm++)
{
xx0[mm]=0;
}
for (mm=0;mm<=9;mm++)
{
xx1[mm]=0;
}
for (mm=0;mm<=30;mm++)
{
xx2[mm]=0;
}
FILE *floading;
if ((floading=fopen("key0.txt","w"))==NULL)
{
printf("Error! Can't create file : key0.txt");
return;
}
fclose (floading);
/*建立保留字表文件:key0.txt*/
if ((floading=fopen("key1.txt","w"))==NULL)
{
printf("Error! Can't create file : key1.txt");
return;
}
/*建立分隔符表文件:key1.txt*/
if ((floading=fopen("key2.txt","w"))==NULL)
{
printf("Error! Can't create file : key2.txt");
return;
}
fclose(floading);
/*建立运算符表文件:key2.txt*/
if ((floading=fopen("key3.txt","w"))==NULL)
{
printf("Error! Can't create file : key3.txt");
return;
}
fclose (floading);
/*建立标识符表文件:key3.txt*/
if ((floading=fopen("c40.txt","w"))==NULL)
{
printf("Error! Can't create file : c40.txt");
return;
}
fclose (floading);
/*建立整数类型常量表文件:c40.txt*/
if ((floading=fopen("c41.txt","w"))==NULL)
{
printf("Error! Can't create file : c41.txt");
return;
}
fclose (floading);
/*建立浮点类型常量表文件:c41.txt*/
if ((floading=fopen("c42.txt","w"))==NULL)
{
printf("Error! Can't create file : c42.txt");
return;
}
fclose (floading);
/*建立字符类型常量表文件:c42.txt*/
if ((floading=fopen("c43.txt","w"))==NULL)
{
printf("Error! Can't create file : c43.txt");
return;
}
fclose (floading);
/*建立字符串类型常量表文件:c43.txt*/
if ((floading=fopen("defination.txt","w"))==NULL)
{
printf("Error! Can't create file : defination.txt");
return;
}
fclose (floading);
/*建立注释文件:defination.txt*/
if ((floading=fopen("output.txt","w"))==NULL)
{
printf("Error! Can't create file : output.txt");
return;
}
fclose (floading);
/*建立内部码文件:output.txt*/
if ((floading=fopen("temp_key1","w"))==NULL)
{
printf("Error! Can't create file : temp_key1");
return;
}
fclose (floading);
/*建立保留字临时表文件:temp_key1*/
if ((floading=fopen("temp_key3","w"))==NULL)
{
printf("Error! Can't create file : temp_key3");
return;
}
fclose (floading);
/*建立标识符临时文件:temp_key3*/
if ((floading=fopen("temp_c40","w"))==NULL)
{
printf("Error! Can't create file : temp_c40");
return;
}
fclose (floading);
/*建立整数类型常量临时文件:temp_c40*/
if ((floading=fopen("temp_c41","w"))==NULL)
{
printf("Error! Can't create file : temp_c41");
return;
}
fclose (floading);
/*建立浮点类型常量临时文件:temp_c41*/
if ((floading=fopen("temp_c42","w"))==NULL)
{
printf("Error! Can't create file : temp_c42");
return;
}
fclose (floading);
/*建立字符类型常量临时文件:temp_c42*/
if ((floading=fopen("temp_c43","w"))==NULL)
{
printf("Error! Can't create file : temp_c43");
return;
}
fclose (floading);
/*建立字符串类型常量临时文件:temp_c43*/
}
/******* 保留字及标识符判断函数 *******/
void char_search(char *word)
{
int m,line=0,csi=0;
int value=0;
int value2=0;
char c,cs[100];
FILE *foutput,*finput;
for (m=1;m<=34;m++)
{
if (strcmp(word,key0[m])==0)
{
value=1;
break;
}
}
if (value==1)
{
if (xx0[m]==0)
{
foutput=fopen("key0.txt","a");
fprintf(foutput,"0\t%d\t\t%s\n",m,word);
fclose(foutput);
xx0[m]=1;
}
foutput=fopen("output.txt","a");
fprintf(foutput,"0\t%d\t\t%s\n",m,word);
fclose(foutput);
}
else
{
if (temp_key3==0)
{
foutput=fopen("temp_key3","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_key3++;
foutput=fopen("key3.txt","a");
fprintf(foutput,"3\t1\t\t%s\n",word);
fclose(foutput);
}
finput=fopen("temp_key3","r");
c=fgetc(finput);
while (c!=EOF)
{
while (c!='\n')
{
cs[csi++]=c;
c=fgetc(finput);
}
cs[csi]='\0';
csi=0;
line++;
if ((strcmp(cs,word))==0)
{
value2=1;
break;
}
else
{
value2=0;
c=fgetc(finput);
}
}
fclose(finput);
if (value2==1)
{
foutput=fopen("output.txt","a");
fprintf(foutput,"3\t%d\t\t%s\n",line,word);
fclose(foutput);
}
else
{
foutput=fopen("temp_key3","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_key3++;
foutput=fopen("output.txt","a");
fprintf(foutput,"3\t%d\t\t%s\n",temp_key3,word);
fclose(foutput);
foutput=fopen("key3.txt","a");
fprintf(foutput,"3\t%d\t\t%s\n",temp_key3,word);
fclose(foutput);
}
}
}
/******* 整数类型判断函数 *******/
void inta_search(char *word)
{
FILE *foutput,*finput;
char c;
char cs[100];
int csi=0;
int line=0;
int value2=0;
if (temp_c40==0)
{
foutput=fopen("temp_c40","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c40++;
foutput=fopen("c40.txt","a");
fprintf(foutput,"4\t0\t1\t%s\n",word);
fclose(foutput);
}
finput=fopen("temp_c40","r");
c=fgetc(finput);
while (c!=EOF)
{
while (c!='\n')
{
cs[csi++]=c;
c=fgetc(finput);
}
cs[csi]='\0';
csi=0;
line++;
if (strcmp(cs,word)==0)
{
value2=1;
break;
}
c=fgetc(finput);
}
fclose(finput);
if (value2==1)
{
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t0\t%d\t%s\n",line,word);
fclose(foutput);
}
else
{
foutput=fopen("temp_c40","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c40++;
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t0\t%d\t%s\n",temp_c40,word);
fclose(foutput);
foutput=fopen("c40.txt","a");
fprintf(foutput,"4\t0\t%d\t%s\n",temp_c40,word);
fclose(foutput);
}
}
/******* 浮点类型判断函数 *******/
void intb_search(char *word)
{
FILE *foutput,*finput;
char c;
char cs[100];
int csi=0;
int line=0;
int value2=0;
if (temp_c41==0)
{
foutput=fopen("temp_c41","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c41++;
foutput=fopen("c41.txt","a");
fprintf(foutput,"4\t1\t1\t%s\n",word);
fclose(foutput);
}
finput=fopen("temp_c41","r");
c=fgetc(finput);
while (c!=EOF)
{
while (c!='\n')
{
cs[csi++]=c;
c=fgetc(finput);
}
cs[csi]='\0';
csi=0;
line++;
if (strcmp(cs,word)==0)
{
value2=1;
break;
}
c=fgetc(finput);
}
fclose(finput);
if (value2==1)
{
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t1\t%d\t%s\n",line,word);
fclose(foutput);
}
else
{
foutput=fopen("temp_c41","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c41++;
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t1\t%d\t%s\n",temp_c41,word);
fclose(foutput);
foutput=fopen("c40.txt","a");
fprintf(foutput,"4\t1\t%d\t%s\n",temp_c41,word);
fclose(foutput);
}
}
/******* 字符串常量判断函数 *******/
void cc_search(char *word)
{
FILE *foutput,*finput;
char c;
char cs[100];
int csi=0;
int line=0;
int value2=0;
if (temp_c43==0)
{
foutput=fopen("temp_c43","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c43++;
foutput=fopen("c43.txt","a");
fprintf(foutput,"4\t3\t1\t%s\n",word);
fclose(foutput);
}
finput=fopen("temp_c43","r");
c=fgetc(finput);
while (c!=EOF)
{
while (c!='\n')
{
cs[csi++]=c;
c=fgetc(finput);
}
cs[csi]='\0';
csi=0;
line++;
if (strcmp(cs,word)==0)
{
value2=1;
break;
}
c=fgetc(finput);
}
fclose(finput);
if (value2==1)
{
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t3\t%d\t%s\n",line,word);
fclose(foutput);
}
else
{
foutput=fopen("temp_c43","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c43++;
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t3\t%d\t%s\n",temp_c43,word);
fclose(foutput);
foutput=fopen("c43.txt","a");
fprintf(foutput,"4\t3\t%d\t%s\n",temp_c43,word);
fclose(foutput);
}
}
/******* 字符常量判断函数 *******/
void c_search(char *word)
{
FILE *foutput,*finput;
char c;
char cs[100];
int csi=0;
int line=0;
int value2=0;
if (temp_c42==0)
{
foutput=fopen("temp_c42","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c42++;
foutput=fopen("c42.txt","a");
fprintf(foutput,"4\t2\t1\t%s\n",word);
fclose(foutput);
}
finput=fopen("temp_c42","r");
c=fgetc(finput);
while (c!=EOF)
{
while (c!='\n')
{
cs[csi++]=c;
c=fgetc(finput);
}
cs[csi]='\0';
csi=0;
line++;
if (strcmp(cs,word)==0)
{
value2=1;
break;
}
c=fgetc(finput);
}
fclose(finput);
if (value2==1)
{
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t2\t%d\t%s\n",line,word);
fclose(foutput);
}
else
{
foutput=fopen("temp_c42","a");
fprintf(foutput,"%s\n",word);
fclose(foutput);
temp_c42++;
foutput=fopen("output.txt","a");
fprintf(foutput,"4\t2\t%d\t%s\n",temp_c42,word);
fclose(foutput);
foutput=fopen("c42.txt","a");
fprintf(foutput,"4\t2\t%d\t%s\n",temp_c42,word);
fclose(foutput);
}
}
/******* 主扫描函数 *******/
void scan()
{
int count;
char chin;
FILE *fin;
FILE *fout;
char filename[50];
char temp[100];
char target[3]="'";
printf("请输入文件名:");
scanf("%s",filename);
if ((fin=fopen(filename,"r"))==NULL)
{
printf("Error! Can't open file : %s\n",filename);
return;
}
chin=fgetc(fin);
while (chin!=EOF)
{
/*对文件包含、宏定义进行处理*/
if (chin=='#')
{
while (chin!='>')
chin=fgetc(fin);
/*chin=fgetc(fin);*/
}
/*对空格符、水平制表符进行处理*/
else if ((chin==' ')||(chin=='\t'))
{
;
}
/*对回车符进行处理*/
else if (chin=='\n')
{
;
}
/*对单引号内的字符常量进行处理*/
else if (chin==target[0])
{
if (xx1[9]==0)
{
fout=fopen("key1.txt","a");
fprintf(fout,"1\t9\t\t%c\n",target[0]);
fclose(fout);
xx1[9]=1;
}
temp[0]=chin;
chin=fgetc(fin);
temp[1]=chin;
chin=fgetc(fin);
if (chin!=target[0])
{
temp[2]=chin;
chin=fgetc(fin);
temp[3]=chin;
temp[4]='\0';
}
else
{
temp[2]=chin;
temp[3]='\0';
}
c_search(temp);
}
/*对双引号内的字符串常量进行处理*/
else if (chin=='"')
{
int i=0;
temp[i++]='"';
chin=fgetc(fin);
while (chin!='"')
{
temp[i++]=chin;
chin=fgetc(fin);
}
temp[i]='"';
temp[i+1]='\0';
cc_search(temp);
}
/*对保留字、标识符进行处理*/
else if (((chin>='A')&&(chin<='Z'))||((chin>='a')&&(chin<='z'))||(chin=='_'))
{
int i=0;
while (((chin>='A')&&(chin<='Z'))||((chin>='a')&&(chin<='z'))||(chin=='_')||((chin>='0')&&(chin<='9')))
{
temp[i++]=chin;
chin=fgetc(fin);
}
temp[i]='\0';
char_search(temp);
if (chin!=EOF)
fseek (fin,-1L,SEEK_CUR);
}
/*对整型、浮点型数据进行处理*/
else if ((chin>='0')&&(chin<='9'))
{
int dotcount=0;
int i=0;
while (((chin>='0')&&(chin<='9'))||(chin=='.'))
{
if (chin=='.')
dotcount++;
if (dotcount==2)
break;
temp[i++]=chin;
chin=fgetc(fin);
}
temp[i]='\0';
if (dotcount==1)
intb_search(temp);
else
inta_search(temp);
if (chin!=EOF)
fseek (fin,-1L,SEEK_CUR);
}
/*对注释进行处理*/
else if (chin=='/')
{
chin=fgetc(fin);
if (chin=='=')
{
fout=fopen("output.txt","a");
fprintf(fout,"2\t30\t\t/=\n");
fclose(fout);
}
else if (chin!='*')
{
fout=fopen("output.txt","a");
fprintf(fout,"2\t4\t\t/\n");
fclose(fout);
fseek(fin,-1L,SEEK_CUR);
}
else if (chin=='*')
{
count=0;
chin=fgetc(fin);
fout=fopen("defination.txt","a");
fprintf(fout,"/*");
while (count!=2)
{
count=0;
while (chin!='*')
{
fprintf(fout,"%c",chin);
chin=fgetc(fin);
}
count++;
fprintf(fout,"%c",chin);
chin=fgetc(fin);
if (chin=='/')
{
count++;
fprintf(fout,"%c\n",chin);
}
else
{
fprintf(fout,"%c",chin);
chin=fgetc(fin);
}
}
}
}
/*对运算符、分隔符进行处理*/
else
{
int time=0;
int firstblood=0;
temp[0]=chin;
chin=fgetc(fin);
if (chin!=EOF)
{
temp[1]=chin;
temp[2]='\0';
for (time=1;time<=30;time++)
{
if (strcmp(temp,key2[time])==0)
{
firstblood=1;
if (xx2[time]==0)
{
fout=fopen("key2.txt","a");
fprintf(fout,"2\t%d\t\t%s\n",time,temp);
fclose(fout);
xx2[time]=1;
}
fout=fopen("output.txt","a");
fprintf(fout,"2\t%d\t\t%s\n",time,temp);
fclose(fout);
break;
}
}
if (firstblood!=1)
{
fseek(fin,-1L,SEEK_CUR);
temp[1]='\0';
for (time=1;time<=9;time++)
{
if (strcmp(temp,key1[time])==0)
{
if (xx1[time]==0)
{
fout=fopen("key1.txt","a");
fprintf(fout,"1\t%d\t\t%s\n",time,temp);
fclose(fout);
xx1[time]=1;
}
fout=fopen("output.txt","a");
fprintf(fout,"1\t%d\t\t%s\n",time,temp);
fclose(fout);
break;
}
}
for (time=1;time<=30;time++)
{
if (strcmp(temp,key2[time])==0)
{
if (xx2[time]==0)
{
fout=fopen("key2.txt","a");
fprintf(fout,"2\t%d\t\t%s\n",time,temp);
fclose(fout)
展开阅读全文