From 679aa4588460b53b3b43ad19e76865c373c8f221 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Tue, 31 Mar 2015 00:04:32 +0200 Subject: Rename state_t to parser_state_t --- src/CMakeLists.txt | 2 +- src/parser.cpp | 2 +- src/parser.hpp | 4 ++-- src/parser_state.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ src/parser_state.hpp | 58 +++++++++++++++++++++++++++++++++++++++++++++++ src/solar.cpp | 4 ++-- src/state.cpp | 64 ---------------------------------------------------- src/state.hpp | 58 ----------------------------------------------- 8 files changed, 128 insertions(+), 128 deletions(-) create mode 100644 src/parser_state.cpp create mode 100644 src/parser_state.hpp delete mode 100644 src/state.cpp delete mode 100644 src/state.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index bb5e874..67773dc 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,7 @@ add_executable(solar lex.cpp parser.cpp + parser_state.cpp solar.cpp - state.cpp ) set_target_properties(solar PROPERTIES COMPILE_FLAGS "-std=c++11 -Wall") diff --git a/src/parser.cpp b/src/parser.cpp index bd4dfbf..c931ca6 100644 --- a/src/parser.cpp +++ b/src/parser.cpp @@ -49,7 +49,7 @@ parser_t * parser_alloc(void) { return parser; } -int parser_push(parser_t *parser, int token, const value_t *value, state_t *state) { +int parser_push(parser_t *parser, int token, const value_t *value, parser_state_t *state) { switch (parser->state) { case STATE_INIT: switch (token) { diff --git a/src/parser.hpp b/src/parser.hpp index c5a1e48..0cd6b38 100644 --- a/src/parser.hpp +++ b/src/parser.hpp @@ -26,7 +26,7 @@ #pragma once -#include "state.hpp" +#include "parser_state.hpp" #include @@ -52,7 +52,7 @@ typedef struct parser parser_t; parser_t * parser_alloc(void); -int parser_push(parser_t *parser, int token, const value_t *value, state_t *state); +int parser_push(parser_t *parser, int token, const value_t *value, parser_state_t *state); void parser_free(parser_t *parser); } diff --git a/src/parser_state.cpp b/src/parser_state.cpp new file mode 100644 index 0000000..c8053be --- /dev/null +++ b/src/parser_state.cpp @@ -0,0 +1,64 @@ +/* + 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 "parser_state.hpp" + + +namespace solar { + +void parser_state_t::new_rule(const char *nonterm) { + if (rules.empty()) { + // start rule + current.get_rhs().emplace_back(symbol_t::make_nonterm(nonterm)); + add_rule(); + } + + current = item_t(nonterm); + +} + +void parser_state_t::add_rule_nonterminal(const char *nonterm) { + current.get_rhs().emplace_back(symbol_t::make_nonterm(nonterm)); +} + +void parser_state_t::add_rule_terminal(const char *term) { + current.get_rhs().emplace_back(symbol_t::make_term(term)); +} + +void parser_state_t::add_rule_terminal(unsigned char term) { + current.get_rhs().emplace_back(symbol_t::make_char(term)); +} + +void parser_state_t::add_rule() { + rules.emplace(current.get_lhs(), current); + + while (current.has_next()) { + items.emplace(current.get_next_symbol(), current); + current.shift(); + } +} + +} diff --git a/src/parser_state.hpp b/src/parser_state.hpp new file mode 100644 index 0000000..a0341c6 --- /dev/null +++ b/src/parser_state.hpp @@ -0,0 +1,58 @@ +/* + 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 "item.hpp" + +#include +#include + + +namespace solar { + +class parser_state_t { +private: + std::multimap rules; + std::multimap items; + + item_t current; + +public: + parser_state_t() : current("") {} + + const std::multimap & get_rules() const { + return rules; + } + + void new_rule(const char *nonterm); + void add_rule_nonterminal(const char *nonterm); + void add_rule_terminal(const char *term); + void add_rule_terminal(unsigned char term); + void add_rule(); +}; + +} diff --git a/src/solar.cpp b/src/solar.cpp index a5085a1..f466db8 100644 --- a/src/solar.cpp +++ b/src/solar.cpp @@ -33,7 +33,7 @@ namespace solar { -bool read_grammar(const char *filename, state_t *state) { +bool read_grammar(const char *filename, parser_state_t *state) { FILE *file = fopen(filename, "r"); if (!file) { std::fprintf(stderr, "unable to open file %s\n", filename); @@ -75,7 +75,7 @@ bool read_grammar(const char *filename, state_t *state) { int main() { using namespace solar; - state_t state; + parser_state_t state; if (!read_grammar("grammar.y", &state)) return 1; diff --git a/src/state.cpp b/src/state.cpp deleted file mode 100644 index 958ab3e..0000000 --- a/src/state.cpp +++ /dev/null @@ -1,64 +0,0 @@ -/* - 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 "state.hpp" - - -namespace solar { - -void state_t::new_rule(const char *nonterm) { - if (rules.empty()) { - // start rule - current.get_rhs().emplace_back(symbol_t::make_nonterm(nonterm)); - add_rule(); - } - - current = item_t(nonterm); - -} - -void state_t::add_rule_nonterminal(const char *nonterm) { - current.get_rhs().emplace_back(symbol_t::make_nonterm(nonterm)); -} - -void state_t::add_rule_terminal(const char *term) { - current.get_rhs().emplace_back(symbol_t::make_term(term)); -} - -void state_t::add_rule_terminal(unsigned char term) { - current.get_rhs().emplace_back(symbol_t::make_char(term)); -} - -void state_t::add_rule() { - rules.emplace(current.get_lhs(), current); - - while (current.has_next()) { - items.emplace(current.get_next_symbol(), current); - current.shift(); - } -} - -} diff --git a/src/state.hpp b/src/state.hpp deleted file mode 100644 index 8069ef0..0000000 --- a/src/state.hpp +++ /dev/null @@ -1,58 +0,0 @@ -/* - 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 "item.hpp" - -#include -#include - - -namespace solar { - -class state_t { -private: - std::multimap rules; - std::multimap items; - - item_t current; - -public: - state_t() : current("") {} - - const std::multimap & get_rules() const { - return rules; - } - - void new_rule(const char *nonterm); - void add_rule_nonterminal(const char *nonterm); - void add_rule_terminal(const char *term); - void add_rule_terminal(unsigned char term); - void add_rule(); -}; - -} -- cgit v1.2.3