📄 cifafenxiqi.cpp
字号:
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
#define MAX 18
#define MAXBUF 255
char ch =' '; //读下一个字符
int linenum; //行数计数
/*关键字存储结构*/
struct reserve
{
char lexptr[MAXBUF];
int token; //关键字
};
struct reserve symtable[MAX];//行数
char * str[]={"begin","void","main","program","int","char","real","for",
"to","if","then","else","do","while","var","end","input","output"};
// 以上关键字符号依次的种别号从3~20
/*关键字的定义*/
void init()
{
for( int j=0; j<18; j++)
{
strcpy(symtable[j].lexptr,str[j]);
symtable[j].token=j+3; //关键字种别码从3开始
}
}
/*查找符合的关键字*/
int search(char *temp)
{
for(unsigned int i=0;i < 18;i++)
{
if(!strcmp(symtable[i].lexptr ,temp))
return symtable[i].token;
}
return 0;
}
/*读取源文件后,做词法分析,最后将结果输出在指定文件中*/
void analyse(FILE *fpin,FILE *fpout)
{
char arr[MAXBUF]; //词法分析缓冲区
int i=0;
int j=0;
while((ch=fgetc(fpin))!=EOF)
{
if(ch==' '||ch=='\t')
{
}
else if(ch=='\n')
{
linenum++;
}
else if(isdigit(ch)) //判断字符是否是数字字符
{
while(isdigit(ch))
{
arr[j]=ch;
j++;
ch=fgetc(fpin);
}
arr[j]='\0';
j=0;
fseek(fpin,-1L,SEEK_CUR);
fprintf(fpout,"%s\t%d\n",arr,2) ; //是,就将数字字符及种别码2输出
}
else if (isalpha(ch))
{
while(isalpha(ch)||isdigit(ch)) //判断字符是否是字母字符
{
arr[j]=ch;
j++;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR);
arr[j]='\0';
j=0;
if (search(arr))
{
fprintf(fpout,"%s\t%d\n",arr,search(arr));
}
else
fprintf(fpout,"%s\t%d\n",arr,1); //是,就将字母字符及种别码1输出
}
else if(ch==':') //判断字符是否是运算符和界符
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t%d\n",":=",29);
}
else
{
fprintf(fpout,"%s\t%d\n",":",30);
fseek(fpin,-1L,SEEK_CUR);
}
}
else if (ch=='>')
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t%d\n",">=",32);
}
else
{
fprintf(fpout,"%s\t%d\n",">",31);
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='<')
{
ch=fgetc(fpin);
if(ch=='>')
{
fprintf(fpout,"%s\t%d\n","<>",35);
}
else if(ch=='=')
{
fprintf(fpout,"%s\t%d\n","<=",34);
}
else
{
fprintf(fpout,"%s\t%d\n","<",33);
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='/')
{
ch=fgetc(fpin);
if(ch=='*')
{
ch=fgetc(fpin);
s:
while(ch!='*')
{
ch=fgetc(fpin);
}
while(ch=='*')
{
ch=fgetc(fpin);
while(ch!='/')
{
goto s;
}
}
}
else if(ch=='/')
{
ch=fgetc(fpin);
while(ch!='\n')
{
ch=fgetc(fpin);
}
}
else
{
fprintf(fpout,"%s\t%d\n","/",24);
fseek(fpin,-1L,SEEK_CUR);
}
}
else if(ch=='+')
{
fprintf(fpout,"%s\t%d\n","+",21);
}
else if(ch=='-')
{
fprintf(fpout,"%s\t%d\n","-",22);
}
else if(ch=='*')
{
fprintf(fpout,"%s\t%d\n","*",23);
}
else if(ch=='(')
{
fprintf(fpout,"%s\t%d\n","(",25);
}
else if(ch==')')
{
fprintf(fpout,"%s\t%d\n",")",26);
}
else if(ch=='[')
{
fprintf(fpout,"%s\t%d\n","[",27);
}
else if(ch==']')
{
fprintf(fpout,"%s\t%d\n","]",28);
}
else if(ch=='.')
{
fprintf(fpout,"%s\t%d\n",".",39);
}
else if(ch==';')
{
fprintf(fpout,"%s\t%d\n",";",36);
}
else if(ch=='=')
{
fprintf(fpout,"%s\t%d\n","=",38);
}
else if(ch==',')
{
fprintf(fpout,"%s\t%d\n",",",40);
}
else if(ch=='"')
{
fprintf(fpout,"%s\t%d\n","\"",41);
}
else if(ch=='{')
{
fprintf(fpout,"%s\t%d\n","{",42);
}
else if(ch=='}')
{
fprintf(fpout,"%s\t%d\n","}",43);
}
else fprintf(fpout,"无法识别的字符 %c\n",ch) ;
}
}
void main()
{
printf("***************************词法分析器*************************\n\n\n");
char filenamein[10];
char filenameout[10];
printf("请输入源文件名:\n");
scanf("%s",filenamein);
printf("请输入保存词法分析结果的文件名(请注明文件格式):\n");
scanf("%s",filenameout);
FILE* fpin=fopen(filenamein,"r"); //将源文件以只读的方式打开
FILE* fpout=fopen(filenameout,"w");
init();
analyse(fpin,fpout);
fclose(fpin); //关闭文件
fclose(fpout);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -