From 5887ec38e1ff7f23c50140ccbb12debcab281abd Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 9 Apr 2015 17:21:32 +0200 Subject: generator: add generator_lr0 and generator_slr classes --- src/CMakeLists.txt | 2 ++ src/generator.cpp | 3 +-- src/generator.hpp | 2 +- src/generator_lr0.cpp | 27 +++++++++++++++++++++++++++ src/generator_lr0.hpp | 39 +++++++++++++++++++++++++++++++++++++++ src/generator_slr.cpp | 27 +++++++++++++++++++++++++++ src/generator_slr.hpp | 39 +++++++++++++++++++++++++++++++++++++++ src/solar.cpp | 4 ++-- 8 files changed, 138 insertions(+), 5 deletions(-) create mode 100644 src/generator_lr0.cpp create mode 100644 src/generator_lr0.hpp create mode 100644 src/generator_slr.cpp create mode 100644 src/generator_slr.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 711506c..f4cf78a 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,5 +1,7 @@ add_executable(solar generator.cpp + generator_slr.cpp + generator_lr0.cpp lex.cpp output.cpp parser.cpp diff --git a/src/generator.cpp b/src/generator.cpp index a0f88d8..1774448 100644 --- a/src/generator.cpp +++ b/src/generator.cpp @@ -117,8 +117,7 @@ void generator_t::generate_itemsets() { } } -generator_t::generator_t(const grammar_t &grammar0) - : grammar(grammar0) { +generator_t::generator_t(const grammar_t &grammar0) : grammar(grammar0) { for (size_t i = 0; i < grammar.rules.size(); i++) { item_t rule = grammar.rules[i].item; diff --git a/src/generator.hpp b/src/generator.hpp index 576fccb..1ad70ab 100644 --- a/src/generator.hpp +++ b/src/generator.hpp @@ -121,7 +121,7 @@ public: return gotos; } - +protected: generator_t(const grammar_t &grammar0); }; diff --git a/src/generator_lr0.cpp b/src/generator_lr0.cpp new file mode 100644 index 0000000..cbf051c --- /dev/null +++ b/src/generator_lr0.cpp @@ -0,0 +1,27 @@ +/* + 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 "generator_lr0.hpp" diff --git a/src/generator_lr0.hpp b/src/generator_lr0.hpp new file mode 100644 index 0000000..c434ed6 --- /dev/null +++ b/src/generator_lr0.hpp @@ -0,0 +1,39 @@ +/* + 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.hpp" + + +namespace solar { + +class generator_lr0_t : public generator_t { +public: + generator_lr0_t(const grammar_t &grammar) : generator_t(grammar) {} +}; + +} diff --git a/src/generator_slr.cpp b/src/generator_slr.cpp new file mode 100644 index 0000000..69bb1a0 --- /dev/null +++ b/src/generator_slr.cpp @@ -0,0 +1,27 @@ +/* + 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 "generator_slr.hpp" diff --git a/src/generator_slr.hpp b/src/generator_slr.hpp new file mode 100644 index 0000000..9a2cd42 --- /dev/null +++ b/src/generator_slr.hpp @@ -0,0 +1,39 @@ +/* + 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.hpp" + + +namespace solar { + +class generator_slr_t : public generator_t { +public: + generator_slr_t(const grammar_t &grammar) : generator_t(grammar) {} +}; + +} diff --git a/src/solar.cpp b/src/solar.cpp index 68a1df6..57d4fee 100644 --- a/src/solar.cpp +++ b/src/solar.cpp @@ -26,7 +26,7 @@ #include "lex.hpp" #include "parser.hpp" -#include "generator.hpp" +#include "generator_lr0.hpp" #include "output.hpp" @@ -87,7 +87,7 @@ int main(int argc, char *argv[]) { if (!read_grammar(argv[1], &state)) return 1; - generator_t generator(state.get_grammar()); + generator_lr0_t generator(state.get_grammar()); output_t output(&generator, argv[3], argv[2]); output.write(); -- cgit v1.2.3