2015-04-10 03:18:56 +02:00
|
|
|
/*
|
|
|
|
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_lr0.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
namespace solar {
|
|
|
|
|
2015-04-10 22:00:34 +02:00
|
|
|
void output_lr0_t::emit_state_shift(unsigned state) {
|
2015-04-10 03:18:56 +02:00
|
|
|
std::fprintf(source_file, "\t\t\tswitch (token) {\n");
|
|
|
|
|
2015-04-10 22:00:34 +02:00
|
|
|
if (generator->get_shifts().find(std::make_pair(state, symbol_t::make_nonterm(""))) != generator->get_shifts().end()) {
|
2015-04-10 03:18:56 +02:00
|
|
|
std::fprintf(source_file, "\t\t\tcase 0:\n");
|
|
|
|
std::fprintf(source_file, "\t\t\t\treturn 0;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &token : generator->get_terminals()) {
|
2015-04-10 22:00:34 +02:00
|
|
|
auto it = generator->get_shifts().find(std::make_pair(state, token));
|
2015-04-10 03:18:56 +02:00
|
|
|
if (it == generator->get_shifts().end())
|
|
|
|
continue;
|
|
|
|
|
2015-04-10 19:00:43 +02:00
|
|
|
std::fprintf(source_file, "\t\t\tcase %s:\n", symbol_case(token).c_str());
|
2015-04-10 03:18:56 +02:00
|
|
|
std::fprintf(source_file, "\t\t\t\tparser->stack[parser->top].value.token = *value;\n");
|
|
|
|
std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second));
|
|
|
|
std::fprintf(source_file, "\t\t\t\treturn 1;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\tdefault:\n");
|
|
|
|
std::fprintf(source_file, "\t\t\t\treturn -1;\n");
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\t}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
void output_lr0_t::emit_state_reduce(const item_t &item, int rule_id) {
|
|
|
|
const auto &rhs = item.get_rhs();
|
|
|
|
if (rhs.size())
|
|
|
|
std::fprintf(source_file, "\t\t\tparser->top -= %u;\n", unsigned(rhs.size()));
|
|
|
|
|
|
|
|
if (rule_id >= 0) {
|
|
|
|
const std::string &type = generator->get_grammar().get_nonterm_type(item.get_lhs());
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\t");
|
|
|
|
if (!type.empty())
|
|
|
|
std::fprintf(source_file, "parser->stack[parser->top].value.symbol_%s = ", item.get_lhs().c_str());
|
|
|
|
std::fprintf(source_file, "%sreduce_%i(", prefix(), rule_id);
|
|
|
|
|
|
|
|
bool empty = true;
|
|
|
|
const auto &vars = generator->get_grammar().rules[rule_id].variables;
|
|
|
|
for (unsigned i = 0; i < vars.size(); i++) {
|
|
|
|
const std::string &var = vars[i];
|
|
|
|
if (var.empty())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!empty)
|
|
|
|
std::fprintf(source_file, ", ");
|
|
|
|
|
|
|
|
std::fprintf(source_file, "parser->stack[parser->top + %u].value.%s", i, symbol_values[rhs[i]].c_str());
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (const auto &arg : generator->get_grammar().extra_args) {
|
|
|
|
if (!empty)
|
|
|
|
std::fprintf(source_file, ", ");
|
|
|
|
|
|
|
|
std::fprintf(source_file, "%s", arg.second.c_str());
|
|
|
|
|
|
|
|
empty = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fprintf(source_file, ");\n");
|
|
|
|
}
|
|
|
|
|
2015-04-10 23:22:12 +02:00
|
|
|
emit_gotos(item.get_lhs());
|
2015-04-10 03:18:56 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 22:00:34 +02:00
|
|
|
void output_lr0_t::emit_state(unsigned state) {
|
|
|
|
std::fprintf(source_file, "\t\tcase %u:\n", state);
|
2015-04-10 03:18:56 +02:00
|
|
|
|
2015-04-10 22:00:34 +02:00
|
|
|
auto it = generator->get_reductions().find(state);
|
2015-04-10 03:18:56 +02:00
|
|
|
if (it == generator->get_reductions().end()) {
|
2015-04-10 22:00:34 +02:00
|
|
|
emit_state_shift(state);
|
2015-04-10 03:18:56 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
const rule_t &rule = generator->get_grammar().rules[it->second];
|
|
|
|
emit_state_reduce(rule.item, rule.action.empty() ? -1 : it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::fprintf(source_file, "\t\t\tbreak;\n\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|