词法分析器的设计与实现.docx
《词法分析器的设计与实现.docx》由会员分享,可在线阅读,更多相关《词法分析器的设计与实现.docx(15页珍藏版)》请在冰豆网上搜索。
![词法分析器的设计与实现.docx](https://file1.bdocx.com/fileroot1/2023-1/23/771cb894-638e-43b7-aa83-a31f98e760d2/771cb894-638e-43b7-aa83-a31f98e760d21.gif)
词法分析器的设计与实现
词法分析器的设计与实现
《编译原理》课程实验报告
实验题目:
某种简单程序语言的词法分析器的设计与实现
专业:
计算机科学与技术
班级:
11060341
学号:
11060341
姓名:
实验目的:
设计一个词法分析程序,理解词法分析器实现的原理,掌握程序设计语言中的各类单词的词法分析方法,加深对词法分析原理的理解。
实验任务:
词法分析是从左向右扫描每行源程序的符号,拼成单词,换成统一的二元式(单词种别,单词符号的属性值)表示。
对给定的程序经过词法分析器识别一个个单词符号,并以二元式
(单词种别,单词符号的属性值)显示,本程序则是经过对给定程序段分析后以单词符号和文字提示显示)
实验流程:
程序清单:
#include
#include
#include
usingnamespacestd;
intk=0;
structword
{
charname[10];
intkind;
}word[1000];
charkey[35][10]={"scanf","short","int","long","float","double","char","struct","union",
"printf","typedef","const","unsigned","signed","extern","register","static",
"volatile","void","if","else","switch","case","for","do","while","goto",
"continue","break","default","sizeof","return","include","bool"
};
boolcmp(chara[])
{
inti;
for(intk=0;k<35;k++)
{
if(strcmp(a,key[k])==0)
return1;
}
return0;
}
intmain()
{
#ifdefLOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
intp,q,flag;
chara[1000],b[10],ch;
while(gets(a))
{
p=0;
intlen=strlen(a);
while(p{
ch=a[p];
memset(b,0,sizeof(b));
while(ch=='')
{
p++;
ch=a[p];
}
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='_')
{
flag=0;
q=0;
while((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')||ch=='_'||(ch>='0'&&ch<='9'))
{
if((ch>='0'&&ch<='9')||ch=='_')
flag=1;
b[q++]=ch;
p++;
ch=a[p];
}
if(flag==1)
{
strcpy(word[k].name,b);
word[k++].kind=1;
}
elseif(flag==0)
{
if(ch=='\''||ch=='"')
{
strcpy(word[k].name,b);
word[k++].kind=2;
}
elseif(cmp(b)==1)
{
strcpy(word[k].name,b);
word[k++].kind=3;
}
else
{
strcpy(word[k].name,b);
word[k++].kind=1;
}
}
}
elseif((ch>='0'&&ch<='9')||ch=='-')
{
intt=p-1;
if(a[t]>='0'&&a[t]<='9'||a[t]>='a'&&a[t]<='z'||a[t]>='A'&&a[t]<='Z')
{
p++;
ch=a[p];
if(ch=='-'||ch=='=')
{
b[0]='-';
b[1]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='-';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
else
{
q=0;
b[q++]=ch;
p++;
ch=a[p];
while((ch>='0'&&ch<='9')||ch=='.')
{
b[q++]=ch;
p++;
ch=a[p];
}
strcpy(word[k].name,b);
word[k++].kind=2;
}
}
elseif(ch=='('||ch==')'||ch=='['||ch==']'||ch=='{'||ch=='}'||ch==','||ch==';'||ch==':
'||ch=='\''||ch=='"')//ch=='('||ch==')'||ch=='['||ch==']'||ch=='{'||ch=='}'||
{
b[0]=ch;
strcpy(word[k].name,b);
word[k++].kind=4;
ch=a[++p];
}
elseif(ch=='%'||ch=='^')
{
b[0]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
elseif(ch=='+')
{
p++;
ch=a[p];
if(ch=='+'||ch=='=')
{
b[0]='+';
b[1]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='+';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='*')
{
p++;
ch=a[p];
if(ch=='*'||ch=='=')
{
b[0]='*';
b[1]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='*';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='/')
{
p++;
ch=a[p];
if(ch=='/'||ch=='=')
{
b[0]='/';
b[1]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='/';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='=')
{
p++;
ch=a[p];
if(ch=='=')
{
b[0]=b[1]='=';
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='=';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='>')
{
p++;
ch=a[p];
if(ch=='>'||ch=='=')
{
b[0]='>';
b[1]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='>';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='<')
{
p++;
ch=a[p];
if(ch=='<'||ch=='=')
{
b[0]='<';
b[1]=ch;
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='<';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='!
')
{
p++;
ch=a[p];
if(ch=='=')
{
b[0]='!
';
b[1]='=';
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='!
';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='&')
{
p++;
ch=a[p];
if(ch=='&')
{
b[0]=b[1]='&';
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='&';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
elseif(ch=='|')
{
p++;
ch=a[p];
if(ch=='|')
{
b[0]=b[1]='|';
strcpy(word[k].name,b);
word[k++].kind=5;
ch=a[++p];
}
else
{
b[0]='|';
strcpy(word[k].name,b);
word[k++].kind=5;
}
}
}
}
for(inti=0;i{
switch(word[i].kind)
{
case1:
{
printf("(标识符,");
break;
}
case2:
{
printf("(常量,");
break;
}
case3:
{
printf("(关键字,");
break;
}
case4:
{
printf("(界符,");
break;
}
case5:
{
printf("(运算符,");
break;
}
}
printf("%s)\n",word[i].name);
}
inta1=0,a2=0,a3=0,a4=0,a5=0;
for(inti=0;i{
if(word[i].kind==1)
a1++;
elseif(word[i].kind==2)
a2++;
elseif(word[i].kind==3)
a3++;
elseif(word[i].kind==4)
a4++;
elseif(word[i].kind==5)
a5++;
}
printf("标识符:
%d\n常量:
%d\n关键字:
%d\n界符:
%d\n运算符:
%d\n",a1,a2,a3,a4,a5);
return0;
}
运行结果: