编译原理课程设计词法分析器附含源代码.docx
《编译原理课程设计词法分析器附含源代码.docx》由会员分享,可在线阅读,更多相关《编译原理课程设计词法分析器附含源代码.docx(9页珍藏版)》请在冰豆网上搜索。
编译原理课程设计词法分析器附含源代码
编译原理-词法分析器的设计
一.设计说明及设计要求
一般来说,编译程序的整个过程可以划分为五个阶段:
词法分析、语法分析、中间代码生成、优化和目标代码生成。
本课程设计即为词法分析阶段。
词法分析阶段是编译过程的第一个阶段。
这个阶段的任务是从左到右一个字符一个字符地读入源程序,对构成源程序的字符流进行扫描和分解,从而识别出一个个单词(也称单词符号或符号)。
如保留字(关键字或基本字)、标志符、常数、算符和界符等等。
二.设计中相关关键字说明
1. 基本字:
也称关键字,如C语言中的if,else,while,do,for,case,break,return等。
2. 标志符:
用来表示各种名字,如常量名、变量名和过程名等。
3. 常数:
各种类型的常数,如12,6.88,和“ABC”等。
4. 运算符:
如+,-,*,/,%,<,>,<=,>=等。
5. 界符,如逗点,冒号,分号,括号,#,〈〈,〉〉等。
三 、程序分析
词法分析是编译的第一个阶段,它的主要任务是从左到右逐个字符地对源程序进行
扫描,产生一个个单词序列,用以语法分析。
词法分析工作可以是独立的一遍,把字符流的源程序变为单词序列,输出在一个中间文件上,这个文件做为语法分析程序的输入而继续编译过程。
然而,更一般的情况,常将词法分析程序设计成一个子程序,每当语法分析程序需要一个单词时,则调用该子程序。
词法分析程序每得到一次调用,便从源程序文件中读入一些字符,直到识别出一个单词,或说直到下一个单词的第一个字符为止。
四、模块设计
下面是程序的流程图
五 、程序介绍
在程序当前目录里建立一个文本文档,取名为infile.txt,所有需要分析的程序都写在此文本文档里,程序的结尾必须以“@”标志符结束。
程序结果输出在同一个目录下,文件名为outfile.txt,此文件为自动生成。
本程序所输出的单词符号采用以下二元式表示:
(单词种别,单词自身的值)如程序输出结果(57,"#")(33,"include")(52,"<")(33,"iostream")等。
程序的功能:
(1) 能识别C语言中所有关键字(共32个)(单词种别分别为1—32,详情见程序代码相关部分,下同)
(2) 能识别C语言中自定义的标示符(单词种别为33)
(3)能识别C语言中的常数(单词种别为0)
(4)能识别C语言中几乎所有运算符(单词种别分别为41—54)
(5) 能识别C语言中绝大多数界符(单词种别分别为55—66)
六 、运行结果
输入文件infile.txt
运行结果(输出文件outfile.txt)
七 、设计体会
八、附录部分(程序代码)
单词符号
类别编码
单词符号
类别编码
单词符号
类别编码
单词符号
类别编码
if
3
float
21
+
31
#
62
then
4
short
22
-
32
.
63
else
5
unsigned
23
*
33
64
while
6
continue
24
/
34
:
65
do
7
for
25
<
35
>=
39
begin
8
signed
26
>
36
<=
38
end
9
void
27
=
37
==
41
long
10
default
28
:
=
51
!
=
42
switch
11
goto
29
(
52
%
40
case
12
sizeof
30
)
53
标识符
1
enum
13
volatile
43
;
54
常数
2
register
14
auto
44
[
55
typedef
15
double
45
]
56
char
16
int
46
{
57
extern
17
struct
47
}
58
return
18
break
48
<<
59
union
19
static
49
>>
60
const
20
“
61
提示:
文件的打开和读写函数:
FILE*fp,*out;//定义文件指针
fp=fopen("infile.txt","r"))
如果打开文件"infile.txt"失败,则函数返回NULL,即fp=NULL,第二个参数“r”表示以只读方式打开,如果为”w”,则以可写方式打开
调用fgetc(fp)这个函数一次从fp所指向的文件读取一个字符
charch=fgetc(fp);
想文件写字符的函数为fprintf(FILE*fp,写进的内容)
比如下面的调用fprintf(outfile,"abcd\n")是把字符串“abcd”写到文件outfile的末尾,并且在后面加上了一个换行标志
文件读写完成后要用函数fclose(fp)关闭。
源代码
#include"stdio.h"
#include"string.h"
#include"ctype.h"
voidanalzid(FILE*output,char*p)
{
inti=0;
intcount=0;
if(isalpha(p[0]))
{if(strcmp(p,"if")==0){fprintf(output,"(3,if)\n");}
elseif(strcmp(p,"then")==0){fprintf(output,"(4,then)\n");}
elseif(strcmp(p,"else")==0){fprintf(output,"(5,else)\n");}
elseif(strcmp(p,"while")==0){fprintf(output,"(6,while)\n");}
elseif(strcmp(p,"do")==0){fprintf(output,"(7,do)\n");}
elseif(strcmp(p,"begin")==0){fprintf(output,"(8,begin)\n");}
elseif(strcmp(p,"end")==0){fprintf(output,"(9,end)\n");}
elseif(strcmp(p,"long")==0){fprintf(output,"(10,long)\n");}
elseif(strcmp(p,"switch")==0){fprintf(output,"(11,switch)\n");}
elseif(strcmp(p,"case")==0){fprintf(output,"(12,case)\n");}
elseif(strcmp(p,"enum")==0){fprintf(output,"(13,enum)\n");}
elseif(strcmp(p,"register")==0){fprintf(output,"(14,register)\n");}
elseif(strcmp(p,"typedef")==0){fprintf(output,"(15,typedef)\n");}
elseif(strcmp(p,"char")==0){fprintf(output,"(16,char)\n");}
elseif(strcmp(p,"extern")==0){fprintf(output,"(17,extern)\n");}
elseif(strcmp(p,"return")==0){fprintf(output,"(18,return)\n");}
elseif(strcmp(p,"union")==0){fprintf(output,"(19,union)\n");}
elseif(strcmp(p,"const")==0){fprintf(output,"(20,const)\n");}
elseif(strcmp(p,"float")==0){fprintf(output,"(21,float)\n");}
elseif(strcmp(p,"short")==0){fprintf(output,"(22,short)\n");}
elseif(strcmp(p,"unsigned")==0){fprintf(output,"(23,unsigned)\n");}
elseif(strcmp(p,"continue")==0){fprintf(output,"(24,continue)\n");}
elseif(strcmp(p,"for")==0){fprintf(output,"(25,for)\n");}
elseif(strcmp(p,"signed")==0){fprintf(output,"(26,signed)\n");}
elseif(strcmp(p,"void")==0){fprintf(output,"(27,void)\n");}
elseif(strcmp(p,"default")==0){fprintf(output,"(28,default)\n");}
elseif(strcmp(p,"goto")==0){fprintf(output,"(29,goto)\n");}
elseif(strcmp(p,"sizeof")==0){fprintf(output,"(30,sizeof)\n");}
elseif(strcmp(p,"volatile")==0){fprintf(output,"(43,volatile)\n");}
elseif(strcmp(p,"auto")==0){fprintf(output,"(44,auto)\n");}
elseif(strcmp(p,"double")==0){fprintf(output,"(45,double)\n");}
elseif(strcmp(p,"int")==0){fprintf(output,"(46,int)\n");}
elseif(strcmp(p,"struct")==0){fprintf(output,"(47,struct)\n");}
elseif(strcmp(p,"break")==0){fprintf(output,"(48,break)\n");}
elseif(strcmp(p,"static")==0){fprintf(output,"(49,static)\n");}
else{fprintf(output,"(1,%s)\n",p);}
}
else
{for(;i<(int)strlen(p);i++)if(isdigit(p[i]))count++;
if(count==(int)strlen(p))
{fprintf(output,"(2,%s)\n",p);}
else
if(p[0]=='_'&&(isalpha(p[1])))
{fprintf(output,"(1,%s)\n",p);}
else{fprintf(output,"%s未定义\n",p);}
}
}
voidanalzsy(FILE*outfile,char*p)
{
if(strcmp(p,"=")==0){fprintf(outfile,"(37,=)\n");}
elseif(strcmp(p,"+")==0){fprintf(outfile,"(31,+)\n");}
elseif(strcmp(p,"-")==0){fprintf(outfile,"(32,-)\n");}
elseif(strcmp(p,"*")==0){fprintf(outfile,"(33,*)\n");}
elseif(strcmp(p,"/")==0){fprintf(outfile,"(34,/)\n");}
elseif(strcmp(p,"(")==0){fprintf(outfile,"(52,()\n");}
elseif(strcmp(p,")")==0){fprintf(outfile,"(53,))\n");}
elseif(strcmp(p,"[")==0){fprintf(outfile,"(55,[)\n");}
elseif(strcmp(p,"]")==0){fprintf(outfile,"(56,])\n");}
elseif(strcmp(p,"{")==0){fprintf(outfile,"(57,{)\n");}
elseif(strcmp(p,"}")==0){fprintf(outfile,"(58,})\n");}
elseif(strcmp(p,"<<")==0){fprintf(outfile,"(59,<<)\n");}
elseif(strcmp(p,">>")==0){fprintf(outfile,"(60,>>)\n");}
elseif(strcmp(p,"'")==0){fprintf(outfile,"(61,')\n");}
elseif(strcmp(p,"#")==0){fprintf(outfile,"(62,#)\n");}
elseif(strcmp(p,".")==0){fprintf(outfile,"(64,.)\n");}
elseif(strcmp(p,"*")==0){fprintf(outfile,"(33,*)\n");}
elseif(strcmp(p,"/")==0){fprintf(outfile,"(34,/)\n");}
elseif(strcmp(p,"%")==0){fprintf(outfile,"(40,%)\n");}
elseif(strcmp(p,",")==0){fprintf(outfile,"(64,,)\n");}
elseif(strcmp(p,":
")==0){fprintf(outfile,"(65,:
)\n");}
elseif(strcmp(p,";")==0){fprintf(outfile,"(54,;)\n");}
elseif(strcmp(p,">")==0){fprintf(outfile,"(36,>)\n");}
elseif(strcmp(p,"<")==0){fprintf(outfile,"(35,<)\n");}
elseif(strcmp(p,">=")==0){fprintf(outfile,"(39,>=)\n");}
elseif(strcmp(p,"<=")==0){fprintf(outfile,"(38,<=)\n");}
elseif(strcmp(p,"==")==0){fprintf(outfile,"(41,==)\n");}
elseif(strcmp(p,"!
=")==0){fprintf(outfile,"(42,!
=)\n");}
elseif(strcmp(p,"")==0);
elseif(strcmp(p,"\n")==0);
else{fprintf(outfile,"%s未定义\n",p);}
}
voidmain()
{FILE*fp,*out;
inti=0,x=0,y=0;
intEA=0;
charch,str[10000],idstr[10],systr[2];
if((fp=fopen("infile.txt","r"))==NULL)
{printf("Cannotopeninfile!
\n");
exit(0);}
if((out=fopen("outfile.txt","w"))==NULL)
{printf("Cannotopenoutfile!
\n");
exit(0);}
ch=fgetc(fp);
while(ch!
=EOF)
{str[i]=ch;str[i+1]='\0';i++;ch=fgetc(fp);}
i=0;
while
(1)
{
if(str[i]=='@')break;
else
if((str[i]>='a'&&str[i]<='z')||(str[i]>='A'&&str[i]<='Z')||
(str[i]>='0'&&str[i]<='9')||(str[i]=='_'))
{idstr[x]=str[i];idstr[x+1]='\0';x++;i++;EA=1;}
else
{
x=0;
if((strlen(idstr)!
=0)&&(EA))analzid(out,idstr);
idstr[x]='\0';
if(str[i]<'0'||(str[i]>'9'&&str[i]<'A')||
(str[i]>'Z'&&str[i]<'a')||str[i]>'z')
{
if(str[i]!
='!
'&&str[i]!
='='&&str[i]!
='<'&&str[i]!
='>')
{
systr[y]=str[i];systr[y+1]='\0';analzsy(out,systr);i++;
}
else
if((str[i]=='!
'&&str[i+1]=='=')||(str[i]=='='&&str[i+1]=='=')||
(str[i]=='>'&&str[i+1]=='=')||(str[i]=='<'&&str[i+1]=='=')||
(str[i]=='<'&&str[i+1]=='<')||(str[i]=='>'&&str[i+1]=='>'))
{
systr[y]=str[i];systr[y+1]=str[i+1];systr[y+2]='\0';
analzsy(out,systr);i++;i++;
}
else
{
systr[y]=str[i];systr[y+1]='\0';analzsy(out,systr);i++;
}}}}
printf("-----全部结果已经存入outfile.txt文档-----\n");
fprintf(out,"--------完成--------");
fclose(fp);
fclose(out);
}