-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.hpp
65 lines (49 loc) · 1.53 KB
/
grammar.hpp
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
#ifndef GRAMMAR_H
#define GRAMMAR_H
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <cassert>
typedef int symbol;
typedef std::vector<symbol> rule;
class Grammar {
private:
int symbol_count;
int nonterminal_count;
int terminal_count;
std::unordered_map<std::string, symbol> symbol_codes;
std::vector<std::string> code_symbols;
std::vector<std::vector<std::vector<symbol> > > rules;
void init(std::istream& is, std::string start);
public:
Grammar(std::string fname, std::string start="START");
Grammar(std::istream& is, std::string start="START");
const static symbol START_SYMBOL = 0;
symbol token(std::string symbol) const;
std::vector<symbol> tokenize(std::string sentence) const;
std::vector<symbol> tokenize(std::vector<std::string> sentence) const;
std::string symbol_name(symbol symbol) const;
std::string rule_name(int rule) const;
// Returns the rules starting with a nonterminal.
const std::vector<rule>& operator [](symbol nonterminal) const {
return rules[nonterminal];
}
inline bool is_nonterminal(symbol symbol) const {
return symbol < nonterminal_count;
}
inline bool is_terminal(symbol symbol) const {
return !is_nonterminal(symbol);
}
static inline symbol lhs(const rule &rule) {
return rule[0];
}
static inline unsigned long rhs_size(const rule &rule) {
return rule.size() - 1;
}
static inline symbol rhs(const rule &rule, int idx) {
assert(idx + 1 < rule.size());
return rule[idx + 1];
}
};
#endif // GRAMMAR_H