📄 wordanalyse.cpp
字号:
// WordAnalyse.cpp: implementation of the CWordAnalyse class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
//#include "Compiler.h"
#include "WordAnalyse.h"
#include "assert.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//基本字表的初始化工作
CTwoDim CWordAnalyse::Table_BaseWord[SIZE_TB]={
{CTwoDim($IF,"if")},///////////////////
{CTwoDim($ELSE,"else")},
{CTwoDim($WHILE,"while")},
{CTwoDim($SIGN,"\0")},////////////////////副作用???
{CTwoDim($PLUS,"+")},
{CTwoDim($MINUS,"-")},
{CTwoDim($STAR,"*")},
{CTwoDim($DIVIDE,"/")},
{CTwoDim($AND,"&")},
{CTwoDim($OR,"|")},
{CTwoDim($LOW,"<")},
{CTwoDim($LARGE,">")},
{CTwoDim($EQU,"=")},
{CTwoDim($NOT,"!")},
{CTwoDim($LPAR,"(")},
{CTwoDim($RPAR,")")},
{CTwoDim($LBP,"{")},
{CTwoDim($RBP,"}")},
{CTwoDim($QUOT,"\"")},
{CTwoDim($DOTCO,";")},
{CTwoDim($COMMA,",")},
{CTwoDim($DOT,".")},
{CTwoDim($FCONST,(double)0)},//////////////
{CTwoDim($ICONST,(long)0)},///////////////
{CTwoDim($SCONST,"\0")},
{CTwoDim($DAND,"&&")},
{CTwoDim($DOR,"||")},
{CTwoDim($LOEQ,"<=")},
{CTwoDim($LAEQ,">=")},
{CTwoDim($DEQ,"==")},
{CTwoDim($NEQ,"!=")}};
CWordAnalyse::CWordAnalyse(CEdit * pctrEdit,const CString& strEdit)
{
this->m_pctrEdit=pctrEdit;
this->m_sOriginalResource=strEdit;//原始程序
this->m_ptStr=NULL;//指针
this->m_sWord.Empty();
this->m_sChar.Empty();
this->m_sBuffRes.Empty();
// this->m_ptstrEndOfFile=NULL;
this->m_piPointer=0;
}
CWordAnalyse::~CWordAnalyse()
{
}
//如果即将读取的字符是文件尾结束符,则指针无动作
//(仍指向文件尾NULL,而整型指针无效(=getlenth())),char='\000'
//否则,读取该位置字符,指针后移
bool CWordAnalyse::GetChar()
{
this->m_sChar.Empty();//清空字符
VERIFY(this->m_ptStr!=NULL);
if(*this->m_ptStr!='\000'&&this->m_piPointer<this->m_sBuffRes.GetLength()){
if(IsDBCSLeadByteEx(0,*m_ptStr)){
this->m_sChar=this->m_ptStr[0];
this->m_sChar=this->m_sChar+this->m_ptStr[1];
this->m_piPointer+=2;
}
else{
this->m_sChar=this->m_ptStr[0];
this->m_piPointer++;
}
this->m_ptStr=CharNext(this->m_ptStr);//指针后移一个“单元字符”
}
else{
this->m_sChar='\000';
return false;
}
VERIFY(this->m_sChar.GetLength()<=2);
return true;
}
//
bool CWordAnalyse::GetNBC()
{
while(m_sChar=='\040'||m_sChar=='\r'||m_sChar=='\n'){
VERIFY(this->m_sChar.GetLength()==1);
if(!this->GetChar()) return false;
}
return true;
}
void CWordAnalyse::Concat()
{
this->m_sWord=this->m_sWord+this->m_sChar;
}
bool CWordAnalyse::IsLetterOrChinese()
{
PTSTR ptStr;
ptStr=this->m_sChar.GetBuffer(0);//TRACE("判断是否汉字或字母: %s\n",this->m_sChar);
if(IsDBCSLeadByteEx(0,*ptStr)){//chinese
return true;
}
if('A'<=this->m_sChar&&this->m_sChar<='Z'||
'a'<=this->m_sChar&&this->m_sChar<='z'){//letter
VERIFY(this->m_sChar.GetLength()==1);
return true;
}
return false;
}
bool CWordAnalyse::IsDigit()
{
PTSTR ptStr;
ptStr=this->m_sChar.GetBuffer(0);
if('0'<=this->m_sChar&&this->m_sChar<='9'){
VERIFY(!IsDBCSLeadByteEx(0,*ptStr));
VERIFY(this->m_sChar.GetLength()==1);
return true;
}
else{
return false;
}
}
bool CWordAnalyse::Fortransact(CString &str)
{
int lSp=0;//lSp 用作位置指示器
int i=0;
int j=0;
//--------------------------------------------------------------------
//TRACE("@@@@@@@@@@@@@@@@@@@@@@@@@2预处理前内部源程序长度为:%d\n",this->m_sBuffRes.GetLength());
//TRACE("@@@@@@@@@@@@@@@@@@@@@@@@@2预处理前用户源程序长度为:%d\n",this->m_sOriginalResource.GetLength());
//--------------------------------------------------------------------
int line=0;
bool flag=false;
for(i=0;i<str.GetLength()-3;i++){
flag=false;
if(str.GetAt(i)=='/'&&str.GetAt(i+1)=='*'){
for(line=i;line>0;line--){
if(str.GetAt(line)=='\n'&&str.GetAt(line-1)=='\r')break;
if(str.GetAt(line)=='/'&&str.GetAt(line-1)=='/'){
flag=true;//发现前面有//
break;
}
}
if(flag==true) continue;
lSp=i+2;
for(;lSp<str.GetLength()-1&&(!(str.GetAt(lSp)=='*'&&str.GetAt(lSp+1)=='/'));){
lSp++;
if(lSp>=str.GetLength()-1) break;
}
if(lSp>=str.GetLength()-1) {
MessageBox(NULL,"找到未匹配的/*","语法错误",MB_OK|MB_ICONHAND);
if(m_pctrEdit)
{
this->m_pctrEdit->SetSel(i,i+2);
this->m_pctrEdit->SetFocus();
}
return false;
}
str.Delete(i,lSp+2-i);
for(j=i;j<lSp+2;j++){
str.Insert(i,' ');
}
}
}//删除"/*...............*/"形式的注释),代之以同样数目的空白符
//删除 //
for(i=0;i<str.GetLength()-1;i++){
if(str.GetAt(i)=='/'&&str.GetAt(i+1)=='/'){
lSp=i;//i指向第一个/
for(;!(str.GetAt(lSp)=='\r'&&str.GetAt(lSp+1)=='\n')&&lSp<str.GetLength()-1;){
lSp++;
if(lSp>=str.GetLength()-1) break;
}
if(lSp>=str.GetLength()-1){//lSp指向文末最后一个字符
str.Delete(i,lSp-i+1);//删除到文末
for(j=i;j<lSp+1;j++){
str.Insert(i,' ');//插入同样多的空格
}
}
else {
str.Delete(i,lSp-i);//删除到\r前一个字符
for(j=i;j<lSp;j++){
str.Insert(i,' ');//插入同样多的空格,直到回车键之前(\r)
}
}
}
}//删除"//------------"型),代之以同样数目的空白符
for(i=0;i<str.GetLength();i++){
if(str.GetAt(i)=='\t'){
str.Delete(i,1);
str.Insert(i,' ');
}
}//删除tab字符,并代之以空白符
//MessageBox(NULL,this->m_sOriginalResource,"",0);
//
//--------------------------------------------------------------------
//TRACE("@@@@@@@@@@@@@@@@@@@@@@@@@2预处理后内部源程序长度为:%d\n",this->m_sBuffRes.GetLength());
//TRACE("@@@@@@@@@@@@@@@@@@@@@@@@@2预处理后用户源程序长度为:%d\n",this->m_sOriginalResource.GetLength());
//--------------------------------------------------------------------
return true;
}
bool CWordAnalyse::OnInitWordAnalyse()
{
this->m_sBuffRes=this->m_sOriginalResource;
if(!this->Fortransact(this->m_sBuffRes))return false;//预处理后,m_sBuffRes将发生变化
// MessageBox(NULL,this->m_sBuffRes,"",0);
// TRACE("**********************\n");
this->m_ptStr=this->m_sBuffRes.GetBuffer(0);
VERIFY(m_ptStr!=NULL);
return true;
}
void CWordAnalyse::Retract()
{
VERIFY(this->m_ptStr!=NULL);
if(IsDBCSLeadByteEx(0,*this->m_sChar.GetBuffer(0))){
this->m_piPointer-=2;
}
else{
this->m_piPointer--;
}
this->m_ptStr=CharPrev(this->m_sBuffRes.GetBuffer(0),this->m_ptStr);
VERIFY(this->m_ptStr!=NULL);
VERIFY(this->m_piPointer>=0);
this->m_sChar.Empty();
}
void CWordAnalyse::assistant()
{
if(!this->OnInitWordAnalyse()) return;
//*/
WordType result=$ILLEGALWORD;
CString temp;
while(result!=$ENDOFFILE)
{
result=this->Getsym().WdTp;
switch(result)
{
case $ENDOFFILE://文件尾
MessageBox(NULL,"文件尾","",0);
break;
case $SCONST://字符串型常量
MessageBox(NULL,"字符串","",0);
break;
case $ILLEGALWORD:
MessageBox(NULL,"非法","",0);
break;
case $IF:
temp.Format("识别出了基本字if,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $ELSE:
temp.Format("识别出了基本字else,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $WHILE:
temp.Format("识别出了基本字while,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $SIGN:
temp.Format("识别出了基本字标识符,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $PLUS:
temp.Format("识别出了基本字+,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $MINUS:
temp.Format("识别出了基本字-,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $STAR:
temp.Format("识别出了基本字*,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $DIVIDE:
temp.Format("识别出了基本字/,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $AND:
temp.Format("识别出了基本字&,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $OR:
temp.Format("识别出了基本字|,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $LOW:
temp.Format("识别出了基本字<,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $LARGE:
temp.Format("识别出了基本字>,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $EQU:
temp.Format("识别出了基本字=,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $NOT:
temp.Format("识别出了基本字!,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $LPAR:
temp.Format("识别出了基本字(,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $RPAR:
temp.Format("识别出了基本字),种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $LBP:
temp.Format("识别出了基本字{,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $RBP:
temp.Format("识别出了基本字},种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $DOTCO:
temp.Format("识别出了基本字;,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $COMMA:
temp.Format("识别出了基本字,,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $DOT:
temp.Format("识别出了基本字.,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $FCONST:
temp.Format("识别出了基本字 实常数,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $ICONST:
temp.Format("识别出了基本字 整常数,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $DAND:
temp.Format("识别出了基本字 &&,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $DOR:
temp.Format("识别出了基本字 ||,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $LOEQ:
temp.Format("识别出了基本字 <=,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $LAEQ:
temp.Format("识别出了基本字 >=,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $DEQ:
temp.Format("识别出了基本字 ==,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
case $NEQ:
temp.Format("识别出了基本字 !=,种别码:%d",result);
MessageBox(NULL,temp,"",0);
break;
default:
MessageBox(NULL,"找到了该基本字,但暂时无法识别","",0);
}
}
}
CTwoDim CWordAnalyse::Getsym()
{
bool isNotEnd=true;//记录getchar()的执行情况
WordType WType;//纪录种别编码
CTwoDim TDim;//构造函数已将之初始化了
this->m_sWord.Empty();//每次词法分析前,m_sWord必须清空
isNotEnd=this->GetChar();
if(isNotEnd)isNotEnd=this->GetNBC();
if(isNotEnd){
this->m_pPtWd.pFst_word=CharPrev(this->m_sBuffRes.GetBuffer(0),this->m_ptStr);//字头指针
if(IsDBCSLeadByteEx(0,*m_pPtWd.pFst_word)){
this->m_pPtWd.piFst_word=this->m_piPointer-2;//字头指针
}
else{
this->m_pPtWd.piFst_word=this->m_piPointer-1;//字头指针
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -