这是大三上写的程序了,今天突然看到了,只贴出它的逻辑判断部分,有兴趣要全部源代码的可以给邮箱给我
LexicalAnalyzerLogic.h:
#pragma once
#include "stdafx.h"
#include <fstream>
#include <string>
#include <vector>
using namespace std;
typedef enum
{
UNDEFINE=0,
KEY_WORD=1, //关键字
ID, //标识符
UNSINGED_NUMBER, //无符号整数
OPERATOR, //操作数
SEPARATOR //分隔符
}Kind;
class CLexicalAnalyzerLogic
{
string m_strinput;
string m_stroutput;
std::vector<string> m_key_word_list; //保存关键字
bool Start();
bool Scan(int& i,string& str); //从i开始输入,返回结束的i,将截取的单词放到str中
void GetNB(const string& str); //去掉str中注释和多余的空白
bool LookUp(const string& str,Kind& kind); //判断单词str的类型
bool IsKeyWord(const string& str);
bool IsID(const string& str);
bool IsUnsinedNumber(const string& str);
bool IsOperator(const string& str);
bool IsSeparator(const string& str);
inline bool IsOperator(const char& ch)
{
if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='='||ch=='>'||ch=='<')
return true;
return false;
}
inline bool IsSeparator(const char& ch)
{
if(ch=='('||ch==')'||ch==','||ch==';'||ch=='{'||ch=='}')
return true;
return false;
}
inline bool IsLetterChar(const char& ch){if(ch>='A'&&ch<='Z'||ch>='a'&&ch<='z') return true; return false;}
inline bool IsNumberChar(const char& ch){if(ch>='0'&&ch<='9') return true; return false;}
public:
CLexicalAnalyzerLogic(){};
~CLexicalAnalyzerLogic(){};
bool Init(); //从文件中读出关键字列表
bool Input(const string& str);
string OutPut(){return m_stroutput;}
};
#include "stdafx.h"
#include "LexicalAnalyzerLogic.h"
bool CLexicalAnalyzerLogic::Init()
{
ifstream fin;
fin.open("KeyWord.txt");
if(fin.fail())
return false;
string tempstr;
while(!fin.eof())
{
fin>>tempstr;
m_key_word_list.push_back(tempstr);
}
return true;
}
bool CLexicalAnalyzerLogic::IsKeyWord(const string& str)
{
std::vector<string>::iterator ite=m_key_word_list.begin();
for(;ite!=m_key_word_list.end();ite++)
if(*ite==str)
return true;
return false;
}
bool CLexicalAnalyzerLogic::IsID(const string& str)
{
unsigned int i=0;
if(IsNumberChar(str[0])) return false;
do
{
if(IsLetterChar(str[i])||IsNumberChar(str[i])||str[i]=='_')
i++;
else
return false;
} while (i<str.size());
return true;
}
bool CLexicalAnalyzerLogic::IsUnsinedNumber(const string& str)
{
unsigned int i=0;
do
{
if(IsNumberChar(str[i])) i++;
else
return false;
} while (i<str.size());
return true;
}
bool CLexicalAnalyzerLogic::IsOperator(const string& str)
{
if(IsOperator(str[0]&&str[0]!=' '))
return true;
return false;
}
bool CLexicalAnalyzerLogic::IsSeparator(const string& str)
{
if(IsSeparator(str[0]))
return true;
return false;
}
bool CLexicalAnalyzerLogic::Input(const string& str)
{
m_strinput="";
GetNB(str);
if(!Start()) return false;
return true;
}
void CLexicalAnalyzerLogic::GetNB(const string& str)
{
unsigned int i=0;
char ch;
while(i<str.size())
{
//略掉注释
ch=str[i];
if(str[i]=='/'&&str[i+1]=='*')
{
do
{
i++;
} while (!(str[i]=='*'&&str[i+1]=='/'));
if(str[i]=='*'&&str[i+1]=='/')
i+=2;
}
else
if(str[i]=='/'&&str[i+1]=='/')
{
do
{
i++;
} while (str[i]!='\n');
}
else//略掉多余的空格和回车,把回车变为空格
if(str[i]==' '||str[i]=='\n'||str[i]=='\r'||str[i]=='\t')
{
if(!IsSeparator(str[i-1])&&!IsOperator(str[i-1])) //保证分割符和空格,操作符和空格在两个单词之间只有一个
m_strinput+=" ";
i++;
while(str[i]==' '||str[i]=='\n'||str[i]=='\r') i++;
}
else
m_strinput+=str[i++];
ch=str[i];
}
}
bool CLexicalAnalyzerLogic::Start()
{
int i=0;
string str;
CString strtemp;
Kind kind;
while(i<m_strinput.size())
{
Scan(i,str);
if(str==" ") i--; //处理字符是第一个空格的情况
LookUp(str,kind);
if(kind!=UNDEFINE)
{
strtemp.Format("( %d, %s )\r\n",int(kind),str.c_str());
m_stroutput+=strtemp;
}
char ch=m_strinput[i];
if(IsSeparator(m_strinput[i]))
{
strtemp.Format("( %d, %c )\r\n",5,m_strinput[i]);
m_stroutput+=strtemp;
}
else
if(IsOperator(m_strinput[i]))
{
strtemp.Format("( %d, %c )\r\n",4,m_strinput[i]);
m_stroutput+=strtemp;
}
i++;
}
return true;
}
bool CLexicalAnalyzerLogic::Scan(int& i,string& str)
{
str="";
while(!IsSeparator(m_strinput[i])&&m_strinput[i]!=' '&&!IsOperator(m_strinput[i])&&i<m_strinput.size())
str+=m_strinput[i++];
if(str=="")
str=m_strinput[i++];
return true;
}
bool CLexicalAnalyzerLogic::LookUp(const string& str,Kind& kind)
{
if(IsKeyWord(str))
{
kind=KEY_WORD;
return true;
}
if(IsID(str))
{
kind=ID;
return true;
}
if(IsUnsinedNumber(str))
{
kind=UNSINGED_NUMBER;
return true;
}
if(IsOperator(str))
{
kind=OPERATOR;
return true;
}
if(IsSeparator(str))
{
kind=SEPARATOR;
return true;
}
kind=UNDEFINE;
return true;
}