-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.cpp
163 lines (137 loc) · 4.14 KB
/
grammar.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
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
#include <fstream>
#include <sstream>
#include <algorithm>
#include <iterator>
#include <deque>
#include <string>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <utility>
#include <vector>
#include "grammar.hpp"
using namespace std;
vector<string> split(string &str) {
istringstream strm(str);
vector<string> words;
copy(istream_iterator<string>(strm),
istream_iterator<string>(),
back_inserter(words));
return words;
}
void Grammar::init(istream& is, string start) {
set< vector<string> > string_rules;
set<string> symbols;
int line_count = 0;
string line;
while (getline(is, line)) {
line_count++;
// Strip trailing spaces.
line.erase(find_if(line.rbegin(), line.rend(), [](int ch) {
return !isspace(ch); }).base(), line.end());
// Strip leading spaces.
line.erase(line.begin(), find_if(line.begin(), line.end(), [](int ch) {
return !isspace(ch); }));
// Strip trailing comments.
auto hash_pos = find_if(line.rbegin(), line.rend(),
[](int ch) { return ch == '#'; }).base();
if (hash_pos != line.begin()) {
line.erase(hash_pos, line.end());
}
if (line.length() == 0) { continue; }
vector<string> rule = split(line);
if (rule.size() == 1) {
cerr << "Error: Rule without RHS." << endl;
exit(1);
}
for (string sym : rule) { symbols.insert(sym); }
string_rules.insert(rule);
}
//separate nonterminals from terminals
unordered_set<string> nonterminals;
for(auto r = string_rules.begin(); r != string_rules.end(); r++){
nonterminals.insert((*r)[0]);
}
nonterminal_count = nonterminals.size();
//create symbol codes
symbol_codes = unordered_map<string, int>();
code_symbols = vector<string>();
//symbol codes start with START
if(nonterminals.find(start) == nonterminals.end()){
cerr << "error: start nonterminal " << start << " not found" << endl;
exit(1);
}
symbol_codes[start] = 0;
code_symbols.push_back(start);
symbol_count = 1;
//symbol codes continue with nonterminals
for(auto sym = nonterminals.begin(); sym != nonterminals.end(); sym++){
if(symbol_codes.find(*sym) == symbol_codes.end()){
symbol_codes[*sym] = symbol_count;
code_symbols.push_back(*sym);
symbol_count++;
}
}
//symbol codes finish with terminals
for(auto sym = symbols.begin(); sym != symbols.end(); sym++){
if(symbol_codes.find(*sym) == symbol_codes.end()){
symbol_codes[*sym] = symbol_count;
code_symbols.push_back(*sym);
symbol_count++;
}
}
//finish up the counts of symbols
terminal_count = symbol_count - nonterminal_count;
//create rule codes
rules = vector<vector<vector<int> > >(nonterminal_count);
for (int i = 0; i < nonterminal_count; i++) {
rules[i] = vector<vector<int> >();
}
for(auto r = string_rules.begin(); r != string_rules.end(); r++){
vector<int> compressed_rule;
for(auto sym = r->begin(); sym != r->end(); sym++){
compressed_rule.push_back(symbol_codes[*sym]);
}
symbol lhs = compressed_rule[0];
rules[lhs].push_back(compressed_rule);
}
}
Grammar::Grammar(istream& is, string start) {
Grammar::init(is, start);
}
Grammar::Grammar(string fname, string start) {
ifstream myfile;
myfile.open(fname);
Grammar::init(myfile, start);
myfile.close();
}
symbol Grammar::token(string symbol) const {
if(symbol_codes.find(symbol) == symbol_codes.end()){
cerr << "error: unrecognized symbol " << symbol << endl;
exit(1);
}
int code = symbol_codes.at(symbol);
if(is_nonterminal(code)){
cerr << "error: cannot parse nonterminal symbol " << symbol << endl;
exit(1);
}
return code;
}
vector<int> Grammar::tokenize(string sentence) const {
vector<int> tokens;
for (string tok : split(sentence)) {
tokens.push_back(token(tok));
}
return tokens;
}
vector<int> Grammar::tokenize(vector<string> sentence) const {
vector<int> tokens;
for(auto sym = sentence.begin(); sym != sentence.end(); sym++){
tokens.push_back(token(*sym));
}
return tokens;
}
string Grammar::symbol_name(int symbol) const {
return code_symbols[symbol];
}