-
Notifications
You must be signed in to change notification settings - Fork 1
/
grammar.h
109 lines (78 loc) · 2.17 KB
/
grammar.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
#ifndef VALIANT_PARSING_GRAMMAR_H
#define VALIANT_PARSING_GRAMMAR_H
#include <algorithm>
#include <iostream>
#include <list>
#include <set>
#include <stdexcept>
#include <string>
#include <valarray>
#include <vector>
using std::begin;
using std::end;
using std::equal;
using std::istream;
using std::list;
using std::ostream;
using std::runtime_error;
using std::set;
using std::string;
using std::to_string;
using std::valarray;
using std::vector;
struct symbol
{
string identifier;
enum class types {terminal, nonterminal} type;
symbol();
symbol(string _identifier, types _type = types::nonterminal);
symbol(const char* _identifier, types _type = types::nonterminal);
bool operator==(const symbol& other) const;
operator string() const;
operator char() const;
bool operator <(const symbol & other) const;
};
ostream& operator<<(ostream& ostr, const symbol & n);
// union of symbols
struct clause
{
clause() {}
clause(const symbol & nont) { disjuncts = {nont};}
set<symbol> disjuncts;
bool is_sym_set(symbol s) { return disjuncts.find(s) != disjuncts.end(); }
void set_sym(symbol s) { disjuncts.insert(s); }
};
ostream& operator<<(ostream& ostr, const clause& c);
clause operator+(const clause &a, const clause &b);
clause operator+=(clause &a, const clause &b);
struct production
{
symbol left;
valarray<symbol> right;
production(symbol l, valarray<symbol> r);
symbol& operator[](int i);
const symbol& operator[](int i) const;
int size() const;
bool operator==(const production& other) const;
};
struct grammar
{
typedef symbol::types types;
vector<symbol> nonterminals;
vector<symbol> terminals;
vector<production> prods;
void add_terminal(string s);
void add_nonterminal(string s);
void add_production(symbol lhs, valarray<symbol> rhs);
types which_is_it(string s);
vector<production> productions_of(symbol s);
int baking_seed = 0;
symbol bake_nonterminal(string name = "");
// returns {G | s -*-> G} \ {s}
set<symbol> unit_closure(symbol sym);
void unit_elimination();
void to_cfg();
void read_from_stream(istream& stream);
};
ostream& operator<<(ostream& ostr, const grammar& g);
#endif //VALIANT_PARSING_GRAMMAR_H