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
|
/*
Copyright (c) 2015, Matthias Schiffer <mschiffer@universe-factory.net>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "generator_slr.hpp"
namespace solar {
void generator_slr_t::generate_first_sets() {
std::set<item_t> items;
for (const std::string &nonterm : get_nonterminals())
first_sets.insert(std::make_pair(nonterm, std::set<symbol_t>()));
for (const rule_t &rule : get_grammar().rules)
items.insert(rule.item);
bool changed = true;
while (changed) {
changed = false;
for (const item_t &item : items) {
std::set<symbol_t> ¤t = first_sets[item.get_lhs()];
if (!item.has_next()) {
changed = changed || current.insert(symbol_t::empty()).second;
continue;
}
const symbol_t &sym = item.get_next_symbol();
if (sym.get_type() != SYMBOL_TYPE_NONTERM) {
changed = changed || current.insert(sym).second;
continue;
}
for (const symbol_t &sym2 : first_sets[sym.get_value()]) {
changed = changed || current.insert(sym2).second;
if (sym2 == symbol_t::empty()) {
item_t next = item;
next.shift();
changed = changed || items.insert(next).second;
}
}
}
}
}
void generator_slr_t::generate_follow_sets() {
generate_first_sets();
for (const std::string &nonterm : get_nonterminals())
follow_sets.insert(std::make_pair(nonterm, std::set<symbol_t>()));
follow_sets[""].insert(symbol_t::empty());
bool changed = true;
while (changed) {
changed = false;
for (const rule_t &rule : get_grammar().rules) {
for (item_t item = rule.item; item.has_next(); item.shift()) {
const symbol_t &sym = item.get_next_symbol();
if (sym.get_type() != SYMBOL_TYPE_NONTERM)
continue;
const std::set<symbol_t> &lhs = follow_sets[item.get_lhs()];
std::set<symbol_t> &rhs = follow_sets[sym.get_value()];
item_t next = item;
next.shift();
bool next_empty = false;
if (next.has_next()) {
const symbol_t &next_sym = next.get_next_symbol();
if (next_sym.get_type() == SYMBOL_TYPE_NONTERM) {
for (const symbol_t &sym2 : first_sets[next_sym.get_value()]) {
if (sym2 == symbol_t::empty())
next_empty = true;
else
changed = changed || rhs.insert(sym2).second;
}
}
else {
changed = changed || rhs.insert(next_sym).second;
}
}
else {
next_empty = true;
}
if (next_empty) {
for (const symbol_t &sym2 : lhs)
changed = changed || rhs.insert(sym2).second;
}
}
}
}
}
bool generator_slr_t::has_reduce_conflict(size_t from, const symbol_t &sym) {
return reductions.count(std::make_pair(from, sym));
}
void generator_slr_t::add_reduction(size_t from, size_t rule) {
const item_t &item = get_grammar().rules[rule].item;
for (const symbol_t sym : follow_sets[item.get_lhs()]) {
if (get_shifts().count(std::make_pair(from, sym)))
throw conflict_error("shift/reduce conflict");
if (!reductions.insert(std::make_pair(std::make_pair(from, sym), rule)).second)
throw conflict_error("reduce/reduce conflict");
}
}
}
|