?? 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 + -