Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
NFTA.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <automaton/TA/NFTA.h>
9#include <core/stringApi.hpp>
10
12
15
16namespace core {
17
18template<class SymbolType, class StateType >
19struct stringApi < automaton::NFTA < SymbolType, StateType > > {
21 static bool first ( ext::istream & input );
22 static void compose ( ext::ostream & output, const automaton::NFTA < SymbolType, StateType > & automaton );
23private:
25 static void composeTransitionsToState(ext::ostream& output, const automaton::NFTA < SymbolType, StateType > & automaton, const StateType & to);
26};
27
28template<class SymbolType, class StateType >
30 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
31
34 }
35
37 throw exception::CommonException("Unrecognised NFTA token.");
38
40
44
46
49 throw exception::CommonException ( "Missing rank" );
50
51 unsigned rank = ext::from_string < unsigned > ( token.value );
52
53 symbols.push_back ( common::ranked_symbol < SymbolType > ( std::move ( symbol ), rank ) );
54
56 }
57
58 ext::set<StateType> finalStates;
60 ext::set<ext::tuple<ext::vector < StateType >, common::ranked_symbol < SymbolType >, StateType > > transitionFunction; // from state, on symbol, to state
61
65 break;
67 continue;
68 else
70
71 parseTransition(input, states, symbols, finalStates, transitionFunction);
73 }
74
76 throw exception::CommonException("Extra data after the automaton.");
77
79
80 res.setInputAlphabet ( ext::set < common::ranked_symbol < SymbolType > > ( symbols.begin ( ), symbols.end ( ) ) );
81 res.setStates(states);
82 res.setFinalStates(finalStates);
83
84 for ( const ext::tuple < ext::vector < StateType >, common::ranked_symbol < SymbolType >, StateType > & transition : transitionFunction )
85 res.addTransition(std::get<1>(transition), std::get<0>(transition), std::get<2>(transition));
86
87 return res;
88}
89
90template<class SymbolType, class StateType >
92 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
93 typename ext::vector < common::ranked_symbol < SymbolType > >::const_iterator iter = symbols.begin();
94
96
97 while ( iter != symbols.end ( ) ) {
100 do {
101 ext::vector < StateType > from = automaton::AutomatonFromStringParserCommon::parseList < StateType > ( input );
102 for ( const StateType & state : from ) {
103 states.insert ( state );
104 }
105 innerTransitionFunction.insert(ext::make_tuple(std::move ( from ), *iter));
106
109 } else {
111 }
112
113 ++iter;
114 }
116
118 states.insert ( to );
119
120 bool initial = false;
121 bool final = false;
122
124 if ( initial )
125 throw exception::CommonException ( "NFTA automata do not have initial states." );
126 if ( final )
127 finalStates.insert ( to );
128
131 throw exception::CommonException("Invalid line format");
132
134
135 for ( ext::tuple < ext::vector < StateType >, common::ranked_symbol < SymbolType > > && innerTransition : ext::make_mover ( innerTransitionFunction ) ) {
136 transitionFunction.insert ( ext::make_tuple ( std::move ( std::get < 0 > ( innerTransition ) ), std::move ( std::get < 1 > ( innerTransition ) ), to ) );
137 }
138}
139
140template<class SymbolType, class StateType >
143}
144
145template<class SymbolType, class StateType >
147 output << "NFTA";
148 for(const auto& symbol : automaton.getInputAlphabet()) {
149 output << " ";
150 core::stringApi < SymbolType >::compose(output, symbol.getSymbol ( ) );
151 output << " " << ext::to_string ( symbol.getRank ( ) );
152 }
153
154 output << std::endl;
155
156 for(const auto& state : automaton.getStates()) {
157 composeTransitionsToState(output, automaton, state);
158
159 output << ' ';
161
162 if(automaton.getFinalStates().find(state) != automaton.getFinalStates().end())
163 output << " >";
164
165 output << std::endl;
166 }
167}
168
169template < class SymbolType, class StateType >
172 auto toStateTransitionsIter = symbolTransitionsToState.begin ( );
173
174 for(const common::ranked_symbol < SymbolType > & inputSymbol : automaton.getInputAlphabet()) {
175 output << ' ';
176 bool first = true;
177 if ( toStateTransitionsIter == symbolTransitionsToState.end ( ) || toStateTransitionsIter->first.first != inputSymbol ) {
178 output << '-';
179 } else while ( toStateTransitionsIter != symbolTransitionsToState.end ( ) && toStateTransitionsIter->first.first == inputSymbol ) {
180 if ( ! first )
181 output << ", ";
182
183 first = false;
184
185 automaton::AutomatonToStringComposerCommon::composeList ( output, toStateTransitionsIter->first.second );
186 ++ toStateTransitionsIter;
187 }
188 }
189}
190
191} /* namespace core */
192
static Token next(ext::istream &input)
Definition: AutomatonFromStringLexer.cpp:12
Nondeterministic finite tree automaton without epsilon transitions. Accepts regular tree languages.
Definition: NFTA.h:72
Definition: ranked_symbol.hpp:20
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
static Token peek(ext::istream &input)
Definition: lexer.hpp:53
static void putback(ext::istream &input, const Token &token)
Definition: lexer.hpp:61
Definition: istream.h:32
Class extending the multimap class from the standard library. Original reason is to allow printing of...
Definition: multimap.hpp:48
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: multimap.hpp:167
auto end() &
Inherited behavior of end for non-const instance.
Definition: multimap.hpp:197
Definition: ostream.h:14
Definition: set.hpp:44
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
auto begin() &
Inherited behavior of begin for non-const instance.
Definition: vector.hpp:125
auto end() &
Inherited behavior of end for non-const instance.
Definition: vector.hpp:155
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
Definition: ToGrammar.h:31
Definition: normalize.hpp:10
reference_mover< T > make_mover(T &param)
Move adaptor construction function specialized to lvalue reference parameter.
Definition: iterator.hpp:468
std::string to_string(const T &value)
To string method designated for objects that can be casted to string.
Definition: string.hpp:131
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
static void initialFinalState(ext::istream &input, bool &rightArrow, bool &leftArrow)
Definition: AutomatonFromStringParserCommon.cpp:10
static void composeList(ext::ostream &output, const ext::vector< Type > &list)
Definition: AutomatonToStringComposerCommon.h:21
Definition: stringApi.hpp:26