From ed9c1cd0c97e715d96546edea523fcc0695bc43d Mon Sep 17 00:00:00 2001 From: Robin Jadoul Date: Tue, 17 Jan 2017 11:25:26 +0100 Subject: [PATCH] Basic LR(1) but not LALR(1) test --- tests/CMakeLists.txt | 1 + tests/lr1_only.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 tests/lr1_only.cpp diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b62fe1e..6f6d4ef 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,6 @@ add_executable(Parsodus-test lr0_shift-red.cpp + lr1_only.cpp ) target_link_libraries(Parsodus-test # Parsodus-backends diff --git a/tests/lr1_only.cpp b/tests/lr1_only.cpp new file mode 100644 index 0000000..eb68a57 --- /dev/null +++ b/tests/lr1_only.cpp @@ -0,0 +1,47 @@ +#include "Parsodus/lrtables/generator.h" +#include "Parsodus/lrtables/LR1Itemset.h" +#include "Parsodus/lrtables/LALR1Itemset.h" +#include "gtest/gtest.h" + +#include + +TEST(lr1, only) { + using namespace pds; + + Grammar grammar; + grammar.start = "s"; + grammar.variables = {"s","a","b"}; + grammar.terminals = {"A", "B"}; + + for (const std::pair>& p : std::initializer_list>>({ + {"s", {"B", "a", "A"}}, + {"s", {"b", "A"}}, + {"s", {"a"}}, + {"s", {"B", "b"}}, + {"a", {"A"}}, + {"b", {"A"}} + })) { + auto r = std::make_shared(); + r->head = p.first; + r->tail = p.second; + grammar.rules.emplace_back(std::move(r)); + } + + { + pds::lr::Generator g(grammar); + ASSERT_NO_THROW(g.generate()); + } + + { + pds::lr::Generator g(grammar); + ASSERT_THROW(g.generate(), std::runtime_error); + try { + g.generate(); + } catch (std::runtime_error& e) { + ASSERT_EQ(std::string("reduce-reduce"), e.what()); + } + } +} + + +