📄 scnner.cpp
字号:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<stdlib.h>
typedef struct KeyWord
{
int num;
char *word;
}KeyWordTable;
KeyWordTable kewwordtable[12];// 保存保留字表的全局数组
char TOKEN[20];
FILE *ifp;
FILE *ofp;
int begin_note = 0;//注释开始
long position;
void init_KeyWordTable();
bool isequal(char *s1,char *s2);
int lookup(char *token );
void out (int m,char *token);
void report_error();
void scnner(FILE *fp);
int main(int argc, char* argv[])
{
if(argc!=3)
{
printf("You forgot to enter a filename\n");
exit(0);
}
if((ifp=fopen(argv[1],"r"))==NULL)
{
printf("Open file: %s error!\n",argv[1]);
exit(0);
}
if((ofp=fopen(argv[2],"a"))==NULL)
{
printf("Open file: %s error!\n",argv[2]);
exit(0);
}
init_KeyWordTable();//初始化保留字表
char a;
long position;
a = fgetc(ifp);
if(a!=EOF)
{
while (isspace (a))//检查是否是空格 制表符或换行符
{
a = fgetc(ifp);
}
}
fseek(ifp,-1,1);
while (!feof(ifp))
{
position = ftell(ifp);
printf("%ld\n",position);
scnner(ifp);
position = ftell(ifp);
printf("%ld\n",position);
a = fgetc(ifp);
if(a!=EOF)
{
while (isspace (a))//检查是否是空格 制表符或换行符
{
a = fgetc(ifp);
}
if(a!=EOF)
{
fseek(ifp,-1,1);
}
}
}
fclose(ifp);
fclose(ofp);
return 0;
}
void init_KeyWordTable()
{
kewwordtable[0].num = 1;
kewwordtable[0].word = "begin";
kewwordtable[1].num = 2;
kewwordtable[1].word = "end";
kewwordtable[2].num = 3;
kewwordtable[2].word = "if";
kewwordtable[3].num = 4;
kewwordtable[3].word = "then";
kewwordtable[4].num = 5;
kewwordtable[4].word = "else";
kewwordtable[5].num = 6;
kewwordtable[5].word = "for";
kewwordtable[6].num = 7;
kewwordtable[6].word = "do";
kewwordtable[7].num = 8;
kewwordtable[7].word = "while";
kewwordtable[8].num = 9;
kewwordtable[8].word = "and";
kewwordtable[9].num = 10;
kewwordtable[9].word = "for";
kewwordtable[10].num = 11;
kewwordtable[10].word = "not";
kewwordtable[11].num = 12;
kewwordtable[11].word = "or";
}
bool isequal(char *s1,char *s2)
{
int i=0;
for(;*(s1+i)!='\0'&&*(s2+i)!='\0';)
{
if(*(s1+i)==*(s2+i)||*(s1+i)==*(s2+i)+32||s1[i]==s2[i]-32)//大小写不敏感
{
i++;
}
else
{
return 0;
break;
}
}
if(s1[i]=='\0'&&s2[i]=='\0')
return 1;
else
return 0;
}
int lookup(char *token )
{
for(int i=0;i<12;i++)
if(isequal(token,kewwordtable[i].word))
{
return kewwordtable[i].num;
break;
}
return 0;
}
void out (int m,char *token)
{
fprintf(ofp,"%c%d,%s%c",'(',m,token,')');
}
void report_error()
{
printf("Error:undeclared identifier!\n");
}
void scnner(FILE *fp)
{
char ch;
int i;
int c;
ch = fgetc(fp);
if(ch=='/')
{
ch = fgetc(fp);
if(ch=='*' )
{
if(begin_note % 2 == 1)
{
printf("Error!嵌套的\" /* \"!\n");
}
begin_note ++;
return;
}
else
{
fseek(fp,-1,1);
out (22," ");
return;
}
}
if(ch=='*')
{
ch = fgetc(fp);
if(ch=='/' )
{
if(begin_note % 2 == 0)
{
printf("Error!未匹配的\" */ \"!\n");
return;
}
begin_note++;
return;
}
else
{
fseek(fp,-1,1);
out (27," ");
return;
}
}
if(begin_note % 2==1)
{
return;
}
if(isalpha (ch))
{
TOKEN[0] = ch;
ch = fgetc(fp);
i = 1;
while (isalnum(ch))
{
TOKEN[i] = ch;
i++;
ch = fgetc(fp);
}
TOKEN[i] = '\0';
if (ch!=EOF) //////
{
fseek(fp,-1,1);
}
c = lookup(TOKEN);
if (0==c)
out (13,TOKEN);
else out (c," ");
}
else
if(isdigit(ch))
{
TOKEN[0] = ch;
ch = fgetc(fp);
i = 1;
while(isdigit(ch))
{
TOKEN[i] = ch;
i++;
ch = fgetc(fp);
}
TOKEN[i] = '\0';
fseek(fp,-1,1);
out(14,TOKEN);
}
else
switch(ch)
{
case'<':
ch = fgetc(fp);
if(ch=='=')
out(16," ");
else if(ch=='>')
out(19," ");
else
{
fseek(fp,-1,1);
out (15," ");
}
break;
case'=':
out(17," ");
break;
case'>':
ch = fgetc(fp);
if(ch=='=')
out(20," ");
else
{
fseek(fp,-1,1);
out (18," ");
}
break;
case':':
ch = fgetc(fp);
if(ch=='=')
out(23," ");
else
{
fseek(fp,-1,1);
out (21," ");
}
break;
case'+':
out(25," ");
break;
case'-':
out(26," ");
break;
case'|':
out(28," ");
break;
/* case'/':
ch = fgetc(fp);
if(ch=='*')
out(24," ");
else
{
fseek(fp,-1,1);
out (22," ");
}
break;
case EOF:
break;
case ' ':
break;
case '\t'://制表符
break;
case '\n': //换行
break;
case '\r'://回车
break; */
default:
report_error();
break;
}
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -