?? token.cpp
字號:
#include<iostream>
using namespace std;
#define NAMEMAX 20
#define LENGTH 30
#define REALNUM 27
#define NUM 20
/*************************************************/
typedef struct{
int type;
int index;
char name[NAMEMAX];
}list;
typedef struct{
char name[NAMEMAX];
int type;
}key_word;
/*************************************************/
key_word key[LENGTH];
int key_index=0,token_index=0,line_index=0,list_index=0;
FILE *fp_read, *fp_token, *fp_list;
list l;
char ch;
list List[NUM];
/*************************************************/
void init();
void clean(char *c);
void scan();
void ID_check();
void Notice_check();
void Num_check();
void others();
void Print();
int match(char ch);
int check();
int find();
/*************************************************/
void main()
{
init();
scan();
}
void init()
{
FILE *fp;
char tem[NAMEMAX];
int i=0;
fp=fopen("key.txt","r");
if(fp==NULL)
cout<<"對不起,要讀的文件不能打開"<<endl;
else
{
for(int j=0;j<REALNUM;j++)
{
clean(tem);
ch=fgetc(fp);
while(((ch!=' ')&&(ch!=10)))
{
tem[i]=ch;
i++;
ch=fgetc(fp);
}
i=0;
strcpy(key[key_index].name,tem);
fscanf(fp,"%d",&key[key_index].type);
key_index++;
ch=fgetc(fp);
}
}
fclose(fp);
}
void scan()
{
char file_name[NAMEMAX];
cout<<"請輸入你要編譯的文件:"<<endl;
cin>>file_name;
fp_read=fopen(file_name,"r");
fp_token=fopen("token.txt","w");
fp_list=fopen("list.txt","w");
if(fp_read==NULL||fp_token==NULL||fp_list==NULL)
{
cout<<"對不起,文件不能打開"<<endl;
return;
}
ch=fgetc(fp_read);
while(ch!=EOF)
{
clean(l.name);
if(((ch>64)&&(ch<90))||((ch>96)&&(ch<123)))
ID_check();
else
{
if(ch=='/')
Notice_check();
else
{
if(((ch>47)&&(ch<58)))
Num_check();
else
others();
}
}
}
fclose(fp_read);
fclose(fp_token);
fclose(fp_list);
}
void clean(char *c)
{
for(int i=0;i<NAMEMAX;i++)
c[i]=NULL;
}
void ID_check()
{
int i=0,tag=0;
while(((ch>64)&&(ch<90))||((ch>96)&&(ch<123))||ch=='_'||((ch>47)&&(ch<58)))
{
l.name[i]=ch;
i++;
ch=fgetc(fp_read);
}
for(i=0;i<REALNUM;i++)
{
if(!strcmp(l.name,key[i].name))
{
l.type=key[i].type;
l.index=-1;
tag=1;
break;
}
}
if(tag!=1)
{
l.type=24;
l.index=token_index++;
}
Print();
}
void Notice_check()
{
char ch_next;
ch=fgetc(fp_read);
if(ch='*')
{
while(1)
{
ch=fgetc(fp_read);
if(ch=='*')
{
ch_next=fgetc(fp_read);
if(ch_next=='/')
{
ch=fgetc(fp_read);
return;
}
}
}
}
else if(ch='/')
{
ch=fgetc(fp_read);
while(ch!='/t')
ch=fgetc(fp_read);
ch=fgetc(fp_read);
return;
}
else
{
l.index=-1;
l.name[0]='/';
l.type=25;
Print();
}
}
void Num_check()
{
int i=0;
l.type=23;
while(((ch>47)&&(ch<58)))
{
l.name[i]=ch;
i++;
ch=fgetc(fp_read);
if(ch=='.')
{
l.name[i]='.';
l.type=26;
i++;
ch=fgetc(fp_read);
while(((ch>47)&&(ch<58)))
{ l.name[i]=ch;
i++;
ch=fgetc(fp_read);
}
}
}
l.index=token_index++;
Print();
}
void others()
{
char tem;
if(ch== '<')
{
tem=ch;
ch=fgetc(fp_read);
if(ch=='<')
{
l.type=9;
l.name[0]=l.name[1]='<';
l.index=-1;
Print();
ch=fgetc(fp_read);
return;
}
else
{
l.type=2;
l.name[0]='<';
l.index=-1;
Print();
return;
}
}
else if(ch== '+')
{
tem=ch;
ch=fgetc(fp_read);
if(ch=='+')
{
l.type=10;
l.name[0]=l.name[1]='+';
l.index=-1;
Print();
ch=fgetc(fp_read);
return;
}
else
{
l.type=11;
l.name[0]='+';
l.index=-1;
Print();
return;
}
}
else if(ch==10||ch==13)
{
line_index++;
ch=fgetc(fp_read);
return;
}
else if(ch==' '||ch==9)
{
ch=fgetc(fp_read);
return;
}
else
{
int flag=match(ch);
if(flag==-1)
cout<<"error出現(xiàn)在"<<line_index<<"行";
else
{
l.index=-1;
l.name[0]=key[flag].name[0];
l.type=key[flag].type;
Print();
}
ch=fgetc(fp_read);
}
}
int match(char ch)
{
for(int i=0;i<REALNUM;i++)
{
if(ch==key[i].name[0])
return i;
}
return -1;
}
void Print()
{
int flag;
if(l.type==23||l.type==24||l.type==26)
{
flag=check();
if(flag)
{
fprintf(fp_list,"%3d %3d %s\n",l.type,l.index,l.name);
}
}
if(!flag)
{
l.index=find();
fprintf(fp_token,"%3d %3d %s\n",l.type,l.index,l.name);
}
else
fprintf(fp_token,"%3d %3d %s\n",l.type,l.index,l.name);
}
int check()
{
int flag;
for(int i=0;i<list_index;i++)
{
flag=strcmp(l.name,List[i].name);
if(!flag)
{
return 0;
}
}
l.index=list_index;
List[list_index].index=l.index;
strcpy(List[list_index].name,l.name);
List[list_index].type=l.type;
list_index++;
return 1;
}
int find()
{
for(int i=0;i<list_index;i++)
{
if(!strcmp(l.name,List[i].name))
return i;
}
return -1;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -