From 7a14a8ec2fd45a496bcaa15c13a7904ba09c4c95 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 10 Apr 2015 03:18:56 +0200 Subject: Split output_lr0_t out of output_t --- src/CMakeLists.txt | 3 +- src/output.cpp | 167 +++++++++-------------------------------------------- src/output.hpp | 19 +++--- src/output_lr0.cpp | 146 ++++++++++++++++++++++++++++++++++++++++++++++ src/output_lr0.hpp | 55 ++++++++++++++++++ src/solar.cpp | 4 +- 6 files changed, 242 insertions(+), 152 deletions(-) create mode 100644 src/output_lr0.cpp create mode 100644 src/output_lr0.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f4cf78a..725d0f0 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,9 +1,10 @@ add_executable(solar generator.cpp - generator_slr.cpp generator_lr0.cpp + generator_slr.cpp lex.cpp output.cpp + output_lr0.cpp parser.cpp parser_state.cpp solar.cpp diff --git a/src/output.cpp b/src/output.cpp index f9ffc1f..7f6e5c8 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -33,11 +33,10 @@ namespace solar { -output_t::output_t(const generator_lr0_t *generator0, const char *header, const char *source) +output_t::output_t(const char *header, const char *source) : prefix_str("parse_"), token_prefix_str("TOK_"), stack_size(100), - generator(generator0), header_filename(header) { header_file = std::fopen(header, "w"); if (!header_file) @@ -46,23 +45,25 @@ output_t::output_t(const generator_lr0_t *generator0, const char *header, const source_file = std::fopen(source, "w"); if (!source_file) throw std::system_error(errno, std::generic_category(), "unable to open source output file for writing"); +} - for (const std::string &nonterm : generator->get_nonterminals()) +output_t::~output_t() { + std::fclose(header_file); + std::fclose(source_file); +} + +void output_t::initialize() { + for (const std::string &nonterm : get_generator()->get_nonterminals()) symbol_values.insert(std::make_pair(symbol_t::make_nonterm(nonterm.c_str()), "symbol_" + nonterm)); - for (const symbol_t &term : generator->get_terminals()) { + for (const symbol_t &term : get_generator()->get_terminals()) { if (term.get_type() == SYMBOL_TYPE_TERM) tokens.insert(std::make_pair(term.get_value(), tokens.size())); - symbol_values.insert(std::make_pair(term, "token." + generator->get_grammar().get_term_type(term).second)); + symbol_values.insert(std::make_pair(term, "token." + get_generator()->get_grammar().get_term_type(term).second)); } } -output_t::~output_t() { - std::fclose(header_file); - std::fclose(source_file); -} - void output_t::emit_tokens() { if (tokens.empty()) return; @@ -80,8 +81,8 @@ void output_t::emit_token_value() { std::map token_values; - for (const symbol_t &term : generator->get_terminals()) { - const auto &type = generator->get_grammar().get_term_type(term); + for (const symbol_t &term : get_generator()->get_terminals()) { + const auto &type = get_generator()->get_grammar().get_term_type(term); if (!type.first.empty()) token_values.insert(std::make_pair(type.second, type.first)); } @@ -95,8 +96,8 @@ void output_t::emit_token_value() { void output_t::emit_header() { std::fprintf(header_file, "#pragma once\n\n"); - if (!generator->get_grammar().header_block.empty()) - std::fprintf(header_file, "%s\n", generator->get_grammar().header_block.c_str()); + if (!get_generator()->get_grammar().header_block.empty()) + std::fprintf(header_file, "%s\n", get_generator()->get_grammar().header_block.c_str()); emit_tokens(); emit_token_value(); @@ -108,19 +109,19 @@ void output_t::emit_header() { std::fprintf(header_file, "int %spush(%scontext_t *parser, int token, const %stoken_value_t *value", prefix(), prefix(), prefix()); - for (const auto &arg : generator->get_grammar().extra_args) + for (const auto &arg : get_generator()->get_grammar().extra_args) std::fprintf(header_file, ", %s %s", arg.first.c_str(), arg.second.c_str()); std::fprintf(header_file, ");\n"); } void output_t::emit_reduction(unsigned rule_id) { - const rule_t &rule = generator->get_grammar().rules[rule_id]; + const rule_t &rule = get_generator()->get_grammar().rules[rule_id]; std::fprintf(source_file, "static inline "); const item_t &item = rule.item; - const std::string &type = generator->get_grammar().get_nonterm_type(item.get_lhs()); + const std::string &type = get_generator()->get_grammar().get_nonterm_type(item.get_lhs()); if (type.empty()) std::fprintf(source_file, "void"); else @@ -138,12 +139,12 @@ void output_t::emit_reduction(unsigned rule_id) { if (!empty) std::fprintf(source_file, ", "); - std::fprintf(source_file, "%s %s", generator->get_grammar().get_type(item.get_rhs()[i]).c_str(), var.c_str()); + std::fprintf(source_file, "%s %s", get_generator()->get_grammar().get_type(item.get_rhs()[i]).c_str(), var.c_str()); empty = false; } - for (const auto &arg : generator->get_grammar().extra_args) { + for (const auto &arg : get_generator()->get_grammar().extra_args) { if (!empty) std::fprintf(source_file, ", "); @@ -162,7 +163,7 @@ void output_t::emit_reduction(unsigned rule_id) { } void output_t::emit_reductions() { - const auto &rules = generator->get_grammar().rules; + const auto &rules = get_generator()->get_grammar().rules; for (size_t i = 0; i < rules.size(); i++) { if (!rules[i].action.empty()) @@ -170,122 +171,8 @@ void output_t::emit_reductions() { } } -void output_t::emit_state_shift(unsigned i) { - std::fprintf(source_file, "\t\t\tswitch (token) {\n"); - - if (generator->get_shifts().find(std::make_pair(i, symbol_t::make_nonterm(""))) != generator->get_shifts().end()) { - 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()) { - auto it = generator->get_shifts().find(std::make_pair(i, token)); - if (it == generator->get_shifts().end()) - continue; - - if (token.get_type() == SYMBOL_TYPE_CHAR) - std::fprintf(source_file, "\t\t\tcase '%c':\n", token.get_value()[0]); - else - std::fprintf(source_file, "\t\t\tcase %s%s:\n", token_prefix(), token.get_value().c_str()); - - 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_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"); - } - - std::vector> gotos; - - for (size_t i = 0; i < generator->get_state_count(); i++) { - auto it = generator->get_gotos().find(std::make_pair(i, item.get_lhs())); - if (it == generator->get_gotos().end()) - continue; - - gotos.emplace_back(i, it->second); - } - - if (gotos.size() == 1) { - std::fprintf(source_file, "\t\t\tparser->stack[++parser->top].state = %u;\n", gotos[0].second); - } - else { - std::fprintf(source_file, "\t\t\tswitch (parser->stack[parser->top].state) {\n"); - - for (size_t i = 0; i < generator->get_state_count(); i++) { - auto it = generator->get_gotos().find(std::make_pair(i, item.get_lhs())); - if (it == generator->get_gotos().end()) - continue; - - std::fprintf(source_file, "\t\t\tcase %u:\n", unsigned(i)); - std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second)); - std::fprintf(source_file, "\t\t\t\tbreak;\n"); - } - - - std::fprintf(source_file, "\t\t\t}\n"); - } -} - -void output_t::emit_state(unsigned i) { - std::fprintf(source_file, "\t\tcase %u:\n", i); - - auto it = generator->get_reductions().find(i); - if (it == generator->get_reductions().end()) { - emit_state_shift(i); - } - 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"); -} - void output_t::emit_states() { - for (size_t i = 0; i < generator->get_state_count(); i++) + for (size_t i = 0; i < get_generator()->get_state_count(); i++) emit_state(i); } @@ -305,14 +192,14 @@ void output_t::emit_header_include() { void output_t::emit_source() { emit_header_include(); - if (!generator->get_grammar().source_block.empty()) - std::fprintf(source_file, "%s\n\n", generator->get_grammar().source_block.c_str()); + if (!get_generator()->get_grammar().source_block.empty()) + std::fprintf(source_file, "%s\n\n", get_generator()->get_grammar().source_block.c_str()); std::fprintf(source_file, "typedef union %ssymbol_value {\n", prefix()); std::fprintf(source_file, "\t%stoken_value_t token;\n", prefix()); - for (const auto &nonterm : generator->get_nonterminals()) { - const std::string &type = generator->get_grammar().get_nonterm_type(nonterm); + for (const auto &nonterm : get_generator()->get_nonterminals()) { + const std::string &type = get_generator()->get_grammar().get_nonterm_type(nonterm); if (!type.empty()) std::fprintf(source_file, "\t%s symbol_%s;\n", type.c_str(), nonterm.c_str()); @@ -344,7 +231,7 @@ void output_t::emit_source() { emit_reductions(); std::fprintf(source_file, "int %spush(%scontext_t *parser, int token, const %stoken_value_t *value", prefix(), prefix(), prefix()); - for (const auto &arg : generator->get_grammar().extra_args) + for (const auto &arg : get_generator()->get_grammar().extra_args) std::fprintf(source_file, ", %s %s", arg.first.c_str(), arg.second.c_str()); std::fprintf(source_file, ") {\n"); diff --git a/src/output.hpp b/src/output.hpp index 0bff7d3..8c457dd 100644 --- a/src/output.hpp +++ b/src/output.hpp @@ -26,7 +26,7 @@ #pragma once -#include "generator_lr0.hpp" +#include "generator_slr.hpp" #include @@ -34,13 +34,11 @@ namespace solar { class output_t { -private: +protected: std::string prefix_str; std::string token_prefix_str; unsigned stack_size; - const generator_lr0_t *generator; - std::string header_filename; std::FILE *header_file; @@ -65,16 +63,19 @@ private: void emit_reduction(unsigned rule_id); void emit_reductions(); - void emit_state_shift(unsigned i); - void emit_state_reduce(const item_t &item, int rule_id); - void emit_state(unsigned i); void emit_states(); void emit_header_include(); void emit_source(); + void initialize(); + + virtual const generator_t * get_generator() = 0; + virtual void emit_state(unsigned i) = 0; + + output_t(const char *header, const char *source); + public: - output_t(const generator_lr0_t *generator0, const char *header, const char *source); - ~output_t(); + virtual ~output_t(); void write(); }; diff --git a/src/output_lr0.cpp b/src/output_lr0.cpp new file mode 100644 index 0000000..86206a3 --- /dev/null +++ b/src/output_lr0.cpp @@ -0,0 +1,146 @@ +/* + 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_lr0.hpp" + + +namespace solar { + +void output_lr0_t::emit_state_shift(unsigned i) { + std::fprintf(source_file, "\t\t\tswitch (token) {\n"); + + if (generator->get_shifts().find(std::make_pair(i, symbol_t::make_nonterm(""))) != generator->get_shifts().end()) { + 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()) { + auto it = generator->get_shifts().find(std::make_pair(i, token)); + if (it == generator->get_shifts().end()) + continue; + + if (token.get_type() == SYMBOL_TYPE_CHAR) + std::fprintf(source_file, "\t\t\tcase '%c':\n", token.get_value()[0]); + else + std::fprintf(source_file, "\t\t\tcase %s%s:\n", token_prefix(), token.get_value().c_str()); + + 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"); + } + + std::vector> gotos; + + for (size_t i = 0; i < generator->get_state_count(); i++) { + auto it = generator->get_gotos().find(std::make_pair(i, item.get_lhs())); + if (it == generator->get_gotos().end()) + continue; + + gotos.emplace_back(i, it->second); + } + + if (gotos.size() == 1) { + std::fprintf(source_file, "\t\t\tparser->stack[++parser->top].state = %u;\n", gotos[0].second); + } + else { + std::fprintf(source_file, "\t\t\tswitch (parser->stack[parser->top].state) {\n"); + + for (size_t i = 0; i < generator->get_state_count(); i++) { + auto it = generator->get_gotos().find(std::make_pair(i, item.get_lhs())); + if (it == generator->get_gotos().end()) + continue; + + std::fprintf(source_file, "\t\t\tcase %u:\n", unsigned(i)); + std::fprintf(source_file, "\t\t\t\tparser->stack[++parser->top].state = %u;\n", unsigned(it->second)); + std::fprintf(source_file, "\t\t\t\tbreak;\n"); + } + + + std::fprintf(source_file, "\t\t\t}\n"); + } +} + +void output_lr0_t::emit_state(unsigned i) { + std::fprintf(source_file, "\t\tcase %u:\n", i); + + auto it = generator->get_reductions().find(i); + if (it == generator->get_reductions().end()) { + emit_state_shift(i); + } + 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"); +} + +} diff --git a/src/output_lr0.hpp b/src/output_lr0.hpp new file mode 100644 index 0000000..dc77761 --- /dev/null +++ b/src/output_lr0.hpp @@ -0,0 +1,55 @@ +/* + 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. +*/ + + +#pragma once + +#include "generator_lr0.hpp" +#include "output.hpp" + + +namespace solar { + +class output_lr0_t : public output_t { +private: + const generator_lr0_t *generator; + + void emit_state_shift(unsigned i); + void emit_state_reduce(const item_t &item, int rule_id); + +protected: + virtual const generator_t * get_generator() { + return generator; + } + + virtual void emit_state(unsigned i); + +public: + output_lr0_t(const generator_lr0_t *generator0, const char *header, const char *source) : output_t(header, source), generator(generator0) { + initialize(); + } +}; + +} diff --git a/src/solar.cpp b/src/solar.cpp index 57d4fee..e6c0b0a 100644 --- a/src/solar.cpp +++ b/src/solar.cpp @@ -27,7 +27,7 @@ #include "lex.hpp" #include "parser.hpp" #include "generator_lr0.hpp" -#include "output.hpp" +#include "output_lr0.hpp" #include @@ -89,7 +89,7 @@ int main(int argc, char *argv[]) { generator_lr0_t generator(state.get_grammar()); - output_t output(&generator, argv[3], argv[2]); + output_lr0_t output(&generator, argv[3], argv[2]); output.write(); return 0; -- cgit v1.2.3