Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Concepts
NPDA.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
26#include <core/xmlApi.hpp>
27#include <automaton/PDA/NPDA.h>
28#include "../common/AutomatonFromXMLParser.h"
29#include "../common/AutomatonToXMLComposer.h"
30
31namespace core {
32
33template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
34struct xmlApi < automaton::NPDA < InputSymbolType, PushdownStoreSymbolType, StateType > > {
42 static std::string xmlTagName ( ) {
43 return "NPDA";
44 }
45
53 static bool first ( const ext::deque < sax::Token >::const_iterator & input ) {
55 }
56
65
73
81
89};
90
91template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
94
95 ext::set<StateType> states = automaton::AutomatonFromXMLParser::parseStates<StateType>(input);
96 ext::set < InputSymbolType > inputSymbols = automaton::AutomatonFromXMLParser::parseInputAlphabet < InputSymbolType > ( input );
97 ext::set<PushdownStoreSymbolType> pushdownStoreSymbols = automaton::AutomatonFromXMLParser::parsePushdownStoreAlphabet<PushdownStoreSymbolType>(input);
98 StateType initialState = automaton::AutomatonFromXMLParser::parseInitialState<StateType>(input);
99 PushdownStoreSymbolType initialPushdownStoreSymbol = automaton::AutomatonFromXMLParser::parseInitialPushdownStoreSymbol<PushdownStoreSymbolType>(input);
100 ext::set<StateType> finalStates = automaton::AutomatonFromXMLParser::parseFinalStates<StateType>(input);
101
102 automaton::NPDA < InputSymbolType, PushdownStoreSymbolType, StateType > automaton(std::move(initialState), std::move(initialPushdownStoreSymbol));
103 automaton.setStates(std::move(states));
104 automaton.setInputAlphabet(std::move(inputSymbols));
105 automaton.setPushdownStoreAlphabet(std::move(pushdownStoreSymbols));
106 automaton.setFinalStates(std::move(finalStates));
107
109
111 return automaton;
112}
113
114template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
117 StateType from = automaton::AutomatonFromXMLParser::parseTransitionFrom<StateType>(input);
118 common::symbol_or_epsilon < InputSymbolType > inputSymbol = automaton::AutomatonFromXMLParser::parseTransitionInputSymbol < common::symbol_or_epsilon < InputSymbolType > > ( input );
119 ext::vector<PushdownStoreSymbolType> pop = automaton::AutomatonFromXMLParser::parseTransitionPop<PushdownStoreSymbolType>(input);
120 StateType to = automaton::AutomatonFromXMLParser::parseTransitionTo<StateType>(input);
121 ext::vector<PushdownStoreSymbolType> push = automaton::AutomatonFromXMLParser::parseTransitionPush<PushdownStoreSymbolType>(input);
123
124 automaton.addTransition(std::move(from), std::move(inputSymbol), std::move(pop), std::move(to), std::move(push));
125}
126
127template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
129 out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT );
130
137 composeTransitions ( out, automaton );
138
139 out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT );
140}
141
142template < class InputSymbolType, class PushdownStoreSymbolType, class StateType >
144 out.emplace_back("transitions", sax::Token::TokenType::START_ELEMENT);
145 for(const auto& transition : automaton.getTransitions()) {
146 out.emplace_back("transition", sax::Token::TokenType::START_ELEMENT);
147
148 automaton::AutomatonToXMLComposer::composeTransitionFrom(out, std::get<0>(transition.first));
149 automaton::AutomatonToXMLComposer::composeTransitionInputSymbol(out, std::get<1>(transition.first));
150 automaton::AutomatonToXMLComposer::composeTransitionPop(out, std::get<2>(transition.first));
151 automaton::AutomatonToXMLComposer::composeTransitionTo(out, transition.second.first);
153
154 out.emplace_back("transition", sax::Token::TokenType::END_ELEMENT);
155 }
156
157 out.emplace_back("transitions", sax::Token::TokenType::END_ELEMENT);
158}
159
160} /* namespace core */
161
static void parseTransitions(ext::deque< sax::Token >::iterator &input, T &automaton)
Definition: AutomatonFromXMLParser.h:114
static void composeTransitionTo(ext::deque< sax::Token > &, const StateType &state)
Definition: AutomatonToXMLComposer.h:252
static void composeStates(ext::deque< sax::Token > &, const ext::set< StateType > &states)
Definition: AutomatonToXMLComposer.h:100
static void composeTransitionFrom(ext::deque< sax::Token > &, const StateType &state)
Definition: AutomatonToXMLComposer.h:259
static void composeInputAlphabet(ext::deque< sax::Token > &, const ext::set< SymbolType > &symbols)
Definition: AutomatonToXMLComposer.h:109
static void composeInitialPushdownStoreSymbol(ext::deque< sax::Token > &, const SymbolType &symbol)
Definition: AutomatonToXMLComposer.h:197
static void composeTransitionPop(ext::deque< sax::Token > &, const ext::vector< SymbolType > &symbols)
Definition: AutomatonToXMLComposer.h:284
static void composeTransitionPush(ext::deque< sax::Token > &, const ext::vector< SymbolType > &symbols)
Definition: AutomatonToXMLComposer.h:300
static void composeFinalStates(ext::deque< sax::Token > &, const ext::set< StateType > &states)
Definition: AutomatonToXMLComposer.h:170
static void composeTransitionInputSymbol(ext::deque< sax::Token > &, const SymbolType &symbol)
Definition: AutomatonToXMLComposer.h:316
static void composeInitialState(ext::deque< sax::Token > &, const StateType &state)
Definition: AutomatonToXMLComposer.h:163
static void composePushdownStoreAlphabet(ext::deque< sax::Token > &, const ext::set< SymbolType > &symbols)
Definition: AutomatonToXMLComposer.h:179
Definition: NPDA.h:74
Definition: symbol_or_epsilon.hpp:24
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
static void popToken(ext::deque< Token >::iterator &input, Token::TokenType type, const std::string &data)
Definition: FromXMLParserHelper.cpp:39
static bool isToken(ext::deque< Token >::const_iterator input, Token::TokenType type, const std::string &data)
Definition: FromXMLParserHelper.cpp:29
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
Definition: ToGrammar.h:31
Definition: normalize.hpp:10
static bool first(const ext::deque< sax::Token >::const_iterator &input)
Tests whether the token stream starts with this type.
Definition: NPDA.h:53
static std::string xmlTagName()
The XML tag name of class.
Definition: NPDA.h:42
Definition: xmlApi.hpp:27