C++词法分析器

最好是自己写的
2025-02-25 06:41:17
推荐回答(1个)
回答1:

#include
#include
#include
#include
#include
#include
#include
using namespace std;
vector keywords;
vector > symble;
vector > bound;
char ch;
int isKeyWord(const string& s,int& n)
{
    if(keywords.empty())
    {
        return 0;
    }
    for(vector::iterator it = keywords.begin();
        it != keywords.end();it++,n++)
    {
       if(s == *it)
       {
            return 1;
       }
    }
    return -1;
}
int isSymble(const string& s, string& result)
{
    for(vector >::iterator it = symble.begin();
        it != symble.end();it++)
    {
       if(s == (*it).first)
       {
            result = (*it).second;
            return 1;
       }
    }
    return 0;
}
int isBound(const string& s, string& result)
{
    for(vector >::iterator it = bound.begin();
        it != bound.end();it++)
    {
       if(s == (*it).first)
       {
            result = (*it).second;
            return 1;
       }
    }
    return 0;
}
void analyse(FILE *fp)
{
    string temp = "";
    string str = "";
    string result = "";
    int id = 0;
    while((ch = fgetc(fp)) != EOF)
    {
        temp = "";
        str = ch;
        id = 0;
        if(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
        {
            while(ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r')
            {
                ch = fgetc(fp);
            }
            fseek(fp,-1L,SEEK_CUR);
        }
        else if(isalpha(ch))
        {
            while(isalpha(ch) || isdigit(ch))
            {
                temp = temp + ch;
                ch = fgetc(fp);
            }
            fseek(fp,-1L,SEEK_CUR);
            if(isKeyWord(temp,id) == 1)
            {
                cout << temp << "\t$关键字 , " << id << endl;
            }
            else
            {
                cout << temp << "\t$标识符" << endl;
            }
        }
        else if(isdigit(ch))
        {
            while(isdigit(ch))
            {
                temp = temp + ch;
                ch = fgetc(fp);
            }
            fseek(fp,-1L,SEEK_CUR);
            cout << temp << "\t$整型" << endl;
        }
        else if(isSymble(str,result))
        {
            cout << ch << "\t$" << "运算符" << result << endl;
            /*case '+':cout << ch << "\t$ADD" << endl;break;
            case '-':cout << ch << "\t$SUBTRACT" << endl;break;
            case '*':cout << ch << "\t$MULTIPLY" << endl;break;
            case '/' :cout << ch << "\t$DIVIDE" << endl;break;
            case '=' :cout << ch << "\t$ASSIGN" << endl;break;
            case '(' :cout << ch << "\t$LPAR" << endl;break;
            case ')' :cout << ch << "\t$RPAR" << endl;break;
            case '[' :cout << ch << "\t$LSB" << endl;break;
            case ']' :cout << ch << "\t$RSB" << endl;break;
            case ';' :cout << ch << "\t$SEMICOLON" << endl;break;
            case '.' :cout << ch << "\t$DOT" << endl;break;
            case ',' :cout << ch << "\t$COMMA" << endl;break;
            case '{' :cout << ch << "\t$LBRACE" << endl; break;
            case '}' :cout << ch << "\t$RBRACE" << endl;break;
            default :cout << ch << "\t$UnKnow" << endl;*/
        }
        else if(isBound(str,result))
        {
            cout << ch << "\t$" << "界符" << result << endl;
        }
        else
        {
            cout << ch << "\t$未知" << endl;
        }
    }
}
int main()
{
    string line, symbelLine,boundLine,word,filename,symbleName,symbleId,boundName,boundId;
    cout << "请输入要解析的文件名" << endl;
    cin >> filename;
    cout << "请输入该编程语言的关键字" << endl;
    while(getline(cin,line))
    {
        istringstream stream(line);
        while(stream >> word)
        {
            keywords.push_back(word);
        }
    }
    cin.clear();
    cout << "请输入该编程语言的运算符,格式为 符号名称  符号" << endl;
    while(getline(cin,symbelLine))
    {
        istringstream stream(symbelLine);
        while(stream >> symbleName >> symbleId)
        {
            symble.push_back(pair(symbleName,symbleId));
        }   
    }
    cin.clear();
    cout << "请输入该编程语言的界符,格式为 符号名称  符号" << endl;
    while(getline(cin,boundLine))
    {
        istringstream stream(boundLine);
        while(stream >> boundName >> boundId)
        {
            bound.push_back(pair(boundName,boundId));
        }   
    }
    FILE *fp;
    fp = fopen(filename.c_str(),"r");
    if(!fp)
    {
        cout << "文件操作错误,请检查后重试" << endl;
        return -1;
    }
    analyse(fp);
    fclose(fp);
    cout << "按任意键退出" << endl;
    getchar();
    return 0;
}

http://blog.csdn.net/my_offer/article/details/7344375