48 lines
1.2 KiB
C++
48 lines
1.2 KiB
C++
#include "Parsodus/lrtables/generator.h"
|
|
#include "Parsodus/lrtables/LR1Itemset.h"
|
|
#include "Parsodus/lrtables/LALR1Itemset.h"
|
|
#include "gtest/gtest.h"
|
|
|
|
#include <memory>
|
|
|
|
TEST(lr1, only) {
|
|
using namespace pds;
|
|
|
|
Grammar grammar;
|
|
grammar.start = "s";
|
|
grammar.variables = {"s","a","b"};
|
|
grammar.terminals = {"A", "B"};
|
|
|
|
for (const std::pair<std::string, std::vector<std::string>>& p : std::initializer_list<std::pair<std::string, std::vector<std::string>>>({
|
|
{"s", {"B", "a", "A"}},
|
|
{"s", {"b", "A"}},
|
|
{"s", {"a"}},
|
|
{"s", {"B", "b"}},
|
|
{"a", {"A"}},
|
|
{"b", {"A"}}
|
|
})) {
|
|
auto r = std::make_shared<Rule>();
|
|
r->head = p.first;
|
|
r->tail = p.second;
|
|
grammar.rules.emplace_back(std::move(r));
|
|
}
|
|
|
|
{
|
|
pds::lr::Generator<lr::LR1Itemset> g(grammar);
|
|
ASSERT_NO_THROW(g.generate());
|
|
}
|
|
|
|
{
|
|
pds::lr::Generator<pds::lr::LALR1Itemset> 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());
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|