C++实现词法分析器(学习)

版权声明:转载注明出处 https://blog.csdn.net/he__yuan/article/details/79972119

参考网上程序,我把生成的数据存在文件里,能分析C语言的源程序

#include<fstream>  
#include <iostream>  
#include <stdlib.h>  
#include <stdio.h>  
using namespace std;  
string KEYWORD[17]={"if","else","void","return","while","struct","for","do","int", //关键字  
                    "char","double","float","case","sizeof","break","swicth","default"};  
char SEPARATER[10]={';',',','{','}','[',']','(',')','"','#'};    //分隔符  
char OPERATOR[11]={'+','-','*','/','>','<','=','!','%',':','?'};     //运算符  
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<17;i++){  
        if(KEYWORD[i]==word){  
            return true;  
        }  
    }  
    return false;  
}  
/**判断是否为分隔符**/  
bool IsSeparater(char ch){  
    for(int i=0;i<10;i++){  
        if(SEPARATER[i]==ch){  
            return true;  
        }  
    }  
    return false;  
}  

/**判断是否为运算符**/  
bool IsOperator(char ch){  
    for(int i=0;i<11;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 <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){  
    ofstream fout("out.txt");
    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))
            {  
                printf("%3d    ",value(KEYWORD,17,arr));  
                cout<<arr<<"  关键字"<<endl;  
                fout<<"<"<<value(KEYWORD,17,arr)<<" "<<arr<<"   "<<"关键字>\n";
            }  
            else  
            {  
                printf("%3d    ",IDENTIFIER);  
                cout<<arr<<"  标识符"<<endl;  
                fout<<"<"<<IDENTIFIER<<"    "<<arr<<"   "<<"标识符>\n";
            }  
        }  
        else if(IsDigit(ch))
        {           //判断是否为数字  
            while(IsDigit(ch)||(ch=='.'&&IsDigit(fgetc(fpin))))
            {  
                arr += ch;  
                ch=fgetc(fpin);  
            }  
            fseek(fpin,-1L,SEEK_CUR);  
            printf("%3d    ",CONSTANT);  
            cout<<arr<<"  整形数"<<endl; 
            fout<<"<"<<CONSTANT<<"  "<<arr<<"   "<<"整形数>\n"; 
        }  
        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);  
            printf("%3d    ",CONSTANT);  
            cout<<arr<<"  标识符"<<endl;  
            fout<<"<"<<CONSTANT<<"  "<<arr<<"   "<<"标识符>\n"; 
        }  
        else switch(ch)
        {  
        case '+':  
        case '-':  
        case '*':  
        case '/':  
        case '>':  
        case '<':  
        case '=':  
        case '!':  
        case ':':  
        case '?':  
        case '%':  
            {  
                arr += ch;  
                printf("%3d    ",value(OPERATOR,11,*arr.data()));  
                cout<<arr<<"  运算符"<<endl;  
                fout<<"<"<<value(OPERATOR,11,*arr.data())<<"    "<<arr<<"   "<<"运算符>\n"; 
                break;  
            }  
        case ';':  
        case ',':  
        case '(':  
        case ')':  
        case '[':  
        case ']':  
        case '{':  
        case '}':  
        case '"':  
        case '#':  
            {  
              arr += ch;  
              printf("%3d    ",value(SEPARATER,10,*arr.data()));  
              cout<<arr<<"  分隔符"<<endl;  
             fout<<"<"<<value(SEPARATER,10,*arr.data())<<"  "<<arr<<"   "<<"分隔符>\n"; 
              break;  
            }  
        default :
        {
             arr += ch;  
        cout<<"\""<<arr<<"\":无法识别的字符!\n"<<endl; 
        fout<<"<无法识别的字符!>\n"; 
        } 
        }  

    }  
  fout.close();
}  
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;  
    analyse(fpin);  
    return 0;  
}  
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页