-
Notifications
You must be signed in to change notification settings - Fork 1
/
earley_serial.cpp
89 lines (77 loc) · 2.11 KB
/
earley_serial.cpp
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
#include "earley_serial.hpp"
#include <iostream>
#include <algorithm>
#include <map>
#include <utility>
using namespace std;
typedef vector<unordered_set<State> > chart_t;
typedef deque<State> worklist_t;
void EarleySerialParser::print_chart(ostream &strm) {
for (int i = 0; i < chart.size(); i++) {
for (const State &s : chart[i]) {
strm << "(0, ";
s.print(strm, grammar);
strm << ")" << endl;
}
}
}
int EarleySerialParser::chart_size() {
int size = 0;
for (int i = 0; i < chart.size(); i++) {
size += chart[i].size();
}
return size;
}
inline void EarleySerialParser::insert(int k, State new_state) {
bool did_insert = chart[k].insert(new_state).second;
if (did_insert) {
worklist[k].push_back(new_state);
}
}
void EarleySerialParser::parse() {
// Insert rules of the form (START -> . a, 0) into C[0].
for (const rule &r : grammar[Grammar::START_SYMBOL]) {
insert(0, State(&r, 0));
}
for (int k = 0; k < sentence.size() + 1; k++) {
while (worklist[k].size() > 0) {
State state = *worklist[k].begin();
worklist[k].pop_front();
if (!state.is_finished()) {
symbol next_elem = state.next_symbol();
if (grammar.is_nonterminal(next_elem)) {
for (const rule &r : grammar[next_elem]) {
insert(k, State(&r, k));
}
} else {
if (k + 1 < chart.size()) {
if (sentence[k] == next_elem) {
insert(k+1, state.incr_pos(k+1));
}
}
}
} else {
int &origin = state.origin;
for (auto s = chart[origin].begin(); s != chart[origin].end(); ++s) {
if (!s->is_finished() && s->next_symbol() == state.lhs()) {
insert(k, s->incr_pos(k));
}
}
}
}
}
}
string EarleySerialParser::name() {
return "earley_serial";
}
bool EarleySerialParser::is_parallel() {
return false;
}
void EarleySerialParser::reset() {
chart.clear();
worklist.clear();
for (int i = 0; i < sentence.size() + 1; i++) {
chart.push_back(std::unordered_set<State>());
worklist.push_back(std::deque<State>());
}
}