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
|
/*
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 "output_source_slr.hpp"
namespace solar {
void output_source_slr_t::emit_state_shift(unsigned state) {
std::map<unsigned, std::set<symbol_t>> shifts;
for (const symbol_t &token : get_generator()->get_terminals()) {
auto it = get_generator()->get_shifts().find({state, token});
if (it == get_generator()->get_shifts().end())
continue;
std::set<symbol_t> &symbols = shifts.insert({it->second, std::set<symbol_t>()}).first->second;
symbols.insert(token);
}
for (const auto &entry : shifts) {
for (const symbol_t &sym : entry.second)
write_case(symbol_case(sym));
write_line_("parser->stack[++parser->top].state = ", entry.first);
write_line_("return 1");
write_line();
}
}
void output_source_slr_t::emit_state_reduce_code(const item_t &item, unsigned rule_id) {
const auto &rhs = item.get_rhs();
const std::string &type = get_generator()->get_grammar().get_nonterm_type(item.get_lhs());
const auto &vars = get_generator()->get_grammar().rules[rule_id].variables;
function_t reduce_func("", prefixed(combine("reduce_", rule_id)));
for (unsigned i = 0; i < vars.size(); i++) {
if (vars[i].first.empty())
continue;
reduce_func.add_arg("", combine("parser->stack[parser->top + ", i, "].value.", symbol_value(rhs[i])));
}
for (const auto &arg : get_generator()->get_grammar().extra_args)
reduce_func.add_arg(arg);
if (type.empty())
write_line_(call(reduce_func));
else
write_line_("result.symbol_", item.get_lhs(), " = ", call(reduce_func));
for (unsigned i = 0; i < vars.size(); i++) {
if (vars[i].second)
continue;
auto it = get_generator()->get_grammar().destructors.find(rhs[i]);
if (it == get_generator()->get_grammar().destructors.end())
continue;
write_line_(it->second, "(parser->stack[parser->top + ", i, "].value.", symbol_value(rhs[i]), ")");
}
if (!type.empty())
write_line_("parser->stack[parser->top].value.symbol_", item.get_lhs(), " = result.symbol_", item.get_lhs());
}
bool output_source_slr_t::emit_state_reduce(unsigned state) {
std::map<unsigned, std::set<symbol_t>> reductions;
for (const symbol_t &token : get_generator()->get_terminals()) {
auto it = get_generator()->get_reductions().find({state, token});
if (it == get_generator()->get_reductions().end())
continue;
std::set<symbol_t> &symbols = reductions.insert({it->second, std::set<symbol_t>()}).first->second;
symbols.insert(token);
}
for (const auto &entry : reductions) {
if (reductions.size() == 1) {
write_default();
}
else {
for (const symbol_t &sym : entry.second)
write_case(symbol_case(sym));
}
const rule_t &rule = get_generator()->get_grammar().rules[entry.first];
if (rule.item.get_rhs().size())
write_line_("parser->top -= ", rule.item.get_rhs().size());
if (!rule.action.empty())
emit_state_reduce_code(rule.item, entry.first);
emit_gotos(rule.item.get_lhs());
if (reductions.size() != 1) {
write_line_("break");
write_line();
}
}
return (reductions.size() == 1);
}
void output_source_slr_t::emit_state(unsigned state) {
block_t switch_token(this, "switch (token)");
if (get_generator()->get_shifts().find({state, symbol_t::make_nonterm("")}) != get_generator()->get_shifts().end()) {
write_case(0);
write_line_("return 0");
write_line();
}
emit_state_shift(state);
bool def = emit_state_reduce(state);
if (!def) {
write_default();
write_line_("return -1");
}
}
}
|