标签:
实验一、词法分析器实验
专业 商业软件工程三班 姓名 梁荣臻 学号201506110206
一、 实验目的
通过词法分析器,进一步了解词法之间的关系。
二、 实验内容和要求
将输入的字符串通过词法分析器一一识别出来,同时将该字符串或者字符并且和种别码一起输出来。
三、 实验方法、步骤及结果测试
可执行程序名:byyl.exe
采用数组存放数据,通过字符串的比较实现关键字的种别码输出
#include<stdio.h>
#define MAX 50
main()
{
char shuru[MAX];
do
{
printf("请输入想要识别的字符:(输入tuichu代表退出)\n");
scanf("%s",&shuru);
if(strcmp(shuru,"begin") == 0)
printf("符号为:%s,种别码为:1\n",shuru);
else if(strcmp(shuru,"if") == 0)
printf("符号为:%s,种别码为:2\n",shuru);
else if(strcmp(shuru,"then") == 0)
printf("符号为:%s,种别码为:3\n",shuru);
else if(strcmp(shuru,"while") == 0)
printf("符号为:%s,种别码为:4\n",shuru);
else if(strcmp(shuru,"do") == 0)
printf("符号为:%s,种别码为:5\n",shuru);
else if(strcmp(shuru,"end") == 0)
printf("符号为:%s,种别码为:6\n",shuru);
else if(strcmp(shuru,"l(l|d)*") == 0)
printf("符号为:%s,种别码为:10\n",shuru);
else if(strcmp(shuru,"dd*") == 0)
printf("符号为:%s,种别码为:11\n",shuru);
else if(strcmp(shuru,"+") == 0)
printf("符号为:%s,种别码为:13\n",shuru);
else if(strcmp(shuru,"-") == 0)
printf("符号为:%s,种别码为:14\n",shuru);
else if(strcmp(shuru,"*") == 0)
printf("符号为:%s,种别码为:15\n",shuru);
else if(strcmp(shuru,"/") == 0)
printf("符号为:%s,种别码为:16\n",shuru);
else if(strcmp(shuru,":") == 0)
printf("符号为:%s,种别码为:17\n",shuru);
else if(strcmp(shuru,":=") == 0)
printf("符号为:%s,种别码为:18\n",shuru);
else if(strcmp(shuru,"<") == 0)
printf("符号为:%s,种别码为:20\n",shuru);
else if(strcmp(shuru,"<=") == 0)
printf("符号为:%s,种别码为:21\n",shuru);
else if(strcmp(shuru,"<>") == 0)
printf("符号为:%s,种别码为:22\n",shuru);
else if(strcmp(shuru,">") == 0)
printf("符号为:%s,种别码为:23\n",shuru);
else if(strcmp(shuru,">=") == 0)
printf("符号为:%s,种别码为:24\n",shuru);
else if(strcmp(shuru,"=") == 0)
printf("符号为:%s,种别码为:25\n",shuru);
else if(strcmp(shuru,";") == 0)
printf("符号为:%s,种别码为:26\n",shuru);
else if(strcmp(shuru,"(") == 0)
printf("符号为:%s,种别码为:27\n",shuru);
else if(strcmp(shuru,")") == 0)
printf("符号为:%s,种别码为:28\n",shuru);
else if(strcmp(shuru,"#") == 0)
printf("符号为:%s,种别码为:0\n",shuru);
else if(strcmp(shuru,"tuichu") == 0)
{
printf("退出成功!\n");
break;
}
else
printf("无法识别此字符串!\n");
}while(-1);
}
四、 实验总结
在这次的实验中了解词法分析的过程,解决一些问题。了解到了如何字符串与字符串进行比较,完成自己的代码再与其他同学一对比才发现自己原来理解错了任务,并不只是识别一个关键词,而是一串代码,之后会努力改正的了。
标签:
原文地址:http://www.cnblogs.com/Lorz/p/5956051.html