目录
ConstantAndVariable.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