-
Notifications
You must be signed in to change notification settings - Fork 0
/
Item.h
134 lines (125 loc) · 3.58 KB
/
Item.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef Item_h
#define Item_h
#include "Symbol.h"
#include <vector>
#define TERMINAL 0
#define NONTERMINAL 1
#define NOTHING -1
using namespace std;
/*
* 项目/产生式
* 上下文无关文法:左边只有一个非终结符,右边是终结符或者非终结符的集合
* 形如:A -> .alpha beta
*/
class Item{
private:
Symbol leftSide;
vector<Symbol> rightSide;
Symbol ahead;
int dotPos; // position of dot [0,n] , n为rightSide的长度
int iid; // id为项目在某一个项目集中的编号
public:
Item(Symbol left, vector<Symbol> right, Symbol lookAhead, int dot_postion=0, int item_id=0){
leftSide = left;
rightSide = right;
ahead = lookAhead;
dotPos = dot_postion;
iid = item_id;
}
Item(){
dotPos = -1;
}
Symbol getLeftSide() const { return leftSide; }
vector<Symbol> getRightSide() const { return rightSide; }
Symbol getAhead() const { return ahead; }
int getDotPos() const { return dotPos; }
void setLeftSide(Symbol left){
leftSide = left;
}
void setRightSide(vector<Symbol> right){
rightSide = right;
}
void rightAppend(Symbol s){
rightSide.push_back(s);
}
void setAhead(Symbol s){
ahead = s;
}
void setDotPos(int pos){
dotPos = pos;
}
void setID(int item_id){
iid = item_id;
}
Symbol symbolAfterDot() const {
if(dotPos >= rightSide.size()) return Symbol(); // 返回非法字符
return rightSide[dotPos];
}
vector<Symbol> symbolsAfterTheSymbolAfterDot() const {
vector<Symbol> res;
res.clear();
for(int i=dotPos+1;i<rightSide.size();i++){
res.push_back(rightSide[i]);
}
return res;
}
// 判断是否为规约项目
bool isReductionItem(){
int pos = dotPos;
// 空集忽略
Symbol null = Symbol(TERMINAL, -1, "null");
while(rightSide[pos] == null){
pos++;
}
return pos == rightSide.size();
}
Item nextItem(){
// if(isReductionItem())
// assert("无法后移\n");
Item next = Item(leftSide, rightSide, ahead, dotPos+1);
return next;
}
// 只看产生式,不看ahead和id
bool equal(Item& item) const{
if(leftSide != item.getLeftSide() || rightSide.size() != item.getRightSide().size())
return false;
for(int i=0;i<rightSide.size();i++){
if(rightSide[i] != item.getRightSide()[i])
return false;
}
return true;
}
bool operator==(const Item& item) const {
// iid 只是在项目集中的编号,并不用比较
if(leftSide==item.getLeftSide()
&& ahead==item.getAhead()
&& dotPos==item.getDotPos()){
if(rightSide.size() != item.getRightSide().size())
return false;
for(int i=0;i<rightSide.size();i++){
if(rightSide[i] != item.getRightSide()[i])
return false;
}
return true;
}
return false;
}
friend ostream& operator<<(ostream& os, const Item& it){
os << it.getLeftSide();
os << " -> ";
vector<Symbol> v = it.getRightSide();
for(int i=0;i<v.size();i++){
if(it.getDotPos() == i){
os << " . ";
}
os << v[i];
}
if(it.getDotPos() == v.size()){
os << " . ";
}
os << ", ";
os << it.getAhead();
return os;
}
};
#endif