2017-01-02 19:51:53 +01:00
|
|
|
#include <iostream>
|
|
|
|
#include <vector>
|
|
|
|
#include <set>
|
|
|
|
#include <string>
|
2017-01-03 15:58:55 +01:00
|
|
|
#include <memory>
|
2017-01-02 19:51:53 +01:00
|
|
|
#include "Parsodus/grammar.h"
|
|
|
|
#include "Parsodus/lrtables/generator.h"
|
|
|
|
#include "Parsodus/lrtables/SLR1Itemset.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
#define ACCEPT_STATE 0
|
|
|
|
#define ERROR_STATE 0
|
|
|
|
|
|
|
|
TEST(lr0, test0) {
|
|
|
|
|
|
|
|
|
|
|
|
pds::Grammar grammar;
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.start = "S";
|
|
|
|
grammar.variables = {"S","A","E"};
|
|
|
|
grammar.terminals = {";", "id", ":=", "+"};
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
std::shared_ptr<pds::Rule> rule(new pds::Rule());
|
|
|
|
rule->head = "S";
|
|
|
|
rule->tail = {"S",";","A"};
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.rules.push_back(std::make_shared<pds::Rule>(*rule));
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
rule->head = "S";
|
|
|
|
rule->tail = {"A"};
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.rules.push_back(std::make_shared<pds::Rule>(*rule));
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
rule->head = "A";
|
|
|
|
rule->tail = {"E"};
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.rules.push_back(std::make_shared<pds::Rule>(*rule));
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
rule->head = "A";
|
|
|
|
rule->tail = {"id",":=","E"};
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.rules.push_back(std::make_shared<pds::Rule>(*rule));
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
rule->head = "E";
|
|
|
|
rule->tail = {"E","+","id"};
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.rules.push_back(std::make_shared<pds::Rule>(*rule));
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
rule->head = "E";
|
|
|
|
rule->tail = {"id"};
|
2017-01-11 17:24:36 +01:00
|
|
|
grammar.rules.push_back(std::make_shared<pds::Rule>(*rule));
|
2017-01-02 19:51:53 +01:00
|
|
|
|
|
|
|
|
2017-01-11 17:24:36 +01:00
|
|
|
{
|
|
|
|
pds::lr::Generator<pds::lr::LR0Itemset> g(grammar);
|
|
|
|
ASSERT_THROW(g.generate(), std::runtime_error);
|
|
|
|
try {
|
|
|
|
g.generate();
|
|
|
|
} catch (std::runtime_error& e) {
|
|
|
|
ASSERT_EQ(std::string("shift-reduce"), e.what());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
|
|
|
pds::lr::Generator<pds::lr::SLR1Itemset> g(grammar);
|
|
|
|
ASSERT_NO_THROW(g.generate());
|
|
|
|
}
|
|
|
|
// auto tbl = g.generate(); // LRTable
|
2017-01-02 19:51:53 +01:00
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
// EXPECT_THROW(throw "shift-reduce", std::exception);
|
|
|
|
/*
|
2017-01-02 19:51:53 +01:00
|
|
|
std::vector<std::map<std::string, std::pair<pds::lr::Action,std::size_t> >> act {
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::SHIFT,4) } },
|
|
|
|
{ {";", std::make_pair(pds::lr::Action::SHIFT,5) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::SHIFT, ACCEPT_STATE)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,3) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,3) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::REDUCE,3) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::REDUCE,3) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE, 3)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,4) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,4) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::ERROR, ERROR_STATE) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::REDUCE,4) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE,4)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,7) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,7) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::REDUCE,7) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::ERROR, ERROR_STATE) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE, 7)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::SHIFT,4) } },
|
|
|
|
{ {"id", std::make_pair(pds::lr::Action::SHIFT,9) } },
|
|
|
|
{ {"id", std::make_pair(pds::lr::Action::SHIFT, 11) } },
|
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,2) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,2) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::REDUCE,2) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::REDUCE,2) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE, 2)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,6) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,6) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::REDUCE,6) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::REDUCE,6) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE, 6)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,5) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,5) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::ERROR, ERROR_STATE) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::REDUCE,5) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE,5)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"id", std::make_pair(pds::lr::Action::REDUCE,7) },
|
|
|
|
{";", std::make_pair(pds::lr::Action::REDUCE,7) },
|
|
|
|
{"+", std::make_pair(pds::lr::Action::REDUCE, 7) },
|
|
|
|
{":=", std::make_pair(pds::lr::Action::REDUCE,7) },
|
|
|
|
{"$", std::make_pair(pds::lr::Action::REDUCE,7)}
|
2017-01-02 19:51:53 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<std::map<std::string, std::size_t>> goto_ = {
|
2017-01-03 15:58:55 +01:00
|
|
|
{ {"S",1}, {"A",2}, {"E",3} }, // state_num:1
|
|
|
|
{ {"A",8}, {"E",3} }, // state_num:5
|
|
|
|
{ {"E",10} }, // state_num:7
|
|
|
|
};
|
2017-01-02 19:51:53 +01:00
|
|
|
|
|
|
|
// Action: enum {ERROR,SHIFT,REDUCE,ACCEPT}
|
2017-01-03 15:58:55 +01:00
|
|
|
|
|
|
|
ASSERT_EQ(goto_.size(),tbl.goto_.size());
|
2017-01-02 19:51:53 +01:00
|
|
|
ASSERT_EQ(act.size(),tbl.act.size());
|
|
|
|
|
|
|
|
for(std::size_t i = 0; i < goto_.size(); i++) {
|
|
|
|
EXPECT_EQ(goto_[i].first,tbl.goto_[i].first);
|
|
|
|
EXPECT_EQ(goto_[i].second.first,tbl.goto_[i].second.first);
|
|
|
|
EXPECT_EQ(goto_[i].second.second,tbl.goto_[i].second.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
for(std::size_t i = 0; i < act.size(); i++) {
|
|
|
|
EXPECT_EQ(act[i].first,tbl.act[i].first);
|
|
|
|
EXPECT_EQ(act[i].second.first,tbl.act[i].second.first);
|
|
|
|
EXPECT_EQ(act[i].second.second,tbl.act[i].second.second);
|
|
|
|
}
|
|
|
|
|
2017-01-03 15:58:55 +01:00
|
|
|
*/
|
2017-01-02 19:51:53 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|