diff --git a/aconvert2/src/DotConverter.cpp b/aconvert2/src/DotConverter.cpp index 687c9cfeb1e6d4ac3b55b71944e08de4fd50401a..c0fd64a977cc6034ef59e99c3ae641e3297a5719 100644 --- a/aconvert2/src/DotConverter.cpp +++ b/aconvert2/src/DotConverter.cpp @@ -347,7 +347,7 @@ void DotConverter::convert(const automaton::VisiblyPushdownDPDA& a, std::ostream //Mark initial states out << "node [shape = plaintext, label=\"start\"]; 0; \n"; - out << "0 -> " << a.getInitialState() << ";\n"; + out << "0 -> " << states.find(a.getInitialState())->second << ";\n"; transitions(a, states, out); out << "}"; @@ -415,7 +415,7 @@ void DotConverter::convert(const automaton::RealTimeHeightDeterministicDPDA& a, //Mark initial states out << "node [shape = plaintext, label=\"start\"]; 0; \n"; - out << "0 -> " << a.getInitialState() << ";\n"; + out << "0 -> " << states.find(a.getInitialState())->second << ";\n"; transitions(a, states, out); out << "}"; diff --git a/adeterminize2/src/adeterminize.cpp b/adeterminize2/src/adeterminize.cpp index d557456be80c180a8a807d11fc362811018afe64..3b8086cbb2205a0ec3224ac23138c04631147cd9 100644 --- a/adeterminize2/src/adeterminize.cpp +++ b/adeterminize2/src/adeterminize.cpp @@ -111,14 +111,14 @@ int main(int argc, char** argv) { return 0; } else if (type == TYPE_VPA) { automaton::VisiblyPushdownNPDA npda = alib::DataFactory::fromTokens<automaton::VisiblyPushdownNPDA>(tokens); - automaton::VisiblyPushdownNPDA dpda = determinize::VPADeterminizer::determinize(npda); + automaton::VisiblyPushdownDPDA dpda = determinize::VPADeterminizer::determinize(npda); alib::DataFactory::toStdout(dpda); return 0; } else if (type == TYPE_RHDPDA) { automaton::NPDA npda = alib::DataFactory::fromTokens<automaton::NPDA>(tokens); automaton::RealTimeHeightDeterministicNPDA rhpda = automaton::PDAToRHPDA::convert(npda); - automaton::RealTimeHeightDeterministicNPDA dpda = determinize::HDPDADeterminizer::determinize(rhpda); - automaton::NPDA dpda2 = automaton::RHPDAToPDA::convert(dpda); + automaton::RealTimeHeightDeterministicDPDA dpda = determinize::HDPDADeterminizer::determinize(rhpda); + automaton::DPDA dpda2 = automaton::RHPDAToPDA::convert(dpda); alib::DataFactory::toStdout(dpda2); return 0; } diff --git a/alib2algo/src/automaton/PDAToRHPDA.cpp b/alib2algo/src/automaton/PDAToRHPDA.cpp index 983792c4910defb908c92a9d92165f60857ec2f0..341b79e86e0f0c410a1cac70d54db36b6240bfd7 100644 --- a/alib2algo/src/automaton/PDAToRHPDA.cpp +++ b/alib2algo/src/automaton/PDAToRHPDA.cpp @@ -30,7 +30,9 @@ automaton::RealTimeHeightDeterministicNPDA PDAToRHPDA::convert( const automaton: } automaton::RealTimeHeightDeterministicDPDA PDAToRHPDA::convert( const automaton::DPDA & pda ) { -/* RealTimeHeightDeterministicNPDA res(alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); + automaton::State q0 = automaton::createUniqueState(automaton::State("q0"), pda.getStates()); + + RealTimeHeightDeterministicDPDA res(q0, alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); res.setInputSymbols(pda.getInputAlphabet()); res.setStates(pda.getStates()); @@ -39,62 +41,55 @@ automaton::RealTimeHeightDeterministicDPDA PDAToRHPDA::convert( const automaton: stackSymbols.insert( alphabet::Symbol { alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK } ); res.setStackSymbols(stackSymbols); - automaton::State q0 = automaton::createUniqueState(automaton::State("q0"), res.getStates()); - res.addState(q0); - res.addInitialState(q0); - - for(const automaton::State& state : pda.getInitialStates()) - for(const alphabet::Symbol& symbol : pda.getInitialSymbols()) - res.addCallTransition(q0, state, symbol); + res.addCallTransition(q0, pda.getInitialState(), pda.getInitialSymbol()); std::string us("us"); int i = 0; for(const auto& transition : pda.getTransitions()) { - for(const auto& to : transition.second) { - if(std::get<2>(transition.first).size() == 0 && to.second.size() == 0) - res.addLocalTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first); - else if(std::get<2>(transition.first).size() == 1 && to.second.size() == 0) - res.addReturnTransition(std::get<0>(transition.first), std::get<1>(transition.first), std::get<2>(transition.first)[0], to.first); - else if(std::get<2>(transition.first).size() == 0 && to.second.size() == 1) - res.addCallTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first, to.second[0]); - else { - int popPushIndex = 0; - int popPushSymbols = std::get<2>(transition.first).size() + to.second.size(); - - automaton::State lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(i)), res.getStates()); - for(const alphabet::Symbol& pop : std::get<2>(transition.first)) { - automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; - if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); - automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; - - res.addState(fromState); - res.addState(toState); - - if(popPushIndex == 0) - res.addReturnTransition(fromState, std::get<1>(transition.first), pop, toState); - else - res.addReturnTransition(fromState, pop, toState); - popPushIndex++; - } - for(const alphabet::Symbol& push : to.second) { - automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; - if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); - automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; - - res.addState(fromState); - res.addState(toState); - - if(popPushIndex == 0) - res.addCallTransition(fromState, std::get<1>(transition.first), toState, push); - else - res.addCallTransition(fromState, toState, push); - popPushIndex++; - } + const auto& to = transition.second; + if(std::get<2>(transition.first).size() == 0 && to.second.size() == 0) + res.addLocalTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first); + else if(std::get<2>(transition.first).size() == 1 && to.second.size() == 0) + res.addReturnTransition(std::get<0>(transition.first), std::get<1>(transition.first), std::get<2>(transition.first)[0], to.first); + else if(std::get<2>(transition.first).size() == 0 && to.second.size() == 1) + res.addCallTransition(std::get<0>(transition.first), std::get<1>(transition.first), to.first, to.second[0]); + else { + int popPushIndex = 0; + int popPushSymbols = std::get<2>(transition.first).size() + to.second.size(); + + automaton::State lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(i)), res.getStates()); + for(const alphabet::Symbol& pop : std::get<2>(transition.first)) { + automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; + if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); + automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; + + res.addState(fromState); + res.addState(toState); + + if(popPushIndex == 0) + res.addReturnTransition(fromState, std::get<1>(transition.first), pop, toState); + else + res.addReturnTransition(fromState, pop, toState); + popPushIndex++; + } + for(const alphabet::Symbol& push : to.second) { + automaton::State fromState = (popPushIndex == 0) ? std::get<0>(transition.first) : lastUS; + if(popPushIndex != 0) lastUS = automaton::createUniqueState(automaton::State(us + std::to_string(++i)), res.getStates()); + automaton::State toState = (popPushIndex == popPushSymbols - 1) ? to.first : lastUS; + + res.addState(fromState); + res.addState(toState); + + if(popPushIndex == 0) + res.addCallTransition(fromState, std::get<1>(transition.first), toState, push); + else + res.addCallTransition(fromState, toState, push); + popPushIndex++; } } } - return res;*/ + return res; } automaton::RealTimeHeightDeterministicNPDA PDAToRHPDA::convert( const automaton::NPDA & pda ) { diff --git a/alib2algo/src/automaton/RHPDAToPDA.cpp b/alib2algo/src/automaton/RHPDAToPDA.cpp index 2651f4334919bfb9e669cdc9eebfb2ddaa469b2c..d4beb955c9bed3fa6ddd721bf3ce70174a8c1bb3 100644 --- a/alib2algo/src/automaton/RHPDAToPDA.cpp +++ b/alib2algo/src/automaton/RHPDAToPDA.cpp @@ -23,56 +23,44 @@ namespace automaton { automaton::DPDA RHPDAToPDA::convert( const automaton::RealTimeHeightDeterministicDPDA & pda ) { -/* automaton::NPDA res; - - res.setInputSymbols(pda.getInputAlphabet()); - - res.setStackSymbols(pda.getStackAlphabet()); - std::map<std::tuple<automaton::State, alphabet::Symbol, std::vector<alphabet::Symbol>>, std::set<std::pair<automaton::State, std::vector<alphabet::Symbol>>>> readingTransitions; std::map<automaton::State, std::set<std::tuple<std::vector<alphabet::Symbol>, automaton::State, std::vector<alphabet::Symbol>>>> epsilonTransitions; for(const auto& transition : pda.getCallTransitions()) { if(std::get<1>(transition.first).is<string::Epsilon>()) { auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - for(const auto& to : transition.second) { - if(to.second.getData() == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK) throw exception::AlibException("Cannot convert"); - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to.first, std::vector<alphabet::Symbol>{to.second})); - } + const auto& to = transition.second; + if(to.second.getData() == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK) throw exception::AlibException("Cannot convert"); + epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to.first, std::vector<alphabet::Symbol>{to.second})); } else { auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{})]; - for(const auto& to : transition.second) { - readT.insert(std::make_pair(to.first, std::vector<alphabet::Symbol>{to.second})); - } + const auto& to = transition.second; + readT.insert(std::make_pair(to.first, std::vector<alphabet::Symbol>{to.second})); } } for(const auto& transition : pda.getLocalTransitions()) { if(std::get<1>(transition.first).is<string::Epsilon>()) { auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - for(const auto& to : transition.second) { - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to, std::vector<alphabet::Symbol>{})); - } + const auto& to = transition.second; + epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{}, to, std::vector<alphabet::Symbol>{})); } else { auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{})]; - for(const auto& to : transition.second) { - readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); - } + const auto& to = transition.second; + readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); } } for(const auto& transition : pda.getReturnTransitions()) { if(std::get<1>(transition.first).is<string::Epsilon>()) { auto& epsT = epsilonTransitions[std::get<0>(transition.first)]; - for(const auto& to : transition.second) { - if(std::get<2>(transition.first).getData() == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK) throw exception::AlibException("Cannot convert"); - epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{std::get<2>(transition.first)}, to, std::vector<alphabet::Symbol>{})); - } + const auto& to = transition.second; + if(std::get<2>(transition.first).getData() == alphabet::BottomOfTheStackSymbol::BOTTOM_OF_THE_STACK) throw exception::AlibException("Cannot convert"); + epsT.insert(std::make_tuple(std::vector<alphabet::Symbol>{std::get<2>(transition.first)}, to, std::vector<alphabet::Symbol>{})); } else { auto& readT = readingTransitions[std::make_tuple(std::get<0>(transition.first), std::get<1>(transition.first).get<alphabet::Symbol>(), std::vector<alphabet::Symbol>{std::get<2>(transition.first)})]; - for(const auto& to : transition.second) { - readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); - } + const auto& to = transition.second; + readT.insert(std::make_pair(to, std::vector<alphabet::Symbol>{})); } } @@ -80,45 +68,46 @@ automaton::DPDA RHPDAToPDA::convert( const automaton::RealTimeHeightDeterministi if(st.second.size() != 1) throw exception::AlibException("Temporary states has more than one leaving transition"); } - for(const auto& st : readingTransitions) { - for(const auto& to : st.second) { - std::vector<alphabet::Symbol> pops; - std::vector<alphabet::Symbol> pushes; + if(epsilonTransitions[pda.getInitialState()].empty()) { + throw exception::AlibException("Cannot determine initial pushdown store symbol"); + } - pops.insert(pops.end(), std::get<2>(st.first).begin(), std::get<2>(st.first).end()); - pushes.insert(pushes.end(), to.second.begin(), to.second.end()); + const auto& st = *epsilonTransitions[pda.getInitialState()].begin(); - automaton::State toState = to.first; - while(!epsilonTransitions[toState].empty()) { - const auto& epsilonT = *epsilonTransitions[toState].begin(); + std::vector<alphabet::Symbol> pops; + std::vector<alphabet::Symbol> pushes; - pops.insert(pops.end(), std::get<0>(epsilonT).begin(), std::get<0>(epsilonT).end()); - pushes.insert(pushes.end(), std::get<2>(epsilonT).begin(), std::get<2>(epsilonT).end()); + pops.insert(pops.end(), std::get<0>(st).begin(), std::get<0>(st).end()); + pushes.insert(pushes.end(), std::get<2>(st).begin(), std::get<2>(st).end()); - toState = std::get<1>(epsilonT); - } + automaton::State toState = std::get<1>(st); + while(!epsilonTransitions[toState].empty()) { + const auto& epsilonT = *epsilonTransitions[toState].begin(); - res.addState(std::get<0>(st.first)); - res.addState(toState); + pops.insert(pops.end(), std::get<0>(epsilonT).begin(), std::get<0>(epsilonT).end()); + pushes.insert(pushes.end(), std::get<2>(epsilonT).begin(), std::get<2>(epsilonT).end()); - res.addTransition(std::get<0>(st.first), std::get<1>(st.first), pops, toState, pushes); - } + toState = std::get<1>(epsilonT); } - if(pda.getInitialStates().size() != 1) + if(pops.size() != 0 && pushes.size() != 1) throw exception::AlibException("Cannot convert"); - for(const automaton::State& initialState : pda.getInitialStates()) { - if(!epsilonTransitions[initialState].empty()) { - const auto& st = *epsilonTransitions[initialState].begin(); + automaton::DPDA res(toState, pushes[0]); + res.setInputSymbols(pda.getInputAlphabet()); + + res.setStackSymbols(pda.getStackAlphabet()); + + for(const auto& st : readingTransitions) { + for(const auto& to : st.second) { std::vector<alphabet::Symbol> pops; std::vector<alphabet::Symbol> pushes; - pops.insert(pops.end(), std::get<0>(st).begin(), std::get<0>(st).end()); - pushes.insert(pushes.end(), std::get<2>(st).begin(), std::get<2>(st).end()); + pops.insert(pops.end(), std::get<2>(st.first).begin(), std::get<2>(st.first).end()); + pushes.insert(pushes.end(), to.second.begin(), to.second.end()); - automaton::State toState = std::get<1>(st); + automaton::State toState = to.first; while(!epsilonTransitions[toState].empty()) { const auto& epsilonT = *epsilonTransitions[toState].begin(); @@ -127,21 +116,20 @@ automaton::DPDA RHPDAToPDA::convert( const automaton::RealTimeHeightDeterministi toState = std::get<1>(epsilonT); } - res.addState(toState); - if(pops.size() != 0 && pushes.size() != 1) - throw exception::AlibException("Cannot convert"); + res.addState(std::get<0>(st.first)); + res.addState(toState); - res.setInitialStates({toState}); - res.setInitialSymbols({pushes[0]}); + res.addTransition(std::get<0>(st.first), std::get<1>(st.first), pops, toState, pushes); } } res.setFinalStates(pda.getFinalStates()); - return res;*/ + return res; } +//This may not work correctly -- generation of initial state and initial symbol automaton::NPDA RHPDAToPDA::convert( const automaton::RealTimeHeightDeterministicNPDA & pda ) { automaton::NPDA res; diff --git a/alib2algo/src/determinize/common/RHDPDACommon.cpp b/alib2algo/src/determinize/common/RHDPDACommon.cpp index 00dbd3b3c073ca517cc33a3e89f090c4e1d07805..d309b75f55b89f66a83bba54d682f11ae41f90c9 100644 --- a/alib2algo/src/determinize/common/RHDPDACommon.cpp +++ b/alib2algo/src/determinize/common/RHDPDACommon.cpp @@ -5,7 +5,9 @@ #include "object/Object.h" #include "alphabet/LabeledSymbol.h" +#include "automaton/PDA/VisiblyPushdownDPDA.h" #include "automaton/PDA/VisiblyPushdownNPDA.h" +#include "automaton/PDA/RealTimeHeightDeterministicDPDA.h" #include "automaton/PDA/RealTimeHeightDeterministicNPDA.h" namespace determinize { @@ -83,8 +85,8 @@ std::set<std::pair<label::Label, label::Label>> createIdentity(std::set<label::L return id; } -template<class T> -const automaton::State* existsDirtyState(const T& d, const T& n) { +template<class T, class R> +const automaton::State* existsDirtyState(const T& d, const R& n) { for(const automaton::State& state : d.getStates()) { const label::Label& stateLabel = state.getName(); const std::set<label::Label> dSubSet = retrieveDSubSet(unpackFromStateLabel(stateLabel)); @@ -122,8 +124,8 @@ continue2: while(false); //TODO remove this return NULL; } -template const automaton::State* existsDirtyState(const automaton::VisiblyPushdownNPDA& d, const automaton::VisiblyPushdownNPDA& n); -template const automaton::State* existsDirtyState(const automaton::RealTimeHeightDeterministicNPDA& d, const automaton::RealTimeHeightDeterministicNPDA& n); +template const automaton::State* existsDirtyState(const automaton::VisiblyPushdownDPDA& d, const automaton::VisiblyPushdownNPDA& n); +template const automaton::State* existsDirtyState(const automaton::RealTimeHeightDeterministicDPDA& d, const automaton::RealTimeHeightDeterministicNPDA& n); template<class T> void localClosure(std::set<label::Label>& states, const std::set<label::Label>& oldStates, const T& d) { @@ -131,7 +133,7 @@ void localClosure(std::set<label::Label>& states, const std::set<label::Label>& for(const label::Label& state : oldStates) { for(const auto& transition : d.getLocalTransitions()) { - if(transition.second.begin()->getName() != state) continue; + if(transition.second.getName() != state) continue; if(!states.count(transition.first.first.getName())) { states.insert(transition.first.first.getName()); @@ -140,7 +142,7 @@ void localClosure(std::set<label::Label>& states, const std::set<label::Label>& } for(const auto& transition : d.getReturnTransitions()) { - if(transition.second.begin()->getName() != state) continue; + if(transition.second.getName() != state) continue; const alphabet::Symbol& popSymbol = std::get<2>(transition.first); if(popSymbol == d.getBottomOfTheStackSymbol()) continue; @@ -158,11 +160,11 @@ void localClosure(std::set<label::Label>& states, const std::set<label::Label>& localClosure(states, newStates, d); } -template void localClosure(std::set<label::Label>& states, const std::set<label::Label>& oldStates, const automaton::VisiblyPushdownNPDA& d); -template void localClosure(std::set<label::Label>& states, const std::set<label::Label>& oldStates, const automaton::RealTimeHeightDeterministicNPDA& d); +template void localClosure(std::set<label::Label>& states, const std::set<label::Label>& oldStates, const automaton::VisiblyPushdownDPDA& d); +template void localClosure(std::set<label::Label>& states, const std::set<label::Label>& oldStates, const automaton::RealTimeHeightDeterministicDPDA& d); -template<class T> -std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const T& d, const T& n) { +template<class T, class R> +std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const T& d, const R& n) { for(const automaton::State& state : d.getStates()) { const label::Label& stateLabel = state.getName(); @@ -186,14 +188,13 @@ break2: while(false); std::set<alphabet::Symbol> topSymbols; for(const label::Label& localState : lc) { for(const auto& transition : d.getCallTransitions()) { - for(const auto& to : transition.second) { - if(localState != to.first.getName()) continue; + const auto& to = transition.second; + if(localState != to.first.getName()) continue; - topSymbols.insert(to.second); - } + topSymbols.insert(to.second); } - if(d.getInitialStates().begin()->getName() == localState) { + if(d.getInitialState().getName() == localState) { topSymbols.insert(d.getBottomOfTheStackSymbol()); } } @@ -209,8 +210,8 @@ break2: while(false); return NULL; } -template std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const automaton::VisiblyPushdownNPDA& d, const automaton::VisiblyPushdownNPDA& n); -template std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const automaton::RealTimeHeightDeterministicNPDA& d, const automaton::RealTimeHeightDeterministicNPDA& n); +template std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const automaton::VisiblyPushdownDPDA& d, const automaton::VisiblyPushdownNPDA& n); +template std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const automaton::RealTimeHeightDeterministicDPDA& d, const automaton::RealTimeHeightDeterministicNPDA& n); std::set<label::Label> retrieveLabels(const std::set<automaton::State>& states) { std::set<label::Label> labels; diff --git a/alib2algo/src/determinize/common/RHDPDACommon.h b/alib2algo/src/determinize/common/RHDPDACommon.h index 765306ab46385beb6206b36c14ec8bc3e6c4cbb2..26fff9071ec8806e4ad8340cb4b3abbdfac6fb3e 100644 --- a/alib2algo/src/determinize/common/RHDPDACommon.h +++ b/alib2algo/src/determinize/common/RHDPDACommon.h @@ -27,14 +27,14 @@ std::set<label::Label> retrieveDSubSet(const std::set<std::pair<label::Label, la std::set<std::pair<label::Label, label::Label>> createIdentity(std::set<label::Label>&& states); -template<class T> -const automaton::State* existsDirtyState(const T& d, const T& n); +template<class T, class R> +const automaton::State* existsDirtyState(const T& d, const R& n); template<class T> void localClosure(std::set<label::Label>& states, const std::set<label::Label>& oldStates, const T& d); -template<class T> -std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const T& d, const T& n); +template<class T, class R> +std::pair<automaton::State, alphabet::Symbol>* existsDirtyStateSymbol(const T& d, const R& n); std::set<label::Label> retrieveLabels(const std::set<automaton::State>& states); diff --git a/alib2algo/src/determinize/hdpda/HDPDADeterminizer.cpp b/alib2algo/src/determinize/hdpda/HDPDADeterminizer.cpp index cde68385e783891ab3eaec47c74153bedb53cc0d..9c01e6c99210bdf890807acd1edeeb33804aa5d3 100644 --- a/alib2algo/src/determinize/hdpda/HDPDADeterminizer.cpp +++ b/alib2algo/src/determinize/hdpda/HDPDADeterminizer.cpp @@ -11,7 +11,7 @@ namespace determinize { -void addRetTransition(const automaton::State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& dvpdaSymbol, const automaton::State& to, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void addRetTransition(const automaton::State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const alphabet::Symbol& dvpdaSymbol, const automaton::State& to, automaton::RealTimeHeightDeterministicDPDA& deterministic) { deterministic.addState(from); deterministic.addState(to); deterministic.addStackSymbol(dvpdaSymbol); @@ -19,7 +19,7 @@ void addRetTransition(const automaton::State& from, const std::variant<string::E deterministic.addReturnTransition(from, input, dvpdaSymbol, to); } -void retInitial(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void retInitial(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::set<std::pair<label::Label, label::Label>> S1; @@ -42,7 +42,7 @@ void retInitial(const automaton::State& state, const alphabet::Symbol& pdaSymbol addRetTransition(state, input, pdaSymbol, automaton::State(packToStateLabel(std::move(S1))), deterministic); } -void ret(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void ret(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::pair<std::set<std::pair<label::Label, label::Label>>, std::variant<string::Epsilon, alphabet::Symbol>> pdaSymbolUnpack = unpackFromDRHDPDAStackSymbolLabel(static_cast<const alphabet::LabeledSymbol&>(pdaSymbol.getData()).getLabel()); const std::set<std::pair<label::Label, label::Label>>& S1 = pdaSymbolUnpack.first; @@ -92,7 +92,7 @@ void ret(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const addRetTransition(state, input, pdaSymbol, automaton::State(packToStateLabel(std::move(S2))), deterministic); } -void addCallTransition(const automaton::State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::State& to, const alphabet::Symbol& dvpdaSymbol, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void addCallTransition(const automaton::State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::State& to, const alphabet::Symbol& dvpdaSymbol, automaton::RealTimeHeightDeterministicDPDA& deterministic) { deterministic.addState(from); deterministic.addState(to); deterministic.addStackSymbol(dvpdaSymbol); @@ -100,7 +100,7 @@ void addCallTransition(const automaton::State& from, const std::variant<string:: deterministic.addCallTransition(from, input, to, dvpdaSymbol); } -void call(const automaton::State& state, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void call(const automaton::State& state, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::set<label::Label> R = retrieveDSubSet(S); @@ -122,14 +122,14 @@ void call(const automaton::State& state, const std::variant<string::Epsilon, alp addCallTransition(state, input, automaton::State(packToStateLabel(createIdentity(std::move(R1)))), alphabet::Symbol(alphabet::LabeledSymbol(packToStackSymbolLabel(std::make_pair(std::move(S), input)))), deterministic); } -void addLocalTransition(const automaton::State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::State& to, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void addLocalTransition(const automaton::State& from, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::State& to, automaton::RealTimeHeightDeterministicDPDA& deterministic) { deterministic.addState(from); deterministic.addState(to); deterministic.addLocalTransition(from, input, to); } -void local(const automaton::State& state, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicNPDA& deterministic) { +void local(const automaton::State& state, const std::variant<string::Epsilon, alphabet::Symbol>& input, const automaton::RealTimeHeightDeterministicNPDA& nondeterministic, automaton::RealTimeHeightDeterministicDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::set<std::pair<label::Label, label::Label>> S1; @@ -185,13 +185,11 @@ std::tuple<std::set<std::variant<string::Epsilon, alphabet::Symbol>>, std::set<s return std::make_tuple(local, call, ret); } -automaton::RealTimeHeightDeterministicNPDA HDPDADeterminizer::determinize(const automaton::RealTimeHeightDeterministicNPDA& n) { - automaton::RealTimeHeightDeterministicNPDA d(n.getBottomOfTheStackSymbol()); - d.setInputSymbols(n.getInputAlphabet()); - +automaton::RealTimeHeightDeterministicDPDA HDPDADeterminizer::determinize(const automaton::RealTimeHeightDeterministicNPDA& n) { label::Label initialLabel = packToStateLabel(createIdentity(retrieveLabels(n.getInitialStates()))); - d.addState(automaton::State(initialLabel)); - d.addInitialState(automaton::State(initialLabel)); + + automaton::RealTimeHeightDeterministicDPDA d(automaton::State(initialLabel), n.getBottomOfTheStackSymbol()); + d.setInputSymbols(n.getInputAlphabet()); for(;;) { std::pair<automaton::State, alphabet::Symbol>* stateSymbol = existsDirtyStateSymbol(d, n); diff --git a/alib2algo/src/determinize/hdpda/HDPDADeterminizer.h b/alib2algo/src/determinize/hdpda/HDPDADeterminizer.h index 97caaf125f3a3887d8599d12a481f575e2ce9174..2d867dcdd04578899e268dbda81b5afe9013f7ca 100644 --- a/alib2algo/src/determinize/hdpda/HDPDADeterminizer.h +++ b/alib2algo/src/determinize/hdpda/HDPDADeterminizer.h @@ -2,6 +2,7 @@ #define HDPDA_DETERMINIZER_H_ #include "automaton/PDA/RealTimeHeightDeterministicNPDA.h" +#include "automaton/PDA/RealTimeHeightDeterministicDPDA.h" namespace determinize { @@ -15,7 +16,7 @@ public: * * @return deterministic visibly pushdown automaton */ - static automaton::RealTimeHeightDeterministicNPDA determinize(const automaton::RealTimeHeightDeterministicNPDA& nondeterministic); + static automaton::RealTimeHeightDeterministicDPDA determinize(const automaton::RealTimeHeightDeterministicNPDA& nondeterministic); }; diff --git a/alib2algo/src/determinize/vpa/VPADeterminizer.cpp b/alib2algo/src/determinize/vpa/VPADeterminizer.cpp index d36257bc41a8ee50bf1e48e431321bbdfd2e9054..136cf49c2c8a96427fbc5dd6be3b9bde7a2ef656 100644 --- a/alib2algo/src/determinize/vpa/VPADeterminizer.cpp +++ b/alib2algo/src/determinize/vpa/VPADeterminizer.cpp @@ -10,7 +10,7 @@ namespace determinize { -void addRetTransition(const automaton::State& from, const alphabet::Symbol& input, const alphabet::Symbol& dvpdaSymbol, const automaton::State& to, automaton::VisiblyPushdownNPDA& deterministic) { +void addRetTransition(const automaton::State& from, const alphabet::Symbol& input, const alphabet::Symbol& dvpdaSymbol, const automaton::State& to, automaton::VisiblyPushdownDPDA& deterministic) { deterministic.addState(from); deterministic.addState(to); deterministic.addStackSymbol(dvpdaSymbol); @@ -18,7 +18,7 @@ void addRetTransition(const automaton::State& from, const alphabet::Symbol& inpu deterministic.addReturnTransition(from, input, dvpdaSymbol, to); } -void retInitial(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownNPDA& deterministic) { +void retInitial(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::set<std::pair<label::Label, label::Label>> S1; @@ -41,7 +41,7 @@ void retInitial(const automaton::State& state, const alphabet::Symbol& pdaSymbol addRetTransition(state, input, pdaSymbol, automaton::State(packToStateLabel(std::move(S1))), deterministic); } -void ret(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownNPDA& deterministic) { +void ret(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::pair<std::set<std::pair<label::Label, label::Label>>, alphabet::Symbol> pdaSymbolUnpack = unpackFromDVPAStackSymbolLabel(static_cast<const alphabet::LabeledSymbol&>(pdaSymbol.getData()).getLabel()); const std::set<std::pair<label::Label, label::Label>>& S1 = pdaSymbolUnpack.first; @@ -91,7 +91,7 @@ void ret(const automaton::State& state, const alphabet::Symbol& pdaSymbol, const addRetTransition(state, input, pdaSymbol, automaton::State(packToStateLabel(std::move(S2))), deterministic); } -void addCallTransition(const automaton::State& from, const alphabet::Symbol& input, const automaton::State& to, const alphabet::Symbol& dvpdaSymbol, automaton::VisiblyPushdownNPDA& deterministic) { +void addCallTransition(const automaton::State& from, const alphabet::Symbol& input, const automaton::State& to, const alphabet::Symbol& dvpdaSymbol, automaton::VisiblyPushdownDPDA& deterministic) { deterministic.addState(from); deterministic.addState(to); deterministic.addStackSymbol(dvpdaSymbol); @@ -99,7 +99,7 @@ void addCallTransition(const automaton::State& from, const alphabet::Symbol& inp deterministic.addCallTransition(from, input, to, dvpdaSymbol); } -void call(const automaton::State& state, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownNPDA& deterministic) { +void call(const automaton::State& state, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::set<label::Label> R = retrieveDSubSet(S); @@ -121,14 +121,14 @@ void call(const automaton::State& state, const alphabet::Symbol& input, const au addCallTransition(state, input, automaton::State(packToStateLabel(createIdentity(std::move(R1)))), alphabet::Symbol(alphabet::LabeledSymbol(packToStackSymbolLabel(std::make_pair(std::move(S), input)))), deterministic); } -void addLocalTransition(const automaton::State& from, const alphabet::Symbol& input, const automaton::State& to, automaton::VisiblyPushdownNPDA& deterministic) { +void addLocalTransition(const automaton::State& from, const alphabet::Symbol& input, const automaton::State& to, automaton::VisiblyPushdownDPDA& deterministic) { deterministic.addState(from); deterministic.addState(to); deterministic.addLocalTransition(from, input, to); } -void local(const automaton::State& state, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownNPDA& deterministic) { +void local(const automaton::State& state, const alphabet::Symbol& input, const automaton::VisiblyPushdownNPDA& nondeterministic, automaton::VisiblyPushdownDPDA& deterministic) { std::set<std::pair<label::Label, label::Label>> S = unpackFromStateLabel(state.getName()); std::set<std::pair<label::Label, label::Label>> S1; @@ -150,16 +150,14 @@ void local(const automaton::State& state, const alphabet::Symbol& input, const a addLocalTransition(state, input, automaton::State(packToStateLabel(std::move(S1))), deterministic); } -automaton::VisiblyPushdownNPDA VPADeterminizer::determinize(const automaton::VisiblyPushdownNPDA& n) { - automaton::VisiblyPushdownNPDA d(n.getBottomOfTheStackSymbol()); +automaton::VisiblyPushdownDPDA VPADeterminizer::determinize(const automaton::VisiblyPushdownNPDA& n) { + label::Label initialLabel = packToStateLabel(createIdentity(retrieveLabels(n.getInitialStates()))); + + automaton::VisiblyPushdownDPDA d(automaton::State(initialLabel), n.getBottomOfTheStackSymbol()); d.setCallInputSymbols(n.getCallInputAlphabet()); d.setLocalInputSymbols(n.getLocalInputAlphabet()); d.setReturnInputSymbols(n.getReturnInputAlphabet()); - label::Label initialLabel = packToStateLabel(createIdentity(retrieveLabels(n.getInitialStates()))); - d.addState(automaton::State(initialLabel)); - d.addInitialState(automaton::State(initialLabel)); - for(;;) { std::pair<automaton::State, alphabet::Symbol>* stateSymbol = existsDirtyStateSymbol(d, n); const automaton::State* state = existsDirtyState(d, n); diff --git a/alib2algo/src/determinize/vpa/VPADeterminizer.h b/alib2algo/src/determinize/vpa/VPADeterminizer.h index d30578612988e01d0cbde55bd8d7f32f057f5b75..f5a7dc31384c162e4fa52e7aacbf4b2dba326415 100644 --- a/alib2algo/src/determinize/vpa/VPADeterminizer.h +++ b/alib2algo/src/determinize/vpa/VPADeterminizer.h @@ -2,6 +2,7 @@ #define VPADETERMINIZER_H_ #include "automaton/PDA/VisiblyPushdownNPDA.h" +#include "automaton/PDA/VisiblyPushdownDPDA.h" namespace determinize { @@ -15,7 +16,7 @@ public: * * @return deterministic visibly pushdown automaton */ - static automaton::VisiblyPushdownNPDA determinize(const automaton::VisiblyPushdownNPDA& nondeterministic); + static automaton::VisiblyPushdownDPDA determinize(const automaton::VisiblyPushdownNPDA& nondeterministic); }; diff --git a/alib2data/src/automaton/AutomatonFromXMLParser.cpp b/alib2data/src/automaton/AutomatonFromXMLParser.cpp index 597a5871d04fc894c0dc6bb2a21238a91b1fc44b..2c75a1e0c4826c24d72dc079f1e90ae494d54956 100644 --- a/alib2data/src/automaton/AutomatonFromXMLParser.cpp +++ b/alib2data/src/automaton/AutomatonFromXMLParser.cpp @@ -16,7 +16,7 @@ namespace automaton { Automaton AutomatonFromXMLParser::parseAutomaton(std::list<sax::Token> &input) const { - return parseAutomaton(input, std::set<FEATURES>({FEATURES::AUTOMATON, FEATURES::EPSILON_NFA, FEATURES::NFA, FEATURES::DFA, FEATURES::COMPACT_NFA, FEATURES::EXTENDED_NFA, FEATURES::DPDA, FEATURES::SINGLE_POP_DPDA, FEATURES::INPUT_DRIVEN_NPDA, FEATURES::VISIBLY_PUSHDOWN_NPDA, FEATURES::REAL_TIME_HEIGHT_DETERMINISTIC_NPDA, FEATURES::NPDA, FEATURES::SINGLE_POP_NPDA, FEATURES::ONE_TAPE_DTM})); + return parseAutomaton(input, std::set<FEATURES>({FEATURES::AUTOMATON, FEATURES::EPSILON_NFA, FEATURES::NFA, FEATURES::DFA, FEATURES::COMPACT_NFA, FEATURES::EXTENDED_NFA, FEATURES::DPDA, FEATURES::SINGLE_POP_DPDA, FEATURES::INPUT_DRIVEN_NPDA, FEATURES::VISIBLY_PUSHDOWN_DPDA, FEATURES::VISIBLY_PUSHDOWN_NPDA, FEATURES::REAL_TIME_HEIGHT_DETERMINISTIC_DPDA, FEATURES::REAL_TIME_HEIGHT_DETERMINISTIC_NPDA, FEATURES::NPDA, FEATURES::SINGLE_POP_NPDA, FEATURES::ONE_TAPE_DTM})); } Automaton AutomatonFromXMLParser::parseAutomaton(std::list<sax::Token>& input, const std::set<FEATURES>& features) const { @@ -69,11 +69,11 @@ Automaton AutomatonFromXMLParser::parseAutomaton(std::list<sax::Token>& input, c if(!features.count(FEATURES::ONE_TAPE_DTM)) throw exception::AlibException(); return Automaton(parseOneTapeDTM(input)); } else - throw sax::ParserException(sax::Token("Automaton / EpsilonNFA / NFA / DFA / CompactNFA / ExtendedNFA / DPDA / SinglePopDPDA / InputDrivenNPDA / VisiblyPushdownNPDA / RealTimeHeightDeterministicNPDA / NPDA / SinglePopNPDA / OneTapeDTM", sax::Token::TokenType::START_ELEMENT), input.front()); + throw sax::ParserException(sax::Token("Automaton / EpsilonNFA / NFA / DFA / CompactNFA / ExtendedNFA / DPDA / SinglePopDPDA / InputDrivenNPDA / VisiblyPushdownDPDA / VisiblyPushdownNPDA / RealTimeHeightDeterministicDPDA / RealTimeHeightDeterministicNPDA / NPDA / SinglePopNPDA / OneTapeDTM", sax::Token::TokenType::START_ELEMENT), input.front()); } bool AutomatonFromXMLParser::first(std::list<sax::Token>& input) const { - if(isToken(input, sax::Token::TokenType::START_ELEMENT, "automaton") || isToken(input, sax::Token::TokenType::START_ELEMENT, "EpsilonNFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "NFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "DFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "CompactNFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "ExtendedNFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "DPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "SinglePopDPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "InputDrivenNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "VisiblyPushdownNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "RealTimeHeightDeterministicNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "NPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "SinglePopNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "OneTapeDTM")) { + if(isToken(input, sax::Token::TokenType::START_ELEMENT, "automaton") || isToken(input, sax::Token::TokenType::START_ELEMENT, "EpsilonNFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "NFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "DFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "CompactNFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "ExtendedNFA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "DPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "SinglePopDPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "InputDrivenNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "VisiblyPushdownDPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "VisiblyPushdownNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "RealTimeHeightDeterministicDPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "RealTimeHeightDeterministicNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "NPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "SinglePopNPDA") || isToken(input, sax::Token::TokenType::START_ELEMENT, "OneTapeDTM")) { return true; } else { return false;