/* Copyright (c) 2015, Matthias Schiffer 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> 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 &symbols = shifts.insert({it->second, std::set()}).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> 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 &symbols = reductions.insert({it->second, std::set()}).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"); } } }