这是我们在编译原理课程上所做的实验,我将它记录下来希望能帮助到其它同学。
要求:统计一下input.txt文档中的代码进行词法分析
void main()
{
int x0e,AB,C;
x0e=(AB+C*C)/8;
if(){}
else{}
for(x=0;x++;x<10)
{i++;
}
do{} while(i<10);
}$
符号编码表:
输出内容:
代码如下:
#include <stdio.h>
#include <ctype.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
FILE *fp;
char cbuffer;
char *key[8]={"void","int","float","char","if","else","while","do"};//关键字
char *border[9]={"(",")","[","]","{","}",":",";",","}; //标点符
char *arithmetic[4]={"+","-","*","/"}; //运算符
char *relation[10]={"<","<=",">",">=","==","!=","&","&&","||","="}; //关系符
char *consts[20]; char *label[20];
int constnum=0,labelnum=0;
int j =0;
int search(char searchchar[],int wordtype)
{
int i=0;
switch(wordtype)
{
case 1://判断是否为关键字
for(i=0;i<=7;i++)
{
if (strcmp(key[i],searchchar)==0)
return(i+26);
}
return 0;
case 2: //判断是否为标点符
{ for (i=0;i<=8;i++)
{
if (strcmp(border[i],searchchar)==0)
return(i+17);
}
return(0);
}
case 3://判断是否为运算符
{
for (i=0;i<=3;i++)
{
if (strcmp(arithmetic[i],searchchar)==0)
{
return(i+3);
}
}
return(0);
}
case 4://判断是否为关系符
{
for (i=0;i<=9;i++)
if (strcmp(relation[i],searchchar)==0)
return(i+7);
return(0);
}
case 5:
{
for (i=0;i<=constnum;i++)
{
if(consts[i] && (strcmp(consts[i],searchchar)==0))
return(i+1);
}
consts[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(consts[i-1],searchchar);
constnum++;
return(i);
}
case 6:
{
for(i=0;i<=labelnum;i++)
if(label[i] && (strcmp(label[i],searchchar)==0))
return(i+1);
label[i-1]=(char *)malloc(sizeof(searchchar));
strcpy(label[i-1],searchchar);
labelnum++;
return(i);
}
default:
return 0;
}
}
char alphaprocess(char buffer)
{
int i=-1;
char alphatp[20];
while((isalpha(buffer))||(isdigit(buffer)))
{
alphatp[++i]=buffer;
buffer=fgetc(fp);
}
alphatp[i+1]='\0';
if (search(alphatp,1)) {
j = search(alphatp,1);
printf("关键字:(%d, \"%s\")\n",j,alphatp);
}
else
{
search(alphatp,6);
printf("标识符:(1, \"%s\")\n",alphatp);
}
return(buffer);
}
char digitprocess(char buffer)
{
int i=-1;
char digittp[20];
while ((isdigit(buffer)))
{
digittp[++i]=buffer;
buffer=fgetc(fp);
}
digittp[i+1]='\0';
search(digittp,5);
printf("整数:(2, \"%s\")\n",digittp); return(buffer);
}
char otherprocess(char buffer)
{
int i=-1;
char othertp[20];
othertp[0]=buffer;
othertp[1]='\0';
if (search(othertp,3))
{
j= search(othertp,3);
printf("运算符:(%d, \"%s\")\n",j,othertp);
buffer=fgetc(fp);
goto out;
}
if (search(othertp,4))
{
buffer=fgetc(fp);
othertp[1]=buffer;
othertp[2]='\0';
if (search(othertp,4))
{
j= search(othertp,4);
printf("关系符:(%d, \"%s\")\n",j,othertp);
goto out;
}
else
othertp[1]='\0';
j= search(othertp,4);
printf("关系符:(%d, \"%s\")\n",j,othertp);
goto out;
}
if (buffer==':')
{
buffer=fgetc(fp);
if (buffer=='=')
printf(":= (2,2)\n");
buffer=fgetc(fp);
goto out;
}
else
{
if (search(othertp,2))
{
j= search(othertp,2);
printf("标点符:(%d, \"%s\")\n",j,othertp);
buffer=fgetc(fp);
goto out;
}
}
if ((buffer!='\n')&&(buffer!=' '))
printf("未定义:(0,\"%c\")\n",buffer);
buffer=fgetc(fp);
out:
return(buffer);
}
void main()
{
int i;
for (i=0;i<=20;i++)
{
label[i]=NULL;
consts[i]=NULL;
}
if ((fp=fopen("input.txt","r"))==NULL)
printf("error");
else
{
cbuffer = fgetc(fp);
while (cbuffer!=EOF)
{
if (isalpha(cbuffer))
cbuffer=alphaprocess(cbuffer);
else
if(isdigit(cbuffer))
cbuffer=digitprocess(cbuffer);
else
cbuffer=otherprocess(cbuffer);
}
printf("over\n");
getchar();
}
}