?? word.cpp
字號:
#include ".\word.h"
#using <mscorlib.dll>
Word::Word(void)
{inword="void main int short char float double long unsigned struct sizeof auto static register extern typedef union enum if else goto while do switch case return break continue for";
sign="+ - * / % ++ -- += -= *= /= %= = [ ] & ^ | ~ << >> ! ? : < > . && || == >= <= ->";
marco="#define #undefine #endif #include #ifdef #else #ifndef";
bound="; { } ( ) \" \' , ";
word[0]='\0';
flag=0;//默認不寫入程序源碼注釋
flagto_txt=0;//注釋形式 /* */ 的開關
}
bool Word::setFlag()
{
flag=!flag;return 1;
}
bool Word::isSign(char frist,char second)
//功能:檢查字符是不是屬于運算符集 不是返回0 是返回1
{
char arry[3];
arry[0]=frist;
arry[1]=second;
arry[2]='\0';
//cout<<arry<<endl;getch();
if(sign.isIn(arry)) return 1;
else return 0;
}
bool Word::isBound(char data)
//功能:判斷字符是否是界符,是返回1 否則返回0
{
char arry[2];
arry[0]=data;
arry[1]='\0';
if(bound.isIn(arry)) return 1;
else return 0;
}
bool Word::isMarco(char *arry)
//功能:判斷是否是預編譯命令 是返回1 否則返回 0
{
if(marco.isIn(arry) ) return 1;
else return 0;
}
bool Word::isInWord(char *arry)
//檢查字符串是否是保留字 是返回1 不是返回 0
{
if(inword.isIn(arry) ) return 1;
else return 0;
}
bool Word::isNum(char data)
{//判斷是否是數字字符
if(data>='0'&&data<='9') return 1;
else return 0;
}
bool Word::isWord(char data)
{//判斷是否是字母
if( (data>='a'&&data<='z')||(data>='A'&&data<='Z') ) return 1;
else return 0;
}
bool Word::outPut(char *word,char *strings)
{
//功能:將word和strings指向的字符串寫入文件
outfile.write(word,strlen(word));// write to file
outfile.put('\t');//寫入一個制表符
if(strings)outfile.write(strings,strlen(strings));
outfile.put(10);//寫入一個回車
return 1;
}
bool Word::outPut(char data,char *strings)
{ //功能:將字符data寫進文件
outfile.put(data);
outfile.put('\t');
outfile.write(strings,strlen(strings));
outfile.put(10);
return 1;
}
bool Word::outPut(char data1,char data2,char *strings)
{
//將字符data1和字符data2和屬性文字strings寫進文件
outfile.put(data1);
outfile.put(data2);
outfile.put('\t');
outfile.write(strings,strlen(strings));
outfile.put(10);
return 1;
}
bool Word::infIn()
{ outputfilename[0]='\0';
ifstream infile;//輸入文件流對象
char filename[30];//存放文件名
cout<<"[輸入一個供分析的C語言源文件:]"<<endl;
cout<<" [源程序文件名]:";
cin>>filename;
infile.open(filename,ios_base::in);
if(infile==NULL)
{
cout<<" [打開源文件失敗]:請檢查文件名是否輸入正確.";getch();return 0;
}
cout<<"[輸入保存分析結果的文件名:]"<<endl;
while(1)
{
cout<<" [保存到文件]:";
cin>>outputfilename;
ifstream outtext(outputfilename,ios_base::in);
if(outtext)
{
cout<<" [警告]:文件已經存在,要覆蓋嗎?[y/n]";
char c;
c=getch();
if(c=='y'){outtext.close();break;}//同意覆蓋
continue;//重新輸入文件名
}
outtext.close();
break;
}//while(1)
//將輸入的文件名與私有數據 ofstream outfile 掛鉤
outfile.open(outputfilename,ios_base::out);
if(!outfile)
{
cout<<"[錯誤]:輸出文件創建失敗."<<endl;getch();exit(0);
}
outPut("[詞法分析的結果]");
outPut("[分析 的 文 件:]",filename);
outPut("[當 前 文 件:]",outputfilename);
outPut("[分析結 果如下:]");
char linebuffer[81];//行緩沖區
system("cls");
cout<<"[開始對文件進行分析:]"<<endl;
while(infile.good() )
{
infile.getline(linebuffer,80,10); //讀一行到行緩沖區
cout<<"//"<<linebuffer<<endl;
if(flag) outPut("//",linebuffer);
//cout<<endl;
wordAnalyze(linebuffer); //對這一行做詞法分析
}
//getch();
infile.close();
outfile.close();
return 1;
}
bool Word::wordAnalyze(char *line)
{//功能:對一行做詞法分析
if( strlen(line) == 0) return 1;//不對空行做詞法分析
char *p;
bool sign=0;
p=line;
int i;
while(*p)
{
if(*p==' '||*p==10||*p=='\t'){p++;continue;}//過濾空格和回車符
if(*p=='/'&& *(p+1)=='/') return 1;//過濾掉注釋
if(*p=='/'&& *(p+1)=='*')
{
flagto_txt=!flagto_txt;p=p+2;
}
if(flagto_txt)
{
while(*p&& !( *p=='*' && *(p+1)=='/') ) p++;
if(! *p) return 0;//還沒有出現與/* 匹配的 */ 返回
flagto_txt=!flagto_txt;//出現與/*匹配的*/ 繼續
p=p+2;//跳過 */
if(! *p) return 1;
}
if(*p=='#')
{
i=0;
while(*p!=' '&&*p && *p!='<')
{
word[i]=*p;
p++;i++;
}
word[i]='\0';
if(marco.isIn(word))
{
cout<<word<<" "<<"預編譯命令"<<endl;
outPut(word,"預編譯命令");
}
else
{
cout<<word<<" "<<"非法形式"<<endl;
outPut(word,"非法形式");
}
if(! *p) return 1;
else continue;
}
else if( isWord(*p) ) //是字母
{ i=0;
while(isWord(*p) && *p )//是保留字或者是標識符
{
word[i]=*p;
p++;i++;
}
if(*p=='_')
while(*p!=' '&&*p!=10&& *p&&!isBound(*p) )
{
word[i]=*p;
p++;i++;
}
word[i]='\0';//分離出一個詞
//cout<<word<<endl;getch();
if(isInWord(word))
{
cout<<word<<" "<<"保留字"<<endl;
outPut(word,"保留字");
}
else
{
outPut(word,"標識符或者過程名");
cout<<word<<" "<<"標識符或過程名"<<endl;
}
continue;
}//else if
else if( isNum(*p) )//是數字
{ i=0;
while(*p=='.'||isNum(*p) )
{
word[i]=*p;
i++;
p++;
}
if(*p=='_')
{
while(*p!=' '&&*p!=10 && *p&& !isSign(*p) )
{
word[i]=*p;
p++;i++;
}
word[i]='\0';
cout<<word<<" "<<"字符常量或標識符"<<endl;//分離出數
outPut(word,"字符常量或標識符");
}
else
{
word[i]='\0';
cout<<word<<" 數據常量"<<endl;
outPut(word,"數據常量");
}
continue;
}//else if
else if(isSign(*p)) //是運算符
{ if( *(p+1) && isSign( *p,*(p+1))) //雙字符運算符
{
cout<<*p<<*(p+1)<<" "<<"運算符"<<endl;
outPut(*p,*(p+1),"運算符");
p=p+2;
continue;
}
else//單字符運算符
{
cout<<*p<<" "<<"運算符"<<endl;
outPut(*p,"運算符");
p++;
//getch();
}//else
if(!*p) return 1;//完成
continue;
}//if
else if(isBound(*p)) //如果是分界符
{
if(*p=='\"')
{ cout<<*p<<" 分界符"<<endl;
outPut(*p,"分界符");
sign=!sign;//引號開關 1:前引號 0 后引號
if(sign)
{ i=0;
p++;//跳過前引號
while(*p!='\"'&&*p!=10)
{
word[i]=*p;
p++;i++;
}
word[i]='\0';
cout<<word<<" 字符串常量"<<endl;
outPut(word,"字符串常量");
}//if
if(*p=='\"')
{ cout<<*p<<" 分界符"<<endl;
outPut(*p,"分界符");
p++;//跳過后引號
}
}//if
else
{
cout<<*p<<" 分界符"<<endl;
outPut(*p,"分界符");
p++;
}
if(! *p) return 1;
else continue;
}//else if
else if(*p=='_')
{ i=0;
while(*p!=' '&& *p!=10 && *p &&!isSign(*p) )
{
word[i]=*p;
p++;i++;
}
word[i]='\0';
cout<<word<<" 字符常量或標識符"<<endl;
outPut(word,"字符常量或標識符");
continue;
}
else
{ if(*p!=10) cout<<*p<<" "<<"非法字符"<<endl;
outPut(*p,"非法字符");
p++;
}//else
}//while(*p);
return 1;
}
bool Word::forMain()
{
char c[10];
while(1)
{
system("cls");
cout<<" [C語言詞法分析程序]"<<endl;
cout<<" [孤單北半球@2005_12_28]"<<endl;
cout<<" [go] 詞法分析."<<endl;
cout<<" [exit] 退出."<<endl;
cout<<" [detel] 是否向輸出文件寫入注釋形式的源碼."<<endl;
cout<<"□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□□"<<endl;
if(flag)cout<<" [細節顯示]"<<endl;
else cout<<endl;
cout<<" [輸入命令]:";
cin>>c;
if(strcmp(c,"go")==0)
{ system("cls");
cout<<"[詞法分析:]"<<endl;
infIn();
cout<<"[任意鍵返回到菜單...]";
getch();
}
else if(strcmp(c,"exit")==0)break;
else if(strcmp(c,"detel")==0) setFlag();
else
{
cout<<"[錯誤]:"<<c<<"不是一個預定的命令";
getch();
}
}//while(1)
return 1;
}//forMain();
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -