📄 221105027_计续本05_李超_编译词法分析.c
字号:
#include<stdio.h>
#include<ctype.h>
#define keywordSum 8
char * keyword[keywordSum]={"if","else","for","while","do","int","read","write"};
char singleword[50]="-*/(){};,:!";
char doubleword[10]="><=!&|";
extern char Scanin[300],Scanout[300];
Extern FILE *fin,*fout;
int TESTscan()
{
char ch, token[40];
int es=0,j,n;
printf("请输入源程序文件名(包括路径):");
scanf("%s",Scanin);
if ((fin=fopen(Scanin,"r"))==NULL)
{
printf(“\n 打开词法分析输入文件出错!\n");
return(1);
}
if ((fout=fopen(Scanout,"w"))==NULL)
{
printf("\n 创建词法分析输出文件出错!\n");
return(2);
}
ch=getc(fin);
while (ch!=EOF)
{
while (ch==''||ch=='\n'||ch=='\t') ch=getc(fin);
if (isalpha(ch))
{
token[0]=ch;j=1;
ch=getc(fin);
while (isalnum(ch))
{
token [j++]=ch;
ch=getc(fin);
}
token[j]='\0';
//查保留字
n=0;
while ((n<keywordSum) && strcmp(token,keyword[n])) n++;
if (n>=keywordSum)
fprintf(fout,"%s\t%s\n","ID",token);
else
fprintf(fout,"%s\t%s\n",token,token) ;
}
else if (isdigit(ch))
{
token[0]=ch;j=1;
ch=getc(fin);
while(isdigit(ch))
{
token[j++]=ch;
ch=getc(fin);
}
token[j]='\0'
fprintf(fout,"%s\t%s\n","NUM",token) ;
}
else if (strchr(doubleword,ch)>0)
{
token[0]=ch;token[1]='\0';
ch=getc(fin);
}else
token[1]='\0';
fprintf(fout,"%s\t%s\n",token,token);
}
else if (ch=='/')
{
ch=getc(fin);
if (ch=='*')
{
char ch1;
ch1=getc(fin);
do
{
ch=ch1;ch1=getc(fin);
}
while ((ch!='*'||ch1!='/')&&ch1!=EOF);
ch=getc(fin);
}
}else
{
token[0]='/' ;token[1]='\0';
fprintf(fout,"%s\t%s\n",token,token);
}
}else
{
token[0]=ch;token[1]='\0';
ch=getc(fin);
es=3;
printf(fout,"%s\t%s\n", "ERROR",token);
}
}
fclose(fin);
fclose(fout);
return(es);
#include<stdio.h>
#include<ctype.h>
extern int TEST scan( );
char Scanin[300],Scanout[300] ;
FILE *fin,*fout;
void main( ){
int es=0;
es=TESTscan( ) ;
if (es>0) printf(“词法分析有错,编译停止!") ;
else printf (“词法分析成功!\n") ;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -