-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbol.h
70 lines (63 loc) · 1.69 KB
/
Symbol.h
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
#ifndef Symbol_h
#define Symbol_h
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <string>
#include "assert.h"
using namespace std;
#define TERMINAL 0
#define NONTERMINAL 1
#define INVALID -1
/*
* 符号类定义
*/
class Symbol{
private:
int type; // terminal or nonterminal or invalid(包括空集)
int sid; // define by yourself(特别的,-1留给空集,0留给$)
string represent; // 字符内容表示
public:
Symbol(int symbol_type, int symbol_id, string symbol_content=""){
type = symbol_type;
sid = symbol_id;
represent = symbol_content;
}
// 默认构造非法字符
Symbol(){
type = INVALID;
sid = -1;
represent = "";
}
int getType() const {return type;}
int getID() const {return sid;}
string getContent() const { return represent;}
void setType(int symbol_type){
type = symbol_type;
}
void setID(int symbol_id){
sid = symbol_id;
}
void setContent(string content){
represent = content;
}
// 对于Symbol会存在查找,排序等操作,因此需要操作符重载
bool operator==(const Symbol &s) const {
if(type == s.getType() && sid == s.getID()) return true;
return false;
}
bool operator!=(const Symbol &s) const {
if(type != s.getType() || sid != s.getID()) return true;
return false;
}
bool operator<(const Symbol &s) const {
if(type == s.type) return sid < s.sid;
return type < s.type;
}
friend ostream& operator<<(ostream& os, const Symbol& sym){
os<<sym.getContent();
return os;
}
};
#endif