编译原理词法分析

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.小结

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值