-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.hpp
More file actions
90 lines (82 loc) · 2.28 KB
/
table.hpp
File metadata and controls
90 lines (82 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#pragma once
#include <vector>
#include <string>
#include <iostream>
#include <fstream>
#include "Compiler.hpp"
#include "Scanner.hpp"
using namespace std;
extern bool tableswitch; // 是否打印符号表
extern ofstream fas; // 输出文件流
// 错误报告函数声明
void reportError(int errCode);
// 符号类型枚举
enum class Objekt
{
constant, // 常量,应该不会用到
string,
variable,
function,
int_pointer,
string_pointer
};
class Table
{
public: // 符号表项结构体
struct Item
{
std::string name; // 名字
Objekt kind; // 类型
string str_val; // 字符串值 (仅当 kind 为 string 时使用)
int value = 0; // 值 (仅当 kind 为 constant 时使用)
int level = 0; // 层级
int adr = 0; // 地址
int size = 0; // 大小 (过程)
};
int tx = 0; // 当前符号表项指针
Scanner &lex; //对词法分析器的引用
private:
std::vector<Item> table; // 符号表存储
public:
/**
* 构造函数,初始化符号表容量
*/
Table(Scanner &lex);
/**
* 获得符号表某一项的内容
*
* @param i 符号表中的位置
* @return 符号表第 i 项的内容
*/
Item &get(int i);
/**
* 把符号添加到符号表中
*
* @param k 符号类型:constant, variable, procedure
* @param lev 名字所在的层次
* @param dx 当前应分配的变量的相对地址
* @param name 符号名称
* @param str_value 字符串值(仅用于字符串类型)
*/
void enter(Objekt k, int lev, int dx, const std::string &name = "", const std::string &str_value = "");
/**
* 打印符号表内容
*
* @param start 当前作用域符号表区间的左端
*/
void debugTable(int start);
/**
* 在符号表中查找名字的位置
*
* @param idt 要查找的名字
* @return 如果找到则返回名字项的下标,否则返回-1
*/
int position(const std::string &idt);
/**
* 查找符号的类型
*
* @param idt 要查找的名字
* @return 如果找到则返回符号的类型,否则返回Objekt::variable(默认)
*/
Objekt lookup(const std::string &idt);
};