?? 221105027_計(jì)續(xù)本05_李超_編譯詞法分析.c
字號(hào):
#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("請(qǐng)輸入源程序文件名(包括路徑):");
scanf("%s",Scanin);
if ((fin=fopen(Scanin,"r"))==NULL)
{
printf(“\n 打開詞法分析輸入文件出錯(cuò)!\n");
return(1);
}
if ((fout=fopen(Scanout,"w"))==NULL)
{
printf("\n 創(chuàng)建詞法分析輸出文件出錯(cuò)!\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(“詞法分析有錯(cuò),編譯停止!") ;
else printf (“詞法分析成功!\n") ;
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -