📄 xing.cpp
字号:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <iostream.h>
#include <stdlib.h>
//定义单词编码表的数据结构
struct symbolrecord
{ char symbol[10];
int id;
} ;
//定义符号表的数据结构
struct entrytype
{ char idname[10];
int address;
int type;
};
//定义常数表的数据结构
struct digittype
{ int num;
int address;
};
//Token字的数据结构
struct tokentype
{ int id;
int entry;
};
FILE *fp; //源文件指针
struct digittype d[10]; //定义常数表,个数指针
struct entrytype a[40]; //定义符号表,个数指针
int k=0,t=0;
//单词编码表初始化
struct symbolrecord s[30]=
{ "func",0,
"while",1,
"do",2,
"If",3,
"Then",4,
"Else",5,
"true",6,
"false",7,
"int",8,
"const",9,
"{",11,
"}",12,
";",13,
",",14,
"=",15,
"!",16,
"&",17,
"|",18,
"<",19,
">",20,
"<=",21,
">=",22,
"<>",23,
"==",24,
"+",25,
"*",26,
"(",27,
")",28,
"id",29};
//识别标识符算法
tokentype recogid(char ch)
{ tokentype tokenid;
FILE *fs;
int flag,fflag;
char word[10]={0};
int i,j;
i=0;
while(isalpha(ch)||isdigit(ch))
{ word[i]=ch;
ch=fgetc(fp);
i=i+1;
}
ungetc(ch,fp);
word[i]='\0';
for(j=0;j<=8;j++)
{ flag=strcmp(word, s[j].symbol);
if(flag==0) //是关键字
{ tokenid.id=j;
tokenid.entry=-1;
break;
}
}
if(flag!=0)
{ for(j=0;j<=k;j++)
{ fflag=strcmp(a[j].idname,word);
if(fflag==0) //在符号表中可以找到
{ tokenid.id=29;
tokenid.entry=a[j].address;
break;
}
}
if(fflag!=0)
{ fs=fopen("symbol.txt","a"); //符号表中不存在的标识符
strcpy(a[k].idname, word);
a[k].address=k;
a[k].type=29;
tokenid.id=29;
tokenid.entry=k;
for(j=0;j<9;j++)
fprintf(fs,"%c",word[j]);
fprintf(fs,"%c",'\t');
fprintf(fs,"%d",a[k].address);
fprintf(fs,"%c",'\t');
fprintf(fs,"%d",a[k].type);
fprintf(fs,"%c",'\n');
fclose(fs);
k=k+1;
}
}
return tokenid;
}
///识别数字函数
tokentype recogdig(char ch)
{ int flag;
int i=0,j;
int num=0;
tokentype tokenid;
while(isdigit(ch))
{ num=(ch-48)+num*10;
ch=fgetc(fp);
i=i+1;
}
for(j=0;j<=t;j++)
if(d[j].num==num)
{ flag=1;
tokenid.id=9;
tokenid.entry=d[j].address;
break;
}
if(flag!=1)
{ d[t].num=num;
d[t].address=t;
tokenid.id=9;
tokenid.entry=t;
t=t+1;
}
return tokenid;
}
//识别算符和界符函数
tokentype recogdel(char ch)
{ tokentype tokenid;
switch(ch)
{ case'{':tokenid.id=11;break;
case'}':tokenid.id=12;break;
case';':tokenid.id=13;break;
case',':tokenid.id=14;break;
case'=':ch=fgetc(fp);
if(ch=='=') tokenid.id=24;
else { tokenid.id=15;
ungetc(ch,fp);
};
break;
case'!':tokenid.id=16;break;
case'&':tokenid.id=17;break;
case'|':tokenid.id=18;break;
case'<':ch=fgetc(fp);
if(ch=='=') tokenid.id=21;
else
if(ch=='>') tokenid.id=23;
else{ tokenid.id=19;
ungetc(ch,fp);
};
break;
case'>':ch=fgetc(fp);
if(ch=='=') tokenid.id=22;
else { tokenid.id=20;
ungetc(ch,fp);
};
break;
case'+':tokenid.id=25;break;
case'*':tokenid.id=26;break;
case'(':tokenid.id=27;break;
case')':tokenid.id=28;break;
}
tokenid.entry=-1;
return tokenid;
}
/////////////////////handlecom函数////////////
tokentype handlecom(char ch)
{ tokentype tokenid;
char ch1;
int flag=0;
if(ch!='*' )
{ tokenid.id=29;
tokenid.entry=-1;
}
else
{ while(flag==0)
{ ch1=ch;
ch=fgetc(fp);
if((ch1='*')&&(ch='/'))
flag=1;
}
}
return tokenid;
}
///////////sort函数/////////
void sort(char ch)
{
struct tokentype tokenword;
FILE * fq = fopen("tokenfile.txt","a");
if(isalpha(ch))
tokenword=recogid(ch); //字母
else if(isdigit(ch))
tokenword=recogdig(ch); //数字
else if(ch=='/')
tokenword=handlecom(ch);
else
tokenword=recogdel(ch);
printf("%d\t%d\n",tokenword.id,tokenword.entry);
fprintf(fq,"%d",tokenword.id);
fprintf(fq,"%c",'\t');
fprintf(fq,"%d",tokenword.entry);
fprintf(fq,"%c",'\n');
fclose(fq);
}
/////////////scanner函数////////
void scanner()
{ char ch;
fp=fopen("source.txt","r");
ch=getc(fp);
while(ch!=EOF)
{ if(!isspace(ch))
{ sort(ch);
}
ch=fgetc(fp);
}
fclose(fp);
}
void main()
{ int i;
printf("token字如下:\n");
scanner();
printf("************************************\n");
printf("符号表如下:\n");
printf("%s\t\t%s\t\t%s\n","idname","address","type");
for(i=0;i<=k-1;i++)
printf("%s\t\t%d\t\t%d\n",a[i].idname,a[i].address,a[i].type);
printf("************************************\n");
printf("常数表如下:\n");
printf("%s\t\t%s\n","num","address");
for(i=0;i<=t-1;i++)
printf("%d\t\t%d\n",d[i].num,d[i].address);
cout<<endl;
system("pause");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -