首页 > 代码库 > 词法分析实验报告
词法分析实验报告
|
实验一、××实验
专业 商业软件工程 姓名 万峰 学号 201506110215
一、 实验目的
通过设计、开发一个词法分析程序,加深对课堂教学内容的理解,提高此法分析方法的实践能力。
二、 实验内容和要求
选取无符号的算术各类单词为识别对象,要求将其中的各个单词识别出来。输入:有无符号数和+,-,*,/,构成的算术表达式。
输出:对识别出的没一单词均单行输出其类别码。
为简单起见,假设编译语言具有以下特征,该词法分析器至少能识别以下几类种别码:
三、 实验方法、步骤及结果测试
- 1. 源程序名:
#include<stdio.h>
#define MATHN 100
main()
{
char N[MATHN],M[MATHN],W[MATHN]; //存放字符串的数组
int i,a=0,b=0,j;
printf("请您输入字符串:");
gets(N);
for(i=0;i<N;i++)
{
j=(int) N[i];
M[i]=N[MATHN];
if((j>=65) && (j<=90)||(j>=90) && (j<=122))
{
M[a]=N[i];
a++;
}
else{
while(a!=0)
{
M[a]=‘\0‘;
if(strcmp(M,"auto")==0||strcmp(M,"int")==0||strcmp(M,"double")==0
||strcmp(M,"long")==0||strcmp(M,"char")==0||strcmp(M,"float")==0
||strcmp(M,"short")==0||strcmp(M,"signed")==0||strcmp(M,"unsigned")==0
||strcmp(M,"struct")==0||strcmp(M,"union")==0||strcmp(M,"enum")==0
||strcmp(M,"static")==0||strcmp(M,"switch")==0||strcmp(M,"case")==0||strcmp(M,"default")==0
||strcmp(M,"break")==0||strcmp(M,"register")==0||strcmp(M,"const")==0
||strcmp(M,"volatile")==0||strcmp(M,"typedef")==0||strcmp(M,"extern")==0
||strcmp(M,"return")==0||strcmp(M,"void")==0||strcmp(M,"continue")==0
||strcmp(M,"do")==0||strcmp(M,"while ")==0||strcmp(M,"if")==0||strcmp(M,"else")==0
||strcmp(M,"for")==0||strcmp(M,"goto")==0||strcmp(M,"sizeof")==0)
{
M[a]=‘]‘;
printf("[ 1-");
puts(M);
a=0;
memset(M,0,MATHN);
}
else
{
M[a]=‘ ]‘;
printf("[ 10- ");
puts(M);
a=0;
memset(M,0,MATHN);
}
}
if((j>48)&& (j<57))
{
W[b]=N[i];
b++;
}
else{
if((int) N[i-1]>48 && (int) N[i-1]<57)
{
W[b]=‘ ]‘;
printf("[ 11- ");
puts(W);
b=0;
memset(W,0,MATHN);
}
if(j==42||j==43||j==45||j==47||j==61||j==62||j==60)
{
if((int) N[i+1]==61)
{
switch (j)
{
case 60:
printf("[ 21-%c= ]",N[i]);
printf("\n");
break ;
case 62:
printf("[ 24-%c= ]",N[i]);
printf("\n");
break;
default :
printf("[ 非法字符-%c ]",N[i]);
printf("\n");
break;
}
i=i+1;
}
else
{
switch (j)
{
case 42:
printf("[ 15-%c ]",N[i]);
printf("\n");
break ;
case 43:
printf("[ 13-%c ]",N[i]);
printf("\n");
break;
case 45:
printf("[ 14-%c ]",N[i]);
printf("\n");
break;
case 60:
printf("[ 20-%c ]",N[i]);
printf("\n");
break;
case 61:
printf("[ 25-%c ]",N[i]);
printf("\n");
break;
case 62:
printf("[ 24-%c ]",N[i]);
printf("\n");
break;
default :
printf("[ 非法字符-%c ]",N[i]);
printf("\n");
break;
}
}
}
else if(j==32)
{
}
else if(j==33||j==44||j==46||j==58||j==59||j==63||j==44)
{
switch (j)
{
case 59:
printf("[ 26-%c ]",N[i]);
printf("\n");
break ;
default :
printf("[ 非法字符-%c ]",N[i]);
printf("\n");
break;
}
}
else if(j==34||j==35||j==38||j==39||j==40||j==41||j==123||j==124||j==125||j==126)
{
switch (j)
{
case 40:
printf("[ 27-%c ]",N[i]);
printf("\n");
break ;
case 41:
printf("[ 28-%c ]",N[i]);
printf("\n");
break ;
case 35:
printf("[ 0-%c ]",N[i]);
printf("\n");
break ;
default :
printf("[ 非法字符-%c ]",N[i]);
printf("\n");
break;
}
}
else
{
break;
}
}
}
}
}
- 2. 主要程序段及其解释:
if(j==42||j==43||j==45||j==47||j==61||j==62||j==60) //+ - * / 的运算ASCII的值
{
if((int) N[i+1]==61)
{ switch (j) {
case 60:
printf("[ 21-%c= ]",N[i]); //
printf("\n");
break ;
case 62:
printf("[ 24-%c= ]",N[i]);
printf("\n");
break;
default :
printf("[ 非法字符-%c ]",N[i]);
printf("\n");
break;
}
i=i+1;
}
else
{
switch (j)
{
case 42:
printf("[ 15-%c ]",N[i]);
printf("\n");
break ;
case 43:
printf("[ 13-%c ]",N[i]);
printf("\n");
break;
case 45:
printf("[ 14-%c ]",N[i]);
printf("\n");
break;
case 60:
printf("[ 20-%c ]",N[i]);
printf("\n");
break;
case 61:
printf("[ 25-%c ]",N[i]);
printf("\n");
break;
case 62:
printf("[ 24-%c ]",N[i]);
printf("\n");
break;
default :
printf("[ 非法字符-%c ]",N[i]);
printf("\n");
break;
} } }
- 3. 运行结果及分析
四、 实验总结
通过这次词法分析的作业,我学会了一些词法原理。在自己的努力和同学的帮助下,我成功地将这代码写出来了,虽然这代码不算完美,但却是我认真去完成的。
词法分析实验报告