编译原理词法分析

来源:互联网 发布:淘宝的卖家中心在哪里 编辑:程序博客网 时间:2024/05/21 16:23

1.题目描述

这里写图片描述
这里写图片描述

2.代码实现

(1)注意标识符和无符号整数的重复问题,本人采用map解决。
(2)cin >> ch自动忽略空白字符。

#include <iostream>#include <vector>#include <string>#include <map>using namespace std;struct pairs {    int category;    int rank;    pairs(int c, int r) : category(c), rank(r) {}};int isBoundaries(char ch) {    switch (ch) {    case '(':        return 0;    case ')':        return 1;    case '{':        return 2;    case '}':        return 3;    case ',':        return 4;    case ';':        return 5;    }    return -1;}int isOperators(char ch1) {    if (ch1 == '+') {        return 0;    }    if (ch1 == '*') {        return 1;    }    if (ch1 == '=')        return 2;    return -1;}int isKeywords(string str) {    if (str == "int") return 0;    if (str == "main") return 1;    if (str == "return") return 2;    return -1;}vector<pairs> result;map<string,int> uInts;map<string,int> identifiers;void judgeStrs(string str) {    if (str == "") return;    int r = 0;    if (isdigit((int)(str[0]))) {        r = uInts.size();        auto it = uInts.find(str);        if (it == uInts.end())            uInts.insert(pair<string, int>(str, r));        else            r = it->second;        pairs p(5, r);        result.push_back(p);    }    else {        r = isKeywords(str);        //  identifier        if (r == -1) {            r = identifiers.size();            auto it = identifiers.find(str);            if (it == identifiers.end())                identifiers.insert(pair<string, int>(str, r));            else                r = it->second;            pairs p(4, r);            result.push_back(p);        }        else {            pairs p(1, r);            result.push_back(p);        }    }}void printResult() {    int n = result.size();    for (int i = 0; i < n; ++i) {        cout << "<" << result[i].category << "," << result[i].rank << ">";    }    cout << endl;    cout << "identifieres:";    for (auto it = identifiers.begin(); it != identifiers.end(); ++it) {        cout << it->first << " ";    }    cout << endl;    cout << "Unsigned_integer:";    for (auto it = uInts.begin(); it != uInts.end(); ++it) {        cout << it->first << " ";    }    cout << endl;}int main() {    string tempStr = "";    char ch;    int preOpe = 0;    int tempR = 0;    while (cin.get(ch) && (ch != '#')) {        if (ch == ' ' || ch == '\n' || ch == '\t') {            judgeStrs(tempStr);            tempStr = "";            continue;        }        //  判断是不是操作符        if (isOperators(ch) != -1) {            // 判断前面的tempStr            judgeStrs(tempStr);            // 判断操作符            if (preOpe == 0) {                pairs temp(2, isOperators(ch));                result.push_back(temp);                preOpe = 1;            }            else {                pairs t = result.back();                result.pop_back();                tempStr = "";                if (t.rank == 0) {                    tempStr += '+';                    tempR = 3;                }                if (t.rank == 1) {                    tempStr += '*';                    tempR = 4;                }                tempStr += '=';                pairs newOpe(t.category, tempR);                result.push_back(newOpe);                preOpe = 0;            }            tempStr = "";        }        else if (isBoundaries(ch) != -1) {            // 先判断tempStr            judgeStrs(tempStr);            // 判断分界符            pairs bound(3, isBoundaries(ch));            result.push_back(bound);            tempStr = "";        }        else {            preOpe = 0;            tempStr += ch;        }    }    printResult();//  system("pause");    return 0;}

3.小结

本题难度不太,分清楚各种类别的字符串并进行判断即可。

原创粉丝点击