编译原理实验:错误处理程序(2)

目录

首先是KV.h,用来保存键值对

然后是Error.h,用来保存错误信息

 ConstantAndVariable.h,用来保存变量常量信息

Function.h 用来保存函数信息


工具类函数:

首先是KV.h,用来保存键值对

#ifndef __KV_H__
#define __KV_H__
#include <algorithm>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <map>
#include <stdlib.h>
#include <string.h>
#include <vector>
using namespace std;
static int CUR_ID = 0;
//键值对
class KV {
public:
    int id = 0;
    string category_code="<空>";
    string value="\0";
    int lineindex = 0;
    vector<KV*> children;
    //构造函数
    KV() { }
    KV(string category_code, string value, int lineindex)
    {
        this->id = CUR_ID++;
        this->category_code = category_code;
        this->value = value;
        this->lineindex = lineindex;
    }

    KV(string category_code)
    {
        this->id = CUR_ID++;
        this->category_code = category_code;
    }

    KV(string category_code,int lineindex)
    {
        this->id = CUR_ID++;
        this->category_code = category_code;
        this->lineindex= lineindex;
    }
    //构造函数
    ~KV() { }

    // void set_KV(KV other)
    // {
    //     this->value = other-value;
    //     this->lineindex = other.lineindex;
    //     this->category_code = other->category_code;
    // }
    // 添加孩子
    void addchildren(KV* child)
    {
        children.push_back(child);
    }

    // 弹出孩子
    void popchildren()
    {
        children.pop_back();
    }

    bool isleaf()
    {
        if(children.size() == 0)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    vector<KV*> get_children()
    {
        return children;
    }

    string get_category_code()
    {
        return category_code;
    }

    int get_lineindex()
    {
        return lineindex;
    }

    string get_value()
    {
        return value;
    }
    int get_id()
    {
        return id;
    }

    

    //重载运算符
    // KV operator=(const KV& other) {
    //         KV kv;
    //         kv.category_code = other.category_code;
    //         kv.lineindex= other.lineindex;
    //         kv.value=other.value;
    //         return KV(other.category_code,other.value,other.lineindex);
    // }
};

#endif

然后是Error.h,用来保存错误信息

#ifndef __ERROR_H__
#define __ERROR_H__
#include <iostream>
#include <string>
using namespace std;

class Error {
private:
public:
    string error_type = "\0";
    string error_lineindex = "\0";

    Error(string error_type, string error_lineindex)
    {
        this->error_type = error_type;
        this->error_lineindex = error_lineindex;
    }
    ~Error() { }

    bool operator==(const Error& other)
    {
        if (this->error_type == other.error_type && this->error_lineindex == other.error_lineindex) {
            return true;
        } else {
            return false;
        }
    }

    bool operator<(const Error& other)
    {
        if (atoi(this->error_lineindex.c_str()) < atoi(other.error_lineindex.c_str())) {
            return true;
        } else {
            return false;
        }
    }
};
#endif

 ConstantAndVariable.h,用来保存变量常量信息

#ifndef __CONSTANTANDVARIABLE_H__
#define __CONSTANTANDVARIABLE_H__
#include <KV.h>
#include <algorithm>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <map>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

class ConstantAndVariable{
    private:
    public:
        //变量名
        string name;
        //变量的类型
        string type;
        //作用域
        string scope;
        //是否为常量
        bool isConstant=false;
        //维度
        int dim=0;
        // 有无初始化
        bool initialization=false;
        
        ConstantAndVariable() {}

        ConstantAndVariable(string name, string type,bool isConstant,int dim ,bool initialization ,string scope) {
            this->name = name;
            this->type = type;
            this->isConstant = isConstant;
            this->dim = dim;
            this->initialization = initialization;
            this->scope = scope;
        }
        ~ConstantAndVariable(){}


};

#endif

Function.h 用来保存函数信息

#ifndef __FUNCTION_H__
#define __FUNCTION_H__
#include <KV.h>
#include <algorithm>
#include <ctype.h>
#include <fstream>
#include <iostream>
#include <map>
#include <string.h>
#include <utility>
#include <vector>
using namespace std;

class Function{
    private:
    public:
        //变量名
        string name;
        //变量的类型
        string type;
        //参数个数
        int parameter_number=0;
        // 参数类型
        vector<string>parameter_types;
        
        Function() {}

        Function(string name, string type) {
            this->name = name;
            this->type = type;
        }
        ~Function(){}

        void add_parameter(string parameter_type){
            parameter_types.push_back(parameter_type);
            parameter_number++;
        }


};

#endif

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值