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