c语言词法分析器_c语言版_源代码(C language lexical analyzer _c language version _ source code)
c语言词法分析器_c语言版_源代码(C language lexical analyzer
_c language version _ source code)
#包括< stdio. h >
#包括<字符串。”
#包括<<程序。”
char * KEY0 [ ] = {”、“自动”、“破”、“案例”、“炭”、“常量”、“继续”、“默认”、“做”、“双”、“人”、“枚举”、“外部”、“浮”、“”、“去”、“如果”、“国际”、“长”、
“登记”、“回归”、“短”、“签署”、“大小”、“静”、“结
构”、“开关”、“类型”、“_complex”、“_imaginary”、“联盟”、“无”、“虚”、“不稳定”,“而”};
保留字表/ * * /
char * key1 [ ] = {“”、“(”、“)”、“[”、“]”、“{”、“}”、“,”、“;”、“'”};
分隔符表/ * * /
char *键[ ] = {“”、“+”、“-”、“*”、“/”、“%”、“<”、“>”、“= =”、“> =”、“< =“,”~=“,”、“&”、“| |”、“<”、“> >”、“~”、“|”、“^“,”“,”“,”,“,”“>”,“+”,“-”,“”,“”,“=”,“=”,“*”,“=”“}”;
运算符表/ * * /
int XX0 [ 35 ] [ 10 ],XX1,XX2 [ 31 ];
国际temp_key3 = 0,temp_c40 = 0,temp_c41 = 0,temp_c42 = 0,
temp_c43 = 0;
/ *******初始化函数******* /
无效()
{
int毫米;
对于(mm = 0;mm,34;采购经理+)
{
XX0 [毫米] = 0;
}
对于(mm = 0;毫米,< 9;毫米+ ++)
{
XX1 [毫米] = 0;
}
对于(mm = 0;mm,30;采购经理+)
{
XX2 [毫米] = 0;
}
*文件载;
如果((载= fopen(“KEY0 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:KEY0 .txt”);
返回;
}
关闭文件(载);
/ * * /建立保留字表文件:KEY0 .txt
如果((载= fopen(“key1 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:KEY1 .txt”);
返回;
}
/ * * /建立分隔符表文件:KEY1 .txt
如果((载= fopen(“key2 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:KEY2 .txt”);
返回;
}
关闭文件(载);
/ * * /建立运算符表文件:KEY2 .txt
如果((载= fopen(“KEY3 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:KEY3 .txt”);
返回;
}
关闭文件(载);
/ * * /建立标识符表文件:KEY3 .txt
如果((载= fopen(“C40 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:C40 .txt”);
返回;
}
关闭文件(载);
/ * * /建立整数类型常量表文件:C40 .txt
如果((载= fopen(“C41 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:C41 .txt”);
返回;
}
关闭文件(载);
/ * * /建立浮点类型常量表文件:C41 .txt
如果((载= fopen(“C42 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:C42 .txt”);
返回;
}
关闭文件(载);
/ * * / txt建立字符类型常量表文件:C42。
如果((载= fopen(“C43 .txt”、“W”))= = null)
{
printf(“错误~无法创建文件:C43 .txt”);
返回;
}
关闭文件(载);
/ * * /建立字符串类型常量表文件:C43 .txt
如果((载= fopen(“定义.txt”、“W”))= = null)
{
printf(“错误~无法创建文件:定义.txt”);
返回;
}
关闭文件(载);
/ * * / txt建立注释文件:定义。
如果((载= fopen(“输出文件”、“W”))= = null)
{
printf(“错误~无法创建文件:输出“txt”);
返回;
}
关闭文件(载);
/ * * / txt建立内部码文件:输出。
如果((载= fopen(“temp_key1”、“W”))= = null)
{
printf(“错误~无法创建文件:temp_key1”);
返回;
}
关闭文件(载);
建立保留字临时表文件:temp_key1 */ /*
如果((载= fopen(“temp_key3”、“W”))= = null)
{
printf(“错误~无法创建文件:temp_key3”);
返回;
}
关闭文件(载);
建立标识符临时文件:temp_key3 */ /*
如果((载= fopen(“temp_c40”、“W”))= = null)
{
printf(“错误~无法创建文件:temp_c40”);
返回;
}
关闭文件(载);
建立整数类型常量临时文件:temp_c40 */ /*
如果((载= fopen(“temp_c41”、“W”))= = null)
{
printf(“错误~无法创建文件:temp_c41”);
返回;
}
关闭文件(载);
建立浮点类型常量临时文件:temp_c41 */ /*
如果((载= fopen(“temp_c42”、“W”))= = null)
{
printf(“错误~无法创建文件:temp_c42”);
返回;
}
关闭文件(载);
建立字符类型常量临时文件:temp_c42 */ /*
如果((载= fopen(“temp_c43”、“W”))= = null)
{
printf(“错误~无法创建文件:temp_c43”);
返回;
}
关闭文件(载);
建立字符串类型常量临时文件:temp_c43 */ /*
}
/ *******保留字及标识符判断函数******* /
/ *******浮点类型判断函数******* /
无效intb_search(char *的话)
{
foutput文件*,* finput;
字符c;
字符cs [ 100 ];
int = 0;
int行= 0;
int value2 = 0;
如果(temp_c41 = = 0)
{
foutput = fopen(“temp_c41”,“”);
fprintf(foutput,“%s”,字);
关闭文件(foutput);
temp_c41 + +;
foutput = fopen(“C41 .txt”,“”);
fprintf(foutput,“4 T1 \ T1 T %s \n”,字);
关闭文件(foutput);
}
finput = fopen(“temp_c41”、“R”);
C =指针(finput);
而(c)~= EOF)
{
而(c)~=“n”
{
cs;
C =指针(finput);
}
cs =“0”;
CSI = 0;
行+;
如果(strcmp(CS,字)= = 0)
{
value2 = 1;
打破;
}
C =指针(finput);
}
关闭文件(finput);
如果(value2 = = 1)
{
foutput = fopen(“输出。txt”,“”);
fprintf(foutput,“4 T1 T T %s %d \n”,行,字);
关闭文件(foutput);
}
其他的
{
foutput = fopen(“temp_c41”,“”);
fprintf(foutput,“%s”,字);
关闭文件(foutput);
temp_c41 + +;
foutput = fopen(“输出。txt”,“”);
fprintf(foutput,“4 T1 T T %s %d \n”,temp_c41,字);
关闭文件(foutput);
foutput = fopen(“C40。txt”,“”);
fprintf(foutput,“4 T1 T T %s %d \n”,temp_c41,字);
关闭文件(foutput);
}
}
/ *******字符串常量判断函数******* /
无效cc_search(char *的话)
{
file * foutput, * finput;
char c;
char cs [100];
int csi = 0;
int line = 0;
int value2 = 0;
if (temp _ c = = 0)
{
foutput = fopen ("temp _ c", "a");
fprintf (foutput, "% s" n ", word);
fclose (foutput);
temp _ c + +;
foutput = fopen ("c43.txt", "a");
fprintf (foutput, "4 i t3 and t1 and t% s \ n", word);
fclose (foutput);
}
finput = fopen ("temp _ c", "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 i t3 ^ t ^ t% d% s \ n", line, word);
fclose (foutput);
}
else
{
foutput = fopen ("temp _ c", "a");
fprintf (foutput, "% s" n ", word);
fclose (foutput);
temp _ c + +;
foutput = fopen ("output.txt", "a");
fprintf (foutput, "4 i t3 ^ t ^ t% d% s \ n", the temp _ c, word);
fclose (foutput);
foutput = fopen ("c43.txt", "a");
fprintf (foutput, "4 i t3 ^ t ^ t% d% s \ n", the temp _ c, 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 i t2 and t1 and 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 i 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 i t2 * t% d% t% s \ n", the temp _ c42,
word);
fclose (foutput);
foutput = fopen ("c42.txt", "a");
fprintf (foutput, "4 i t2 * t% d% t% s \ n", the temp _ c42,
word);
fclose (foutput);
}
}
/ * * * * * * * 主扫描函数 * * * * * * * /
scan (void)
{
int count;
char chin;
file * fin;
file * agricultural development;
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"
文件名);
返回;
}
下巴=指针(鳍);
当(下巴)~= EOF)
{
对文件包含、宏定义进行处理/ * * /
如果(下巴= =“#”)
{
当(下巴)~=“>”
下巴=指针(鳍);
/ *下巴=指针(鳍);* /
}
对空格符、水平制表符进行处理/ * * /
else if((下巴= =”)| |(下巴= = '\t'))
{
;
}
对回车符进行处理/ * * /
否则如果(下巴=“n”)
{
;
}
对单引号内的字符常量进行处理/ * * /
否则如果(目标= [ 0 ])
{
如果(XX1 [ 9 ] = = 0)
{
四= fopen(“key1 .txt”,“”);
fprintf(4,“1 T9 \ T/T %c \n”,目标[ 0 ]);
关闭文件(四);
XX1 [ 9 ] = 1;
}
临时温度[ 0 ] =下巴;
下巴=指针(鳍);
临时温度[ 1 ] =下巴;
下巴=指针(鳍);
如果(下巴)~=目标[ 0 ]
{
临时温度[ 2 ] =下巴;
下巴=指针(鳍);
临时温度[ 3 ] =下巴;
临时值[ 4 ] =“0”;
}
其他的
{
临时温度[ 2 ] =下巴;
临时值[ 3 ] =“0”;
}
c_search(临时);
}
对双引号内的字符串常量进行处理/ * * /
否则,如果(颏=“”)
{
int = i 0;
临时文件“i”;
下巴=指针(鳍);
当(下巴)~=“”
{
体温[下巴];
下巴=指针(鳍);
}
临时[ i ] =“”;
温度[ i + 1 ] = \ 0;
cc_search(临时);
}
对保留字、标识符进行处理/ * * /
else if(((下巴> =“”)&(下巴< = Z))| |((下巴> =“”)&(下巴< = Z))| |(下巴= = '_ '))
{
int = i 0;
而(((下巴> =“”)&(下巴< = Z))| |((下巴> =“”)&(下巴< = Z))| |(下巴= = '_”)| |((下巴> = '0')&(下巴< = 9)))
{
体温[下巴];
下巴=指针(鳍);
}
温度“i =”0;
char_search(临时);
如果(下巴)~= EOF)
fseek(鳍,1l,seek_cur);
}
对整型、浮点型数据进行处理/ * * /
else if((下巴> = '0')&(下巴< = 9))
{
国际dotcount = 0;
int = i 0;
而(((下巴> = '0')&(下巴< = 9))| |(下巴= =“”))
{
如果(下巴=“。”)
dotcount + +;
如果(dotcount = = 2)
打破;
体温[下巴];
下巴=指针(鳍);
}
温度“i =”0;
如果(dotcount = = 1)
intb_search(临时);
其他的
inta_search(临时);
如果(下巴)~= EOF)
fseek(鳍,1l,seek_cur);
}
对注释进行处理/ * * /
否则,如果(下巴=“/”)
{
下巴=指针(鳍);
如果(下巴=“=”)
{
四= fopen(“输出。txt”,“”);
fprintf(4,“2 T30 \ T/T / = \n”);
关闭文件(四);
}
如果(下巴)~=“*”
{
四= fopen(“输出。txt”,“”);
fprintf(4,“2 T4 \ T T / \n”);
关闭文件(四);
fseek(鳍,1l,seek_cur);
}
否则如果(下巴=“*”)
{
计数= 0;
下巴=指针(鳍);
四= fopen(“定义。txt”,“”);
fprintf(4,“*”);
(计数)~= 2)
{
计数= 0;
当(下巴)~=“*”
{
fprintf (up everybody's life, "% c", chin).
chin = fgetc() (fin);
}
count + +;
fprintf (up everybody's life, "% c", chin).
chin = fgetc() (fin);
if (chin = = '/')
{
count + +;
fprintf (up everybody's life, "% c \ n", chin).
}
else
{
fprintf (up everybody's life, "% 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)
{
up everybody's life = fopen ("key2.txt", "a");
fprintf (up everybody's life, "2 \ t% d \ t \ t% s \ n", time,
temp.
fclose (up everybody's life.
xx2 [time] = 1;
}
up everybody's life = fopen ("output.txt", "a");
fprintf (up everybody's life, "2 \ t% d \ t \ t% s \ n", time,
temp.
fclose (up everybody's life.
break;
}
}
if (firstblood! = 1)
{
fseek() (fin, - 11, seek _ cur).
temp [1] = '\ 0';
for (time = 1; time < = 9; time + +)
{
if (strcmp (temp, key1 [time]) = = 0)
{
if (xx1 [time] = = 0)
{
up everybody's life = fopen ("key1.txt", "a");
fprintf (up everybody's life, "1 \ t% d \ t \ t% s \ n", time,
temp.
fclose (up everybody's life.
xx1 [time] = 1;
}
up everybody's life = fopen ("output.txt", "a");
fprintf (up everybody's life, "1 \ t% d \ t \ t% s \ n", time,
temp.
fclose (up everybody's life.
break;
}
}
for (time = 1; time < = 30; time + +)
{
if (strcmp (temp, key2 [time]) = = 0)
{
if (xx2 [time] = = 0)
{
up everybody's life = fopen ("key2.txt", "a");
fprintf (up everybody's life, "2 \ t% d \ t \ t% s \ n", time,
temp.
fclose (up everybody's life.
xx2 [time] = 1;
}
up everybody's life = fopen ("output.txt", "a");
fprintf (up everybody's life, "2 \ t% d \ t \ t% s \ n", time,
temp.
fclose (up everybody's life.
break;
}
}
}
}
}
chin = fgetc() (fin);
}
up everybody's life = fopen ("output.txt", "a");
fprintf (up everybody's life, "1 \ t6 \ t \ t} \ n");
fclose (up everybody's life.
}
/ * * * * * * * main函数 * * * * * * * /
void main ()
{
file * updates;
char charin;
char command = 'q'.
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * c语言词法
分析工具 * * * * * * * * * * * * * * * * * * * * \ n");
printf ("* * \ n");
printf ("* * \ n");
printf ("* 命令如下: * \ n");
printf ("* 0 - > 查看保留字表文件 * \ n");
Printf (* 1 / view delimiter table file *\n ");
Printf ("* 2 - > View operator table file *\n");
Printf ("* 3 / view identifier table file *\n");
Printf ("* 4 - > view type integer constant table *\n");
Printf (* 5 / view floating-point type constant table *\n ");
Printf ("* 6 - > view character type constant table *\n");
Printf ("* 7 - > View string type constant table *\n");
Printf ("* 8 -- > View comment file *\n");
Printf (* 9 - > view internal code file *\n ");
Printf ("*\n * --------------------------");
Printf ("*\n * Q / exit");
Printf
("*********************************************************
******\n");
Printf ("\n");
Load ();
Scan ();
Printf ("\n");
Printf (analysis completed)! \n ");
Getchar ();
Printf ("\n");
Printf (enter command:);
Command=getchar ();
While ((command! ='Q') & & (command! ='q'))
{
Switch (command)
{
Case'0':
{
Printf ("*************************\n");
Printf ("\n");
Fread=fopen ("key0.txt", "R");
Charin=fgetc (FREAD);
While (CHARIN, =EOF)
{
Putchar (CHARIN);
Charin=fgetc (FREAD);
}
Printf ("\n");
Printf ("*************************\n");
Printf ("\n");
Printf (enter command:);
Break;
}
Case'1':
{
Printf ("*************************\n");
Printf ("\n");
Fread=fopen ("key1.txt", "R");
Charin=fgetc (FREAD);
While (CHARIN, =EOF)
{
Putchar (CHARIN);
Charin=fgetc (FREAD);
}
Printf ("\n");
Printf ("*************************\n");
Printf ("\n");
Printf (enter command:);
Break;
}
Case'2':
{
Printf ("*************************\n");
Printf ("\n");
Fread=fopen ("key2.txt", "R");
Charin=fgetc (FREAD);
While (CHARIN, =EOF)
{
Putchar (CHARIN);
Charin=fgetc (FREAD);
}
Printf ("\n");
Printf ("*************************\n");
Printf ("\n");
Printf (enter command:);
Break;
}
Case'3':
{
Printf ("*************************\n");
Printf ("\n");
Fread=fopen ("key3.txt", "R");
Charin=fgetc (FREAD);
While (CHARIN, =EOF)
{
Putchar (CHARIN);
Charin=fgetc (FREAD);
}
Printf ("\n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
case 4:
{
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
fread = fopen ("c40.txt", "r");
charin = fgetc (fread);
while (charin! = eof)
{
putchar (charin);
charin = fgetc (fread);
}
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
case 5:
{
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
fread = fopen ("c41.txt", "r");
charin = fgetc (fread);
while (charin! = eof)
{
putchar (charin);
charin = fgetc (fread);
}
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
case 6:
{
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
fread = fopen ("c42.txt", "r");
charin = fgetc (fread);
while (charin! = eof)
{
putchar (charin);
charin = fgetc (fread);
}
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
case 7:
{
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
fread = fopen ("c43.txt", "r");
charin = fgetc (fread);
while (charin! = eof)
{
putchar (charin);
charin = fgetc (fread);
}
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
case 8:
{
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
fread = fopen ("defination.txt", "r");
charin = fgetc (fread);
while (charin! = eof)
{
putchar (charin);
charin = fgetc (fread);
}
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
case 9:
{
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
fread = fopen ("output.txt", "r");
charin = fgetc (fread);
while (charin! = eof)
{
putchar (charin);
charin = fgetc (fread);
}
printf ("\ n");
printf ("* * * * * * * * * * * * * * * * * * * * * * * * * n");
printf ("\ n");
printf ("请输入命令:");
break;
}
}
command = getchar ();
}
}
本文档为【c语言词法分析器_c语言版_源代码(C language lexical analyzer _c language version _ source code)】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
该文档来自用户分享,如有侵权行为请发邮件ishare@vip.sina.com联系网站客服,我们会及时删除。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。