diff --git a/alib2algo/src/automaton/transform/ZAToPDA.cpp b/alib2algo/src/automaton/transform/ZAToPDA.cpp new file mode 100644 index 0000000000000000000000000000000000000000..8af8ba69c628bbd994dec7af47d3e89b66ece32b --- /dev/null +++ b/alib2algo/src/automaton/transform/ZAToPDA.cpp @@ -0,0 +1,8 @@ +#include <registration/AlgoRegistration.hpp> +#include "ZAToPDA.h" + +namespace { + +auto ZAToPDA = registration::AbstractRegister<automaton::transform::ZAToPDA, automaton::NPDA<DefaultSymbolType, DefaultStateType, char>, const automaton::ArcFactoredNondeterministicZAutomaton<DefaultSymbolType, DefaultStateType>&>(automaton::transform::ZAToPDA::convert); + +} /* namespace */ diff --git a/alib2algo/src/automaton/transform/ZAToPDA.h b/alib2algo/src/automaton/transform/ZAToPDA.h new file mode 100644 index 0000000000000000000000000000000000000000..501ebff14200ed2f946c98f3b55bbb1213364c5a --- /dev/null +++ b/alib2algo/src/automaton/transform/ZAToPDA.h @@ -0,0 +1,72 @@ +/* + * This file is part of Algorithms library toolkit. + * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz) + + * Algorithms library toolkit is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + + * Algorithms library toolkit is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + + * You should have received a copy of the GNU General Public License + * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>. + */ + +#pragma once +#include <alphabet/Bar.h> +#include <alphabet/BottomOfTheStack.h> +#include <automaton/PDA/NPDA.h> +#include <automaton/TA/ArcFactoredNondeterministicZAutomaton.h> + +namespace automaton::transform { + +/** + * Transforms real-time height-deterministic pushdown automaton (RHPDA) to a pushdown automaton (PDA). + */ +class ZAToPDA { +public: + /** + * Transformation of a AFNZA to a PDA. + * @param zautomaton automaton to transform + * @return (non) deterministic PDA equivalent to @p automaton reading prefix bar notation + */ + template <class SymbolType, class StateType> + static automaton::NPDA<SymbolType, StateType, char> convert(const automaton::ArcFactoredNondeterministicZAutomaton<SymbolType, StateType>& zautomaton); +}; + +template <class SymbolType, class StateType> +automaton::NPDA<SymbolType, StateType, char> ZAToPDA::convert(const automaton::ArcFactoredNondeterministicZAutomaton<SymbolType, StateType>& zautomaton) +{ + auto bots = alphabet::BottomOfTheStack::instance<StateType>(); + auto bar = alphabet::Bar::instance<SymbolType>(); + + auto inputAlphabet = zautomaton.getInputAlphabet(); + inputAlphabet.insert(bar); + + auto pushdownAlphabet = zautomaton.getStates(); + pushdownAlphabet.insert(bots); + + automaton::NPDA<SymbolType, StateType, char> res({'q', 'r'}, inputAlphabet, pushdownAlphabet, 'q', bots, {'r'}); + + for (const auto& transition : zautomaton.getTransitions()) { + if (transition.first.template is<ext::pair<StateType, StateType>>()) { + const auto& trf = transition.first.template get<ext::pair<StateType, StateType>>(); + res.addTransition('q', bar, {trf.first, trf.second}, 'q', {transition.second}); + } else { + res.addTransition('q', transition.first.template get<SymbolType>(), {}, 'q', {transition.second}); + } + } + + res.addTransition('r', {}, 'q', {}); + for (const auto& finalState : zautomaton.getFinalStates()) { + res.addTransition('q', bar, {finalState, bots}, 'r', {}); + } + + return res; +} + +} /* namespace automaton::transform */