From 8de90acc6791a62d4b2e48e9bd05daa0d5cfe4b6 Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Fri, 27 Mar 2015 03:38:01 +0100 Subject: Generate items from grammar --- src/state.cpp | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 src/state.cpp (limited to 'src/state.cpp') diff --git a/src/state.cpp b/src/state.cpp new file mode 100644 index 0000000..e798dce --- /dev/null +++ b/src/state.cpp @@ -0,0 +1,67 @@ +/* + 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 (true) { + items.emplace(current.get_next_symbol(), current); + if (!current.can_shift()) + break; + + current.shift(); + } +} + +} -- cgit v1.2.3