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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
#pragma once
#include <vector>
#include <utility>
#include <sstream>
#include <unordered_map>
#include <set>
#include "grammar.hh"
#include "util.hh"
#include "types.hh"
using namespace std;
typedef pair<size_t,size_t> Span;
namespace std {
template <>
struct hash<Span>
{
size_t
operator()(Span const& k) const
{
return ((hash<size_t>()(k.first)
^ (hash<size_t>()(k.second) << 1)) >> 1);
}
};
}
namespace Parse {
void visit(vector<Span>& p,
size_t i, size_t l, size_t r, size_t x=0)
{
for (size_t s = i; s <= r-x; s++) {
for (size_t k = l; k <= r-s; k++) {
p.push_back(Span(k,k+s));
}
}
}
struct ChartItem
{
Span span;
G::Rule const* rule;
vector<Span> tails_spans;
size_t dot;
ChartItem() {}
ChartItem(G::Rule* r) : rule(r), dot(0) {}
ChartItem(G::Rule* r, Span s, size_t dot)
: rule(r), span(s), dot(dot) {}
ChartItem(ChartItem const& o)
: span(o.span),
rule(o.rule),
tails_spans(o.tails_spans),
dot(o.dot)
{
}
ostream&
repr(ostream& os) const
{
os << "ChartItem<";
os << "span=(" << span.first << "," << span.second << "), lhs=";
rule->lhs->repr(os);
os << ", dot=" << dot;
os << ", tails=" << tails_spans.size() << ", ";
os << "rule=";
rule->repr(os);
os << ">";
os << endl;
return os;
}
friend ostream&
operator<<(ostream& os, ChartItem item)
{
item.repr(os);
return os;
}
};
struct Chart
{
size_t n_;
map<Span, vector<ChartItem*> > m_;
unordered_map<string,bool> b_;
vector<ChartItem*>& at(Span s)
{
return m_[s];
}
string h(symbol_t sym, Span s)
{
ostringstream ss;
ss << sym;
ss << s.first;
ss << s.second;
return ss.str();
}
bool
has_at(symbol_t sym, Span s)
{
return b_[h(sym, s)];
}
void add(ChartItem* item, Span s)
{
if (m_.count(s) > 0)
m_[s].push_back(item);
else {
m_.insert(make_pair(s, vector<ChartItem*>{item}));
}
b_[h(item->rule->lhs->symbol(), s)] = true;
}
Chart(size_t n) : n_(n) {}
friend ostream&
operator<<(ostream& os, Chart const& chart)
{
for (map<Span, vector<ChartItem*> >::const_iterator it = chart.m_.cbegin();
it != chart.m_.cend(); it++) {
os << "(" << it->first.first << "," << it->first.second << ")" << endl;
size_t j = 0;
for (auto jt: it->second) {
os << j << " "; jt->repr(os);
j++;
}
}
return os;
}
};
bool
scan(ChartItem* item, vector<symbol_t> in, size_t limit, Chart& passive)
{
while (item->dot < item->rule->rhs.size() &&
item->rule->rhs[item->dot]->type() == G::TERMINAL) {
if (item->span.second == limit) return false;
if (item->rule->rhs[item->dot]->symbol() == in[item->span.second]) {
item->dot++;
item->span.second++;
} else {
return false;
}
}
return true;
}
void
init(vector<symbol_t> const& in, size_t n, Chart& active, Chart& passive, G::Grammar const& g)
{
for (auto rule: g.flat) {
size_t j = 0;
for (auto it: in) {
if (it == rule->rhs.front()->symbol()) {
Span span(j, j+rule->rhs.size());
passive.add(new ChartItem(rule, span, rule->rhs.size()), span);
}
j++;
}
}
}
void
parse(vector<symbol_t> const& in, size_t n, Chart& active, Chart& passive, G::Grammar const& g, size_t max_span_size)
{
vector<Span> spans;
Parse::visit(spans, 1, 0, n);
for (auto span: spans) {
size_t span_size = span.second-span.first;
cout << "Span (" << span.first << "," << span.second << ")" << endl;
for (auto it: g.start_terminal) {
ChartItem* item = new ChartItem(it, Span(span.first,span.first), 0);
if (scan(item, in, span.second, passive)
&& span.first + item->rule->rhs.size() <= span.second) {
active.add(item, span);
}
}
for (auto it: g.start_non_terminal) {
if (it->rhs.size() > span.second-span.first
|| (span_size>max_span_size)) continue;
active.add(new ChartItem(it, Span(span.first,span.first), 0), span);
}
set<symbol_t> new_symbols;
vector<ChartItem*> remaining_items;
while (true) {
if (active.at(span).empty()) break;
ChartItem* item = active.at(span).back();
active.at(span).pop_back();
while (item->rule->rhs[item->dot]->type() == G::NON_TERMINAL) {
symbol_t cur_sym = item->rule->rhs[item->dot]->symbol();
}
}
/*while (true) {
if (active.at(span).empty()) break;
ChartItem* item = active.at(span).back();
active.at(span).pop_back();
bool advanced = false;
vector<Span> spans2;
Parse::visit(spans2, 1, span.first, span.second, 1);
for (auto span2: spans2) {
if (item->rule->rhs[item->dot]->type() == G::NON_TERMINAL) {
if (passive.has_at(item->rule->rhs[item->dot]->symbol(), span2)) {
if (span2.first == item->span.second) {
ChartItem* new_item = new ChartItem(*item);
new_item->span.second = span2.second;
new_item->dot++;
new_item->tails_spans.push_back(span2);
if (scan(new_item, in, span.second, passive)) {
if (new_item->dot == new_item->rule->rhs.size()) {
if (new_item->span.first == span.first && new_item->span.second == span.second) {
new_symbols.insert(new_item->rule->lhs->symbol());
passive.add(new_item, span);
advanced = true;
}
} else {
if (new_item->span.second+(new_item->rule->rhs.size()-new_item->dot) <= span.second) {
active.add(new_item, span);
}
}
}
}
}
}
}
if (!advanced) {
remaining_items.push_back(item);
}
}
for (auto new_sym: new_symbols) {
for (auto rem_item: remaining_items) {
if (rem_item->dot != 0 ||
rem_item->rule->rhs[rem_item->dot]->type() != G::NON_TERMINAL) {
continue;
}
if (rem_item->rule->rhs[rem_item->dot]->symbol() == new_sym) {
ChartItem* new_item = new ChartItem(*rem_item);
new_item->tails_spans.push_back(span);
new_item->dot++;
if (new_item->dot == new_item->rule->rhs.size()) {
new_symbols.insert(new_item->rule->lhs->symbol());
passive.add(new_item, span);
}
}
}
}*/
}
}
} //
|