C语言词法分析器源代码风君版Word下载.docx
- 文档编号:18032690
- 上传时间:2022-12-13
- 格式:DOCX
- 页数:34
- 大小:18.48KB
C语言词法分析器源代码风君版Word下载.docx
《C语言词法分析器源代码风君版Word下载.docx》由会员分享,可在线阅读,更多相关《C语言词法分析器源代码风君版Word下载.docx(34页珍藏版)》请在冰豆网上搜索。
long"
register"
return"
sho
rt"
signed"
sizeof"
static"
struct"
switch"
typedef"
_Complex"
_Imaginar
y"
union"
unsigned"
void"
volatile"
while"
};
/*保留字表*/
char*key1[]={"
"
("
)"
["
]"
{"
}"
;
'
/*分隔符表*/
char*key2[]={"
+"
-"
*"
/"
%"
>
=="
="
!
&
||"
~"
|
^"
?
:
->
++"
--"
."
+="
-="
*="
/="
/*运算符表*/
intxx0[35],xx1[10],xx2[31];
inttemp_key3=0,temp_c40=0,temp_c41=0,temp_c42=0,temp_c43=0;
/*******初始化函数*******/
voidload()
{
intmm;
for(mm=0;
mm<
=34;
mm++)
xx0[mm]=0;
}
=9;
xx1[mm]=0;
=30;
xx2[mm]=0;
FILE*floading;
if((floading=fopen("
key0.txt"
w"
))==NULL)
printf("
Error!
Can'
tcreatefile:
key0.txt"
);
return;
fclose(floading);
/*建立保留字表文件:
key0.txt*/
key1.txt"
key1.txt"
/*建立分隔符表文件:
key1.txt*/
key2.txt"
key2.txt"
fclose(floading);
/*建立运算符表文件:
key2.txt*/
key3.txt"
key3.txt"
/*建立标识符表文件:
key3.txt*/
c40.txt"
c40.txt"
/*建立整数类型常量表文件:
c40.txt*/
c41.txt"
c41.txt"
/*建立浮点类型常量表文件:
c41.txt*/
c42.txt"
c42.txt"
/*建立字符类型常量表文件:
c42.txt*/
c43.txt"
c43.txt"
/*建立字符串类型常量表文件:
c43.txt*/
defination.txt"
defination.txt"
/*建立注释文件:
defination.txt*/
output.txt"
output.txt"
/*建立内部码文件:
output.txt*/
temp_key1"
temp_key1"
/*建立保留字临时表文件:
temp_key1*/
temp_key3"
temp_key3"
/*建立标识符临时文件:
temp_key3*/
temp_c40"
temp_c40"
/*建立整数类型常量临时文件:
temp_c40*/
temp_c41"
temp_c41"
/*建立浮点类型常量临时文件:
temp_c41*/
temp_c42"
temp_c42"
/*建立字符类型常量临时文件:
temp_c42*/
temp_c43"
temp_c43"
/*建立字符串类型常量临时文件:
temp_c43*/
/*******保留字及标识符判断函数*******/
voidchar_search(char*word)
intm,line=0,csi=0;
intvalue=0;
intvalue2=0;
charc,cs[100];
FILE*foutput,*finput;
for(m=1;
m<
m++)
if(strcmp(word,key0[m])==0)
value=1;
break;
if(value==1)
if(xx0[m]==0)
foutput=fopen("
a"
fprintf(foutput,"
0\t%d\t\t%s\n"
m,word);
fclose(foutput);
xx0[m]=1;
else
if(temp_key3==0)
%s\n"
word);
temp_key3++;
3\t1\t\t%s\n"
finput=fopen("
r"
c=fgetc(finput);
while(c!
=EOF)
='
\n'
)
cs[csi++]=c;
cs[csi]='
\0'
csi=0;
line++;
if((strcmp(cs,word))==0)
value2=1;
value2=0;
fclose(finput);
if(value2==1)
3\t%d\t\t%s\n"
line,word);
temp_key3,word);
/*******整数类型判断函数*******/
voidinta_search(char*word)
charc;
charcs[100];
intcsi=0;
intline=0;
if(temp_c40==0)
temp_c40++;
4\t0\t1\t%s\n"
if(strcmp(cs,word)==0)
4\t0\t%d\t%s\n"
temp_c40,word);
/*******浮点类型判断函数*******/
voidintb_search(char*word)
if(temp_c41==0)
temp_c41++;
4\t1\t1\t%s\n"
4\t1\t%d\t%s\n"
temp_c41,word);
/*******字符串常量判断函数*******/
voidcc_search(char*word)
if(temp_c43==0)
temp_c43++;
4\t3\t1\t%s\n"
4\t3\t%d\t%s\n"
temp_c43,word);
/*******字符常量判断函数*******/
voidc_search(char*word)
if(temp_c42==0)
temp_c42++;
4\t2\t1\t%s\n"
4\t2\t%d\t%s\n"
temp_c42,word);
/*******主扫描函数*******/
voidscan()
intcount;
charchin;
FILE*fin;
FILE*fout;
charfilename[50];
chartemp[100];
chartarget[3]="
请输入文件名:
scanf("
%s"
filename);
if((fin=fopen(filename,"
topenfile:
%s\n"
chin=fgetc(fin);
while(chin!
/*对文件包含、宏定义进行处理*/
if(chin=='
#'
/*chin=fgetc(fin);
*/
/*对空格符、水平制表符进行处理*/
elseif((chin=='
'
)||(chin=='
\t'
))
/*对回车符进行处理*/
elseif(chin=='
/*对单引号内的字符常量进行处理*/
elseif(chin==target[0])
if(xx1[9]==0)
fout=fopen("
fprintf(fout,"
1\t9\t\t%c\n"
target[0]);
fclose(fout);
xx1[9]=1;
temp[0]=chin;
temp[1]=chin;
if(chin!
=target[0])
temp[2]=chin;
temp[3]=chin;
temp[4]='
temp[3
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 语言 词法 分析器 源代码 风君版