Fix tests

This commit is contained in:
Thomas Avé 2017-01-03 15:58:55 +01:00
parent 9bc22af7b8
commit 601be444a7
4 changed files with 77 additions and 84 deletions

View File

@ -131,11 +131,11 @@ LRTable Generator<Itemset>::generate() {
} else if (table.act[curIdx].count(term)) {
if (table.act[curIdx][term].first == Action::SHIFT) {
//Shift-Reduce conflict, rapport and resolve it (TODO)
throw "shift-reduce";
throw std::runtime_error("shift-reduce");
} else if (table.act[curIdx][term].first == Action::REDUCE
&& table.act[curIdx][term].second != rule_applied) {
//Reduce-Reduce conflict, rapport it (TODO)
throw 1;
throw std::runtime_error("reduce-reduce");
} else {
//Reduce using the same rule, no problem, NO-OP
}

View File

@ -2,8 +2,9 @@ add_executable(Parsodus-test
lr0_shift-red.cpp
)
target_link_libraries(Parsodus-test
#Parsodus-tables
# Parsodus-backends
Parsodus-tables
Parsodus-util
pds
mstch::mstch
${GTEST_BOTH_LIBRARIES}

View File

@ -2,6 +2,7 @@
#include <vector>
#include <set>
#include <string>
#include <memory>
#include "Parsodus/grammar.h"
#include "Parsodus/lrtables/generator.h"
#include "Parsodus/lrtables/SLR1Itemset.h"
@ -18,105 +19,104 @@ TEST(lr0, test0) {
grammar.variables = {"S'","S","A","E"};
grammar.terminals = {"$",";", "id", ":=", "+"};
pds::Rule rule;
rule.head = "S'";
rule.tail = {"S","$"};
grammar.rules.push_back(rule);
std::shared_ptr<pds::Rule> rule(new pds::Rule());
rule->head = "S'";
rule->tail = {"S","$"};
grammar.rules.push_back(rule);
rule.head = "S";
rule.tail = {"S",";","A"};
grammar.rules.push_back(rule);
rule->head = "S";
rule->tail = {"S",";","A"};
grammar.rules.push_back(rule);
rule.head = "S";
rule.tail = {"A"};
grammar.rules.push_back(rule);
rule->head = "S";
rule->tail = {"A"};
grammar.rules.push_back(rule);
rule.head = "A";
rule.tail = {"E"};
grammar.rules.push_back(rule);
rule->head = "A";
rule->tail = {"E"};
grammar.rules.push_back(rule);
rule.head = "A";
rule.tail = {"id",":=","E"};
grammar.rules.push_back(rule);
rule->head = "A";
rule->tail = {"id",":=","E"};
grammar.rules.push_back(rule);
rule.head = "E";
rule.tail = {"E","+","id"};
grammar.rules.push_back(rule);
rule->head = "E";
rule->tail = {"E","+","id"};
grammar.rules.push_back(rule);
rule.head = "E";
rule.tail = {"id"};
grammar.rules.push_back(rule);
rule->head = "E";
rule->tail = {"id"};
grammar.rules.push_back(rule);
pds::lr::Generator<pds::lr::SLR1Itemset> g(grammar);
auto tbl = g.generate(); // LRTable
EXPECT_THROW(throw "shift-reduce", std::exception);
/*
// EXPECT_THROW(throw "shift-reduce", std::exception);
/*
std::vector<std::map<std::string, std::pair<pds::lr::Action,std::size_t> >> act {
{ {"id", make_pair(pds::lr::Action::SHIFT,4) } },
{ {";", make_pair(pds::lr::Action::SHIFT,5) },
{"$", make_pair(pds::lr::Action::SHIFT, ACCEPT_STATE)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::REDUCE,3) },
{";", make_pair(pds::lr::Action::REDUCE,3) },
{"+", make_pair(pds::lr::Action::REDUCE,3) },
{":=", make_pair(pds::lr::Action::REDUCE,3) },
{"$", make_pair(pds::lr::Action::REDUCE, 3)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::REDUCE,4) },
{";", make_pair(pds::lr::Action::REDUCE,4) },
{"+", make_pair(pds::lr::Action::ERROR, ERROR_STATE) },
{":=", make_pair(pds::lr::Action::REDUCE,4) },
{"$", make_pair(pds::lr::Action::REDUCE,4)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::REDUCE,7) },
{";", make_pair(pds::lr::Action::REDUCE,7) },
{"+", make_pair(pds::lr::Action::REDUCE,7) },
{":=", make_pair(pds::lr::Action::ERROR, ERROR_STATE) },
{"$", make_pair(pds::lr::Action::REDUCE, 7)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::SHIFT,4) } },
{ {"id", make_pair(pds::lr::Action::SHIFT,9) } },
{ {"id", make_pair(pds::lr::Action::SHIFT, 11) } },
{ {"id", make_pair(pds::lr::Action::REDUCE,2) },
{";", make_pair(pds::lr::Action::REDUCE,2) },
{"+", make_pair(pds::lr::Action::REDUCE,2) },
{":=", make_pair(pds::lr::Action::REDUCE,2) },
{"$", make_pair(pds::lr::Action::REDUCE, 2)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::REDUCE,6) },
{";", make_pair(pds::lr::Action::REDUCE,6) },
{"+", make_pair(pds::lr::Action::REDUCE,6) },
{":=", make_pair(pds::lr::Action::REDUCE,6) },
{"$", make_pair(pds::lr::Action::REDUCE, 6)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::REDUCE,5) },
{";", make_pair(pds::lr::Action::REDUCE,5) },
{"+", make_pair(pds::lr::Action::ERROR, ERROR_STATE) },
{":=", make_pair(pds::lr::Action::REDUCE,5) },
{"$", make_pair(pds::lr::Action::REDUCE,5)}
{ {"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)}
},
{ {"id", make_pair(pds::lr::Action::REDUCE,7) },
{";", make_pair(pds::lr::Action::REDUCE,7) },
{"+", make_pair(pds::lr::Action::REDUCE, 7) },
{":=", make_pair(pds::lr::Action::REDUCE,7) },
{"$", make_pair(pds::lr::Action::REDUCE,7)}
{ {"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)}
},
};
std::vector<std::map<std::string, std::size_t>> goto_ = {
{ {"S",1}, {"A",2}, {"E",3} }, /*state_num:1*/
// { {"A",8}, {"E",3} }, /*state_num:5*/
// { {"E",10} }, /*state_num:7*/
// };
{ {"S",1}, {"A",2}, {"E",3} }, // state_num:1
{ {"A",8}, {"E",3} }, // state_num:5
{ {"E",10} }, // state_num:7
};
// Action: enum {ERROR,SHIFT,REDUCE,ACCEPT}
/*
ASSERT_EQ(goto_.size(),tbl.goto_.size());
ASSERT_EQ(goto_.size(),tbl.goto_.size());
ASSERT_EQ(act.size(),tbl.act.size());
for(std::size_t i = 0; i < goto_.size(); i++) {
@ -130,9 +130,8 @@ TEST(lr0, test0) {
EXPECT_EQ(act[i].second.first,tbl.act[i].second.first);
EXPECT_EQ(act[i].second.second,tbl.act[i].second.second);
}
*/
*/

View File

@ -1,7 +0,0 @@
#include <iostream>
#include "gtest/gtest.h"
TEST(parsodus, temporary) {
std::string parsodus = "awesome";
ASSERT_EQ(parsodus, "awesome");
}