编译原理词法分析程序

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#include
#define M 21 //分析表的最大容量
#define G_M 10 //关键字的最大长度
#define H_M 20 //缓冲区的大小
char ch =' '; //存放读入当前的输入字符
int Line_Num=1; //纪录行号
struct reserve //关键字
{
char A[G_M];
int number;
};
struct reserve str1[M]; //str1[M]属于reserve类型
char str2[M][10]={"void","main","include","printf", "scanf","int","float","char","double",
"for","if","else","then","do","while","end","break","switch","const","case","continue"}; //列举出的部分关键字,共21个
void Csh() //对关键字表进行初始化
{ //最小的number是void:3,最大的number是continue:23
int j;
for(j=0; j{
strcpy(str1[j].A,str2[j]); //strcpy为复制函数
str1[j].number=j+3;
}
}
int seek_reserve(char *res) //对关键字进行搜索 seek_reserve()的返回值为关键字的种别码
{
int i;
for(i=0;i{
if((strcmp(str1[i].A,res))==0)
return str1[i].number; //返回数字的种别码
}
return 0;
}
int Zm(char c) //判断是否为字母
{
if(((c<='z')&&(c>='a'))||((c<='Z')&&(c>='A')))
return 1; //如果是字母时,返回1
else
return 0; //如果不是字母时,返回0
}
int Sz(char c) //判断是否为数字
{
if(c>='0'&&c<='9')
return 1; //如果是数字时,返回1
else
return 0; //如果不是数字时,返回0
}
void Fxcx(FILE *fpin,FILE *fpout) //fpin和fpout为文件指针变量 分析程序
{
char arr[H_M]; //输入缓冲区,存放一个单词符号
int j=0;
ch=fgetc(fpin);
while(ch!=EOF)//当EOF的值为-1时,表示输入的已不是正常的字符而是文件结束符
{
if(ch==' '||ch=='\t')
ch=fgetc(fpin); //跳过空格、tab键
else if(ch=='\n')
{
Line_Num++; //当出现换行符时,Line_Num++表示行数自加1.
ch=fgetc(fpin);
}
else if(Zm(ch)) //对字母进行处理
{
while(Zm(ch))
{
if((ch<='Z')&&(ch>='A')) //忽略字母的大小写
ch=ch+32;
arr[j++]=ch;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR); //当退出循环时,指针回退一个字符
arr[j]='\0';
j=0;
if(seek_reserve(arr)) //对关键字进行处理
{
fprintf(fpout,"%s\t\t%d\n",arr,seek_reserve(arr)); //seek_reserve(arr)函数代表返回数字的种别码
ch=fgetc(fpin);
}
else
{
fprintf(fpout,"%s\t\t%d\n",arr,1); //当不是关键字时,认定为普通标识符,种别码为1.
ch=fgetc(fpin);
}
}
else if(Sz(ch)) //对数字进行处理
{
while(Sz(ch))
{
arr[j++]=ch;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR); //当退出循环时,将位移指针回退一个字符
arr[j]='\0';
j=0;
fprintf(fpout,"%s\t\t%d\n",arr,2); //输出无符号整数,种别码为2.
ch=fgetc(fpin);
}
else switch(ch)
{
case'+' :
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t\t%d\n","+=",28);
ch=fgetc(fpin);
}
else if(ch=='+')
{
f

printf(fpout,"%s\t\t%d\n","++",29);
ch=fgetc(fpin);
}
else
{
fprintf(fpout,"%s\t\t%d\n","+",30);
fseek(fpin,-1L,SEEK_CUR);
ch=fgetc(fpin);
}
}
break;
case'-' :
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t\t%d\n","-=",31);
ch=fgetc(fpin);
}
else if(ch=='-')
{
fprintf(fpout,"%s\t\t%d\n","--",32);
ch=fgetc(fpin);
}
else
{
fprintf(fpout,"%s\t\t%d\n","-",33);
fseek(fpin,-1L,SEEK_CUR);
ch=fgetc(fpin);
}
}
break;
case'(' :fprintf(fpout,"%s\t\t%d\n","(",24);ch=fgetc(fpin);break;
case')' :fprintf(fpout,"%s\t\t%d\n",")",25);ch=fgetc(fpin);break;
case'[' :fprintf(fpout,"%s\t\t%d\n","[",26);ch=fgetc(fpin);break;
case']' :fprintf(fpout,"%s\t\t%d\n","]",27);ch=fgetc(fpin);break;
case';' :fprintf(fpout,"%s\t\t%d\n",";",38);ch=fgetc(fpin);break;
case'=' :fprintf(fpout,"%s\t\t%d\n","=",39);ch=fgetc(fpin);break;
case'.' :fprintf(fpout,"%s\t\t%d\n",".",40);ch=fgetc(fpin);break;
case',' :fprintf(fpout,"%s\t\t%d\n",",",41);ch=fgetc(fpin);break;
case':' :fprintf(fpout,"%s\t\t%d\n",":",42);ch=fgetc(fpin);break;
case'#' :fprintf(fpout,"%s\t\t%d\n","#",43);ch=fgetc(fpin);break;
case'*' :fprintf(fpout,"%s\t\t%d\n","*",44);ch=fgetc(fpin);break;
case'{' :fprintf(fpout,"%s\t\t%d\n","{",45);ch=fgetc(fpin);break;
case'}' :fprintf(fpout,"%s\t\t%d\n","}",46);ch=fgetc(fpin);break;
case'"' :fprintf(fpout,"%s\t\t%d\n","\"",47);ch=fgetc(fpin);break;
case'%' :fprintf(fpout,"%s\t\t%d\n","%",48);ch=fgetc(fpin);break;
case'>' :
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t\t%d\n",">=",34);
ch=fgetc(fpin);
}
else
{
fprintf(fpout,"%s\t\t%d\n",">",35);
fseek(fpin,-1L,SEEK_CUR);
ch=fgetc(fpin);
}
}
break;
case'<' :
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t\t%d\n","<=",36);
ch=fgetc(fpin);
}
else
{
fprintf(fpout,"%s\t\t%d\n","<",37);
fseek(fpin,-1L,SEEK_CUR);
ch=fgetc(fpin);
}
}
break;
case'/' :
{
ch=fgetc(fpin); //跳过注释部分
if(ch='/')
{
while(ch!='\n')
ch=fgetc(fpin);
}
}
break;
default :
{
fprintf(fpout,"在第%d行无法识别的字符\t%c\n",Line_Num,ch);
ch=fgetc(fpin);
}
}
}
}
void main() //主程序中完成对输入输出文件的读写
{
char in[10],out[10];
FILE *fpin,*fpout;
printf("input the input file example:\n");
scanf("%s",in);
printf("input the output file example:\n");
scanf("%s",out);
fpin=fopen(in,"r");
fpout=fopen(out,"w");
Csh(); //初始化关键字表
Fxcx(fpin,fpout);
fclose(fpin);
fclose(fpout);
}

相关文档
最新文档