#include <iostream>
#include <bits/stdC++.h>
using namespace std;
string KEYWORD[15]={"begin","end","if","then","else","not","and","or","true","false","while","do","for", //关键字
"step","until"};
char SEPARATER[8]={';',',','{','}','[',']','(',')'}; //分隔符
string OPERATOR[11]={"+","-","*","/",":=","=","<>",">",">=","<","<="};
char FILTER[4]={' ','\t','\r','\n'}; //过滤符
/**判断是否为关键字**/
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(string str){
for(int i=0;i<11;i++){
if(OPERATOR[i]==str){
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 <class T>
int value(T *a,int n,T str){
for(int i=0;i<n;i++){
if(a[i]==str) return i+1;
}
return -1;
}
/**词法分析**/
void analyse(FILE * fpin){
char ch=' ';
string arr="";
while((ch=fgetc(fpin))!=EOF){
arr="";
if(IsFilter(ch)){} //判断是否为过滤符
else if(IsLowLetter(ch)){ //判断是否为关键字
while(IsLowLetter(ch)){
arr += ch;
ch=fgetc(fpin);
}
//fseek(fpin,-1L,SEEK_CUR);
if(IsKeyword(arr)){
cout<<arr<<"\t\t关键字"<<endl;
}
else
{
cout<<arr<<"\t\t标识符"<<endl;
}
}
else if(IsDigit(ch)){ //判断是否为数字
while(IsDigit(ch)||(ch=='.'&&IsDigit(fgetc(fpin)))){
arr += ch;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR);
cout<<arr<<"\t\t整形数"<<endl;
}
else if(IsUpLetter(ch)||IsLowLetter(ch)||ch=='_'){
while(IsUpLetter(ch)||IsLowLetter(ch)||ch=='_'||IsDigit(ch)){
arr += ch;
ch=fgetc(fpin);
}
fseek(fpin,-1L,SEEK_CUR);
cout<<arr<<"\t\t标识符"<<endl;
}
else switch(ch){
case '+':
arr += ch;
cout<<arr<<"\t\t运算符"<<endl;
break;
case '-':
arr += ch;
cout<<arr<<"\t\t运算符"<<endl;
break;
case '*':
arr += ch;
cout<<arr<<"\t\t运算符"<<endl;
break;
case '/':
arr += ch;
cout<<arr<<"\t\t运算符"<<endl;
break;
case ':':
arr += ch;
ch=fgetc(fpin);
if(ch=='=')
cout<<":="<<"\t\t赋值运算符"<<endl;
else
{cout<<":"<<"\t\t无法识别的字符"<<endl;
arr += ch;
}
break;
case '=':
arr += ch;
cout<<"="<<"\t\t比较运算符"<<endl;
break;
case '<':
arr += ch;
ch=fgetc(fpin);
if(ch=='>')
{
cout<<"<>"<<"\t\t运算符"<<endl;
arr += ch;
break;
}
else if(ch=='=')
{
cout<<"<="<<"\t\t运算符"<<endl;
arr += ch;
break;
}
else
{cout<<"<"<<"\t\t运算符"<<endl;
arr += ch;
break;
}
case '>':
arr += ch;
ch=fgetc(fpin);
if(ch=='=')
{
cout<<">="<<"\t\t运算符"<<endl;
arr += ch;
break;
}
else
{cout<<">"<<"\t\t运算符"<<endl;
arr += ch;
break;
}
case ';':
case ',':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
{
arr += ch;
cout<<arr<<"\t\t分隔符"<<endl;
break;
}
default :cout<<"\""<<ch<<"\":无法识别的字符!"<<endl;
}
}
}
int main()
{
char inFile[40];
FILE *fpin;
cout<<"请输入源文件名(包括路径和后缀):";
while(true){
cin>>inFile;
if((fpin=fopen(inFile,"r"))!=NULL)
break;
else{
cout<<"文件名错误!"<<endl;
cout<<"请输入源文件名(包括路径和后缀):";
}
}
cout<<"----词法分析如下----"<<endl;
cout<<"单词 类型"<<endl;
analyse(fpin);
return 0;
}
编译原理——一个简单的词法分析程序
最新推荐文章于 2022-11-21 20:11:54 发布