本文实例为大家分享了C++实现词法分析器的具体代码,供大家参考,具体内容如下
#include
#include
#include
using namespace std;
string KEYWORD[15]={"if","else","void","return","while","then","for","do", //关键字
"int","char","double","float","case","cin","cout"};
char SEPARATER[8]={';',',','{','}','[',']','(',')'}; //分隔符
char OPERATOR[8]={'+','-','*','/','>','<','=','!'}; //运算符
char FILTER[4]={' ','t','r','n'}; //过滤符
const int IDENTIFIER=100; //标识符值
const int ConSTANT=101; //常数值
const int FILTER_VALUE=102; //过滤字符值
bool IsKeyword(string word){
for(int i=0;i<15;i++){
if(KEYWORD[i]==word){
return true;
}
}
return false;
}
bool IsSeparater(char ch){
for(int i=0;i<8;i++){
if(SEPARATER[i]==ch){
return true;
}
}
return false;
}
bool IsOperator(char ch){
for(int i=0;i<8;i++){
if(OPERATOR[i]==ch){
return true;
}
}
return false;
}
bool IsFilter(char ch){
for(int i=0;i<4;i++){
if(FILTER[i]==ch){
return true;
}
}
return false;
}
bool IsUpLetter(char ch){
if(ch>='A' && ch<='Z') return true;
return false;
}
bool IsLowLetter(char ch){
if(ch>='a' && ch<='z') return true;
return false;
}
bool IsDigit(char ch){
if(ch>='0' && ch<='9') return true;
return false;
}
template
int value(T *a,int n,T str){
for(int i=0;iinFile;
if((fpin=fopen(inFile,"r"))!=NULL)
break;
else{
cout<<"文件名错误!"<
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



