⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 编译原理词法分析源码
💻 CPP
字号:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void GetWord(char ch,ifstream & fin);
int SearchKey(const char * word,struct key tab[],int n);
int SearchSymbol(const char * c,struct symbol tab[],int n);
void SaveTo(string content, string type, int value);

//用到的结构数组:
struct key
{
    //关键字表
    string word;
    int value;
}
keytab[] =
{
    {"auto", 0},
    {"break", 1},
    {"case", 2},
    {"char", 3},
    {"const", 4},
    {"continue", 5},
    {"default", 6},
    {"do", 7},
    {"double", 8},
    {"else", 9},
    {"enum", 10},
    {"extern", 11},
    {"float", 12},
    {"for", 13},
    {"goto", 14},
    {"if", 15},
    {"int", 16},
    {"long", 17},
    {"register", 18},
    {"return", 19},
    {"short", 20},
    {"signed", 21},
    {"sizeof", 22},
    {"static", 23},
    {"struct", 24},
    {"switch", 25},
    {"typedef", 26},
    {"union", 27},
    {"unsigned", 28},
    {"void", 29},
    {"volatile", 30},
    {"while", 31}
};

//****
struct symbol
{
    //符号表
    string sym;
    int value;
}
symboltab[] =
{
    {"(", 0},
    {")", 1},
    {"*", 2},
    {"+", 3},
    {",", 4},
    {"-", 5},
    {"/", 6},
    {";", 7},
    {"<", 8},
    {"<=", 9},
    {"<>", 10},
    {"=", 11},
    {"==", 12},
    {">", 13},
    {">=", 14},
    {"[", 15},
    {"]", 16},
    {"{", 17},
    {"}", 18}
};

struct Table
{
    //符号表
    string content;
    string type;
    int value;
};


enum{NKEYS = 32,
     NSYMBOL = 19,
     NUM = 200
    };

string type[] = { "id", "num", "punct", "relop", "addop", "mulop", "keyword"}; //词法分析中的类型   依次为标识符,数字,符号
string word = "";
string typesave;
int place = 0;
int tokenindex = 0;
struct Table * table = new struct Table[NUM];

void SaveTokens()
{
    string filename;
    cout <<"请输入源文件地址: ";
    //cin >> filename;
    cin >> filename;

    ifstream fin(filename.c_str());    //输入流
    ofstream fout("output.txt");   //输出流

    char ch;

    if (!fin || !fout)
    {
        //如果输入的文件路径不对或文件不存在
        cout << "文件操作失败!" << endl;
        return;
    }
    fout << "token序列(每个token形式为二元组):"<<endl;
    cout << "token序列(每个token形式为二元组):"<<endl;
    while (fin.get(ch))
    {
        ++place;
        if ( isspace(ch) )        // 如果是空白字符
        {
            if (ch == '\n' || ch == '\r')
            {
                ++place;
            }
            continue;
        }

        else if ( isalpha(ch) )
        {
            //如果是字母
            typesave = type[0];
            GetWord(ch, fin);

            table[tokenindex].content = word;
            table[tokenindex].type = typesave;
            table[tokenindex].value = SearchKey( word.c_str(), keytab, NKEYS);
            tokenindex++;
            if ( SearchKey( word.c_str(), keytab, NKEYS) >= 0)   //是关键字
            {
                cout << word << " ";
                fout << word << " ";
            }
            else          //不是关键字
            {
                cout << typesave << " ";
                fout << typesave << " ";
            }
            word = "";
        }
        else if ( isdigit(ch) )
        {
            //如果是数字
            typesave = type[1];
            GetWord(ch, fin);

            table[tokenindex].content = word;
            table[tokenindex].type = typesave;
            table[tokenindex].value = 0;
            tokenindex++;
            cout << typesave << " ";
            fout << typesave << " ";
            word = "";
        }
        else if ( ispunct(ch) )
        {
            //如果是符号
            typesave = type[2];
            GetWord(ch, fin);

            table[tokenindex].content = word;
            table[tokenindex].type = typesave;
            table[tokenindex].value = SearchSymbol( word.c_str(), symboltab, NKEYS);
            tokenindex++;
            if (word == "+" || word == "-")
            {
                cout << type[4] << " ";
                fout << type[4] << " ";
            }
            else if (word == "*" || word == "/")
            {
                cout << type[5] << " ";
                fout << type[5] << " ";
            }
            else if (word == "==" || word == "<>" || word == "<=" || word == ">=")
            {
                cout << type[3] << " ";
                fout << type[3] << " ";
            }
            else
            {
                cout << word << " ";   //不是要求替换的符号
                fout << word << " ";
            }
            word = "";
        }
        else
        {
            //出错处理
            cout << "error: unknown identifier!";
        }
    }
    cout << endl;
/*
    cout << "符号表:" <<endl;
    fout << "符号表:" <<endl;
    for (int i = 0; i<tokenindex; i++)
    {

        cout << "(" <<table[i].content<<","<<table[i].type<<","
        <<table[i].value<<")"<<endl;
        fout << "(" <<table[i].content<<","<<table[i].type<<","
        <<table[i].value<<")"<<endl;
    }
    fin.close();
*/
    fout.flush();
    fout.close();
}

//**********************************************
void GetWord(char ch,ifstream & fin)
{
    word += ch;
    while (true)
    {
        fin.get(ch);
        ++place;
        if (typesave == "id")
        {
            if (!isalpha(ch))
            {
                break;
            }
            else
            {
                word += ch;
            }
        }
        else if (typesave == "num")
        {
            if (!isdigit(ch))
            {
                break;
            }
            else
            {
                word += ch;
            }
        }
        else if (typesave == "punct")
        {
            if (!ispunct(ch))
            {
                break;
            }
            else
            {
                word += ch;
            }
        }
    }
    --place;
    fin.seekg(place); //往回读一个
}
//**********************************************
int SearchKey(const char * word, struct key tab[], int n)
{
    int cond;
    int low,high,mid;
    low = 0;
    high = n -1;
    while (low <= high)
    {
        mid = (low + high) / 2;
        if ( ( cond = strcmp(word, tab[mid].word.c_str()) ) < 0 )
            high = mid - 1;
        else if ( cond > 0 )
            low = mid + 1;
        else
            return mid;
    }
    return -1;
}
//**********************************************
int SearchSymbol(const char * word, struct symbol tab[], int n)
{
    for (int i=0; i<NSYMBOL; i++)
    {
        if (strcmp(word, tab[i].sym.c_str())==0)
        {
            return  tab[i].value;
        }
    }
    return -1;
}
//**********************************************
void SaveTo(string content, string type, int value)
{
    table[tokenindex].content = content;
    table[tokenindex].type = type;
    table[tokenindex].value = value;
    tokenindex++;
}

int main()
{
    SaveTokens();
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -