📄 kfs.cpp
字号:
#include<stdio.h>
#include <string.h>
//*****************
//定义关键字表结构
struct keywordtable{
char keyword[10];
int id;
}keyword_table[100];
//定义符号表结构
struct symboltable{
char idname[10];
int address;
char type[10];
}symbol_table[100];
//定义TOKEN字表结构
struct tokentable{
int id;
int entry;
}token_table[100];
//定义常数表结构
struct consttable{
int value;
int address;
char type[10];
}const_table[100];
//******************
//******************
//定义要用到的变量、指针和数组
int token_address=0;//token表地址声明
int symbol_address=0;//符号表地址声明
int const_address=0;//常量表地址声明
char keyword_buffer[10];//保存最近读到的一个关键字
FILE *program_filepoint;//源程序文件全局指针
int line=0;//行计数
//******************
//******************
//定义要用到的函数
char recognize_id(char ch_program);//识别标识符
char recognize_boundary(char ch_program);//识别界符
char recognize_const(char ch_program);//识别常量
int save_output(void);//保存结果
void Intialization(void);//初始化文件
//******************
//******************
//主函数
int main(void)
{
char program_ch;
Intialization();//初始化
program_filepoint=fopen("c://test.txt","r");//打开位于c盘根目录下的测试文件test.txt
if(program_filepoint==NULL){
printf("The test is not exist!\n");
return 1;
}//错误处理
while((program_ch=fgetc(program_filepoint))!= EOF){
if(program_ch>='a'&&program_ch<='z')
recognize_id(program_ch);//若为字母则调用函数recognize_id
else if(program_ch>='0'&&program_ch<='9')
recognize_const(program_ch);//若为数字则调用函数recognize_const
else if(program_ch==' '||program_ch=='\n'||program_ch==' '){
if(program_ch=='\n')
line++;//靠回车来改变行计数
}
else
recognize_boundary(program_ch);//识别界符
}
fclose(program_filepoint);//关闭文件
save_output();//保存结果
return 0;
}
//*****************
//*****************
//初始化文件函数
void Intialization(void){
char symbol_buff='s';
int i=0,j=0;
FILE *fp;
for(i=0;i<100;i++){
keyword_table[i].id=0;
for(j=0;j<10;j++){
keyword_table[i].keyword[j]='\0';
symbol_table[i].idname[j]='\0';
symbol_table[i].type[j]='\0';
const_table[i].type[j]='\0';
}
}//初始化数组,方便printf打印
i=0;
j=0;
if((fp=fopen("c://keyword.txt","r"))!=NULL){
while((symbol_buff=fgetc(fp))!=EOF){
while(symbol_buff!=' '&&symbol_buff!='\n'){
keyword_table[i].keyword[j]=symbol_buff;
symbol_buff=fgetc(fp);
j++;
}
j=0;
while((symbol_buff=fgetc(fp))!='\n'){
keyword_table[i].id*=10;
keyword_table[i].id+=symbol_buff-'0';
}
i++;
}//把关键字表keyword.txt读入内存中方便与待测文件比较
fclose(fp);
}
else
printf("The file is not exist!\n");
}
//*******************
//*******************
//识别标识符函数
char recognize_id(char ch_program){
char entry_buffer[10];
int i=0,compare_count=0;//compare_count比较计数变量
for(i=0;i<10;i++)
entry_buffer[i]='\0';
i=0;//初始化数组,方便printf打印
while(ch_program>='a'&&ch_program<='z'){
entry_buffer[i]=ch_program;
ch_program=fgetc(program_filepoint);
i++;
}//保存输入字母
ungetc(ch_program,program_filepoint);//列回退
while(strcmp(entry_buffer,keyword_table[compare_count].keyword)!=0&&compare_count<100){
compare_count++; //比较关键字,若不相等并且比较次数少于关键字继续比较
}
i=0;
if(compare_count<100){
//如果是关键字,将关键字保存到token表,并且将符号表入口地址设为 -1
token_table[token_address].id=keyword_table[compare_count].id;
token_table[token_address].entry=-1;
if(compare_count<=1)//声明变量类型的关键字位于关键字表的最前端 此时只有int 0 后添加只需更改此处
strcpy(keyword_buffer,entry_buffer);//将识别到的关键字保存到缓冲中备用
token_address++;
}
else{
//是变量,判断是否出现过
while(strcmp(entry_buffer,symbol_table[i].idname)!=0&&i<100){
i++;
}
if(i>=symbol_address){//未出现过的新变量
strcpy(symbol_table[symbol_address].idname,entry_buffer);//保存变量名
symbol_table[symbol_address].address=symbol_address;//保存在符号表中的相对地址
strcpy(symbol_table[symbol_address].type,keyword_buffer);//保存变量类型
token_table[token_address].id=10;//类型 id的ID是10
token_table[token_address].entry=symbol_address;//保存符号表入口
token_address++;//token、关键字表地址增加1
symbol_address++;
}
else{
token_table[token_address].id=10;//类型 id的ID是10
token_table[token_address].entry=symbol_table[i].address;//保存符号表原变量入口
token_address++;//token、符号表地址增加1
}
}
return 0;
}
//******************
//******************
//识别常数函数
char recognize_const(char ch_program){
int value=0;
while(ch_program>='0'&&ch_program<='9'){ //将char[]转换为int类型
value*=10;
value+=ch_program-'0';
ch_program=fgetc(program_filepoint);
}
ungetc(ch_program,program_filepoint);//列回退
const_table[const_address].value=value;//表中常量数值
const_table[const_address].address=const_address;//常量在常量表中的相对地址
strcpy(const_table[const_address].type,keyword_buffer);//保存常量类型
token_table[token_address].id=9;//类型 const的ID是9
token_table[token_address].entry=const_address;//保存常量表入口
token_address++;
const_address++;
return 0;
}
//******************
//******************
//识别界符函数
char recognize_boundary(char ch_program){
int i=0;
while(ch_program!=keyword_table[i].keyword[0]&&i<100){
i++;
}
if(i<100){
//查找到标记符号
token_table[token_address].id=keyword_table[i].id;
token_table[token_address].entry=-1;
token_address++;
}
else {
printf("The other character is in line:%d.\n", line);
}
return 0;
}
//******************
//******************
//保存结果函数
int save_output(void){
FILE* new_file;
int i=0,j=0,k=0;
new_file=fopen("token.txt","w");//写入token字表
if(new_file==NULL)
return 0;
printf("**********\n");
printf("The Table Of Token Is\n");
while(i<token_address){
fprintf(new_file,"%d %d\n",token_table[i].id,token_table[i].entry);
printf("%d %d\n",token_table[i].id,token_table[i].entry);
i++;
}//在文件和屏幕上分别显示token字表
fclose(new_file);
new_file=fopen("const.txt","w");//写入常数表
if(new_file==NULL)
return 0;
printf("**********\n");
printf("\n");
printf("**********\n");
printf(" The Table Of Const Is\n");
while (j<const_address) {
fprintf(new_file,"%d %d %s\n",const_table[j].address,const_table[j].value,const_table[j].type);
printf("%d %d %s\n",const_table[j].address,const_table[j].value,const_table[j].type);
j++;
}//在文件和屏幕上分别显示常数表
fclose(new_file);
new_file=fopen("symbol.txt","w");//写入字符表
if(new_file==NULL)
return 0;
printf("**********\n");
printf("\n");
printf("**********\n");
printf("The Table Of Symbol Is\n");
while (k<symbol_address) {
fprintf(new_file,"%d %s %s\n",symbol_table[k].address,symbol_table[k].idname,symbol_table[k].type);
printf("%d %s %s\n",symbol_table[k].address,symbol_table[k].idname,symbol_table[k].type);
k++;
}//在文件和屏幕上分别显示字符表
printf("**********\n");
return 0;
}
//*****************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -