📄 scan.cpp
字号:
//#include<stdio>
//#include<stdlib>
#include<string>
//#include<malloc>
//#include<ctype>
using namespace std;
#define MAXBUF 32
char ch=' '; /*当前的字符,初始化为空*/
int lineno; /*行号*/
int rowno;/*列号*/
/*33个关键字*/
string keyword[33]={"and","array","begin","bool","call","case","char","constant","do","else",
"end","false","for","if","input","integer","not","of","or","output","procedure",
"program","read","real","repeat","set","then","to","true","until","var","while",
"write", };
int token;
int search(string *word) /*用来查关键字*/
{
int i;
for( i=0; i<33; i++)
{
if(word==(keyword[i])
{
return i;
}
}
return 0;
}
void scanner(FILE *fpin,FILE *fpout) /*扫描字符*/
{
char arr[MAXBUF];/*读出的最长的字符串不超过MAXBUF*/
int i=0;/*分析含字母的字符串用*/
int j=0;/*分析纯数字的字符串用*/
string *num;
ch=fgetc(fpin);
while(ch!=EOF)
{
//putchar(ch);
if( ch=' ') /*空格*/
rowno++;
else if(ch='\t') /*tab */
rowno+=12;
else if( ch='\n') /*回车换行符*/
lineno++;
if(isalpha(ch)) /*读入的是字母*/
{
arr[i] =ch;
i++;
ch=fgetc(fpin); /*读入下一个字符*/
rowno++;
while(isalpha(ch) || isdigit(ch))
{
arr[i] =ch;
i++;
ch=fgetc(fpin);
rowno++;
}
fseek(fpin,-1L,SEEK_CUR); /*文件指针后退一个字节*/
rowno--;
string *word = (char*)malloc(i+1) ;
memcpy(word,arr,i);
//word[i] =''\0'';
if(search(word))/*在关键字表中查找和word字符串相同的,找到就返回种别码*/
{
printf("%s\t\t&d\n",word,search(word));
fprintf(fpout,"%s\t%d \n",word,search(word));
}
else
{
fprintf(fpout,"%s\t%d\n",word,34);/*标示符号*/
}
i=0;
}
else if( isdigit(ch))/*读入的是数字*/
{
arr[i] =ch;
i++;
ch=fgetc(fpin);/*读入下一个字符*/
rowno++;
while(isdigit(ch))
{
arr[j] = ch;
j++;
fscanf(fpin,"%c",&ch);
}
fseek(fpin,-1L,SEEK_CUR);/*文件指针后退一个字节*/
num =(char*) malloc(j+1);
memcpy(num,arr,j); /*把数组里面的内容拷贝到链外一个数组里面*/
/*num[j] =''\0''; */
j=0;/*恢复初始状态*/
fprintf(fpout,"%s\t\t%d\n",num,35);/*常数*/
free(num);/*释放内存*/
}
else if(ch==':')
{
ch=fgetc(fpin);
if(ch=='=')
{
fprintf(fpout,"%s\t%d\n",":=",51); /*如果是 := */
}
else
{
fprintf(fpout,"%s\t%d\n",":",50); /* 如果是 : */
fseek(fpin,-1L,SEEK_CUR);/* 文件指针后退一个字节*/
}
}
else if (ch=='>')
{
ch=fgetc(fpin);
if(ch=='=') /*如果是 >= */
{
fprintf(fpout,"%s\t%d\n",">=",58);
}
else
{
fprintf(fpout,"%s\t%d\n",">",57); /*如果是 > */
fseek(fpin,-1L,SEEK_CUR);/*文件指针后退一个字节 */
}
}
else if(ch=='<')
{
ch=fgetc(fpin);
if(ch=='>')
{
fprintf(fpout,"%s\t%d\n","<>",55); /* 如果是 <> */
}
else if(ch=='=')
{
fprintf(fpout,"%s\t%d\n","<=",54); /* 如果是 <= */
}
else
{
fprintf(fpout,"%s\t%d\n","<",53); /* 如果是 < */
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","/",48);
fseek(fpin,-1L,SEEK_CUR);/*文件指针后退一个字节*/
}
}
else if(ch=='\''){fprintf(fpout,"%s\t%d\n","'",38);}
else if(ch=='+'){fprintf(fpout,"%s\t%d\n","+",43);}
else if(ch=='-'){fprintf(fpout,"%s\t%d\n","-",45);}
else if(ch=='*'){fprintf(fpout,"%s\t%d\n","*",41);}
else if(ch=='('){fprintf(fpout,"%s\t%d\n","(",39);}
else if(ch==')'){fprintf(fpout,"%s\t%d\n",")",40);}
else if(ch=='['){fprintf(fpout,"%s\t%d\n","[",59);}
else if(ch==']'){fprintf(fpout,"%s\t%d\n","]",60);}
else if(ch=='.'){fprintf(fpout,"%s\t%d\n",".",46);}
else if(ch==';'){fprintf(fpout,"%s\t%d\n",";",52);}
else if(ch=='='){fprintf(fpout,"%s\t%d\n","=",56);}
else if(ch==','){fprintf(fpout,"%s\t%d\n",",",44);}
else
fprintf(fpout,"无法识别的字符 %c\n",ch) ;
ch=fgetc(fpin);
}
printf("\nScan end.") ;
}
void main()
{
char filenamein[10];
char filenameout[10];
FILE* fpin;
FILE* fpout;
char sour;
char option;
printf("*********************** SCANNER ***********************\n\n\n");
a: printf("Please enter sourse file name:\n");
scanf("%s",filenamein);
fpin=fopen(filenamein,"r");
while (fpin==NULL)
{
printf("cannot open this file!!!\n\n\n");
goto a;
}
printf("The sourse programe is:\n\n");
sour=fgetc(fpin); /*将原程序显示到屏幕上*/
while (sour!=EOF)
{
putchar(sour);
sour=fgetc(fpin);
}
fseek(fpin,0L,0);/*文件指针回到开始位置,以备扫描*/
printf("\n\nReady to Scan!!!\n");
b: printf("Please enter a file name for save result:\n");
scanf("%s",filenameout);
fpout=fopen(filenameout,"w");
if (fpout==NULL)
{
printf("cannot open this file!!!\n\n\n");
goto b;
}
scanner(fpin,fpout); /*开始扫描*/
fclose(fpin);
fclose(fpout);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -