?? analyst.cpp
字號:
// ANALYST.cpp: implementation of the ANALYST class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "ANALYST.h"
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
ANALYST::ANALYST()
{
char *key[]={"auto","break","case","char","const","continue","default","do","double",
"else","enum","extern","float","for","goto","if","int","long","register",
"return","short","signed","sizeof","static","struct","switch","typedef",
"union","unsigned","void","volatile","while"}; //C++語言關鍵字
char *limit[]={"+=","-=","*=","/=","==","!=","&&","||",">=","<=",">>","<<","--","++",
"->","#","(",")","[","]",".","!","~","*","/","%","+","-","<",">",
"=",",",";","{","}","_","'",":","&"};//運算、限界符
Line=1;
Errorno=0;
fstream outfile;
int i,j;
outfile.open("Key.txt",ios::out); //創建關鍵字表并寫入關鍵字
for(i=0;i<32;i++)
outfile<<key[i]<<endl;
outfile.close();
outfile.open("Limit.txt",ios::out); //創建運算符、界符表并寫入運算符、界符
for(j=0;j<39;j++)
outfile<<limit[j]<<endl;
outfile.close();
outfile.open("Bsf.txt",ios::out); //創建標識符表
outfile.close();
outfile.open("Data.txt",ios::out); //創建數據表
outfile.close();
outfile.open("Output.txt",ios::out); //創建目標代碼文件
outfile<<'1'<<" ";
outfile.close();
outfile.open("Error.txt",ios::out); //創建出錯記錄文件
outfile.close();
}
ANALYST::~ANALYST()
{
}
void ANALYST::Bsfdeal(char *word) //關鍵字,標識符處理函數
{
fstream outfile,infile,outchar;
int number=0,find=0,i;
char ch;
char temp[30];
infile.open("Key.txt",ios::in); //打開關鍵字表,尋找匹配關鍵字
infile.get(ch);
while(ch!=EOF&&ch!='\n')
{
i=0;
while(ch!='\n')
{
temp[i++]=ch;
infile.get(ch);
}
temp[i]='\0';
number++;
if(strcmp(temp,word)==0)
{
outchar.open("Output.txt",ios::app); //若找到匹配關鍵字,寫入目標代碼文件
outchar<<word<<" ";
outchar.close();
infile.close();
find=1;
}
else //向下搜尋
infile.get(ch);
}
infile.close();
if(find==0) //若不是關鍵字,則搜尋標識符
{
infile.open("Bsf.txt",ios::in);
infile.get(ch);
while(ch!=EOF&&ch!='\n')
{
i=0;
while(ch!='\n')
{
temp[i++]=ch;
infile.get(ch);
}
temp[i]='\0';
number++;
if(strcmp(temp,word)==0) //若找到匹配標識符,寫入目標代碼文件
{
outchar.open("Output.txt",ios::app);
outchar<<word<<" ";
outchar.close();
infile.close();
find=1;
}
else
infile.get(ch); //向下搜尋
}
infile.close();
if(find==0) // 若未找到匹配標識符,則為新標識符,寫入標識符表
{
outfile.open("Bsf.txt",ios::app);
outfile<<word<<endl;
outfile.close();
outchar.open("Output.txt",ios::app); //新標識符寫入目標代碼文件
outchar<<word<<" ";
outchar.close();
}
}
}
void ANALYST::Intdeal(char *word) //數據處理函數
{
fstream infile,outfile,outchar;
int number=0,find=0,i;
char ch;
char temp[30];
infile.open("Data.txt",ios::in); //打開數據表,尋找匹配數據
infile.get(ch);
while(ch!=EOF&&ch!='\n')
{
i=0;
while(ch!='\n')
{
temp[i++]=ch;
infile.get(ch);
}
temp[i]='\0';
number++;
if(strcmp(temp,word)==0) //若找到匹配數據,寫入目標代碼文件
{
outchar.open("Output.txt",ios::app);
outchar<<word<<" ";
outchar.close();
infile.close();
find=1;
}
else //否則,向下搜尋
infile.get(ch);
}
infile.close();
if(find==0) //若未找到,則寫入數據表
{
outfile.open("Data.txt",ios::app);
outfile<<word<<endl;
outfile.close();
outchar.open("Output.txt",ios::app); //寫入目標代碼文件
outchar<<word<<" ";
outchar.close();
}
}
void ANALYST::Limitdeal(char *word, int line,fstream Infile) //運算符、界符處理函數
{
int number=0,find=0,i;
char ch;
char temp[30];
fstream infile,outfile, outchar;
infile.open("Limit.txt",ios::in); //打開界符表,開始搜尋
infile.get(ch);
while(ch!='#'&&ch!='\n') //先搜尋雙字符
{
i=0;
while(ch!='\n')
{
temp[i++]=ch;
infile.get(ch);
}
temp[i]='\0';
number++;
if(strcmp(temp,word)==0) //若找到,寫入目標代碼文件
{
outchar.open("Output.txt",ios::app);
outchar<<word<<" ";
outchar.close();
infile.close();
find=1;
}
else
infile.get(ch);
}
if(find==0) //若不是雙字符,則搜尋單字符
{
Infile.seekg(-1,ios::cur);
word[1]='\0';
while(ch!=EOF&&ch!='\n')
{
i=0;
while(ch!='\n')
{
temp[i++]=ch;
infile.get(ch);
}
temp[i]='\0';
number++;
if(strcmp(temp,word)==0) //若為單字符,寫入目標代碼文件
{
outchar.open("Output.txt",ios::app);
outchar<<word<<" ";
outchar.close();
infile.close();
find=1;
}
else
infile.get(ch);
}
infile.close();
if(find==0) //否則為無法識別字符,進行錯誤處理
{
Errorno++;
outfile.open("Error.txt",ios::app); //錯誤信息寫入出錯記錄文件
outfile<<"第"<<line<<"行"<<"字符"<<" "<<word<<" "<<"出錯"<<endl;
outfile.close();
outfile.open("Output.txt",ios::app); //標記出錯點
outfile<<"^_^";
outfile.close();
}
}
}
void ANALYST::Scanner() //掃描函數
{
char temp[30],line[80];
char ch;
char*word;
int i;
fstream Infile, Outfile,Intxt;
char Filename[20];
cout<<"請輸入需編譯的文件名:";
cin>>Filename;
Infile.open(Filename,ios::nocreate|ios::in); //打開需編譯的文件
while(! Infile)
{
cout<<"此文件不存在"<<endl;
exit(0);
}
cout<<endl<<endl;
cout<<"需編譯的文件內容為:"<<endl;
Intxt.open(Filename,ios::in); //輸出需編譯的文件內容
while(Intxt.getline(line,80))
cout<<line<<endl;
Intxt.close();
Infile.get(ch); //開始掃描文件
while(ch!=EOF) //按字符依次掃描源程序,直至結束
{
i=0;
if(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')) //識別標識符
{
while(((ch>='A')&&(ch<='Z'))||((ch>='a')&&(ch<='z'))||(ch=='_')||((ch>='0')&&(ch<='9')))
{
temp[i++]=ch;
Infile.get(ch);
}
word=new char[i+1];
memcpy(word,temp,i);
word[i]='\0';
Bsfdeal(word);
delete []word;
Infile.seekg(-1,ios::cur);
}
else if(ch>='0'&&ch<='9')
{ //識別數字
while(ch>='0'&&ch<='9')
{
temp[i++]=ch;
Infile.get(ch);
}
word=new char[i+1];
memcpy(word,temp,i);
word[i]='\0';
Intdeal(word);
delete []word;
Infile.seekg(-1,ios::cur);
}
else if((ch==' ')||(ch=='\t')) //消除空格符和水平制表符
{
}
else if(ch=='\n') //消除回車并記錄行數
{
Line++;
Outfile.open("Output.txt",ios::app);
Outfile<<endl;
Outfile<<Line<<" ";
Outfile.close();
}
else if(ch=='/')
{ //消除注釋
Infile.get(ch);
if(ch=='=')
{ //判斷是否為‘/=’符號
Outfile.open("Output.txt",ios::app);
Outfile<<"/="<<' ';
Outfile.close();
}
else if(ch=='*')
{ //若為多行注釋的開始,消除包含在里面的所有字符
int count=0;
Infile.get(ch);
while(count!=2)
{ //當掃描到‘*’且緊接著下一個字符為‘/’才是注釋的結束
count=0;
while(ch!='*')
Infile.get(ch);
count++;
Infile.get(ch);
if(ch=='/')
count++;
else
Infile.get(ch);
}
}
else if(ch=='/') //消除單行注釋
{
do
Infile.get(ch);
while(ch!='\n'&&ch!=EOF);
if(ch=='\n')
{
Line++;
Outfile.open("Output.txt",ios::app);
Outfile<<endl;
Outfile<<Line<<" ";
Outfile.close();
}
else
Infile.seekg(-1,ios::cur);
}
else if(ch==EOF) //若為文件末尾,則后退一個字符
Infile.seekg(-1,ios::cur);
else
{
Outfile.open("Output.txt",ios::app);
Outfile<<"/"<<" ";
Outfile.close();
Infile.seekg(-1,ios::cur);
}
}
else if(ch=='"')
{ //處理包含在雙引號中的字符串常量
Outfile.open("Output.txt",ios::app);
Outfile<<ch<<" ";
do
{
Infile.get(ch);
Outfile<<ch;
}
while(ch!='"');
Outfile<<" ";
Outfile.close();
}
else
{ //首字符為其它字符,即運算符,界符或非法字符
temp[0]=ch;
Infile.get(ch); //讀入下一個字符,判斷是否為雙字符運算、限界符
if(ch!=EOF&&ch!='\n')
{
temp[1]=ch;
word=new char[3];
memcpy(word,temp,2);
word[2]='\0';
Limitdeal(word,Line,Infile); //若為運算符界符,調用函數進行處理
delete []word;
}
else
{ //若讀入的下一個字符為文件結束符,處理后需后退
word=new char[2];
memcpy(word,temp,1);
word[1]='\0';
Limitdeal(word,Line,Infile);
delete []word;
Infile.seekg(-1,ios::cur);
}
}
Infile.get(ch);
}
Infile.close();
cout<<endl<<endl;
cout<<"編譯后的目標代碼為:"<<endl;
Infile.open("Output.txt",ios::in); //輸出編譯后的目標代碼
while(Infile.getline(line,80))
cout<<line<<endl;
Infile.close();
if(Errorno!=0)
{
cout<<"文件共有"<<Errorno<<"個錯誤"<<endl;
Infile.open("Error.txt",ios::in);
while(Infile.getline(line,80)) //輸出錯誤信息
cout<<line<<endl;
Infile.close();
}
else
cout<<"文件錯誤0個"<<endl;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -