Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
MultiInitialStateEpsilonNFA.h
Go to the documentation of this file.
1
6#pragma once
7
9#include <core/stringApi.hpp>
10
12
14
15namespace core {
16
17template<class SymbolType, class StateType >
18struct stringApi < automaton::MultiInitialStateEpsilonNFA < SymbolType, StateType > > {
20 static bool first ( ext::istream & input );
22private:
24 static void composeTransitionsFromState(ext::ostream& output, const automaton::MultiInitialStateEpsilonNFA < SymbolType, StateType > & automaton, const StateType & from);
25};
26
27template<class SymbolType, class StateType >
30
31 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
32
35 }
36
38 throw exception::CommonException("Unrecognised MISENFA token.");
39 }
41
45 symbols.push_back ( common::symbol_or_epsilon < SymbolType > ( ) );
46 } else {
49
50 common::symbol_or_epsilon < SymbolType > symbolVariant ( symbol );
51 res.addInputSymbol(symbol);
52 symbols.push_back ( common::symbol_or_epsilon < SymbolType > ( symbol ) );
53 }
54
56 }
57
61 break;
63 continue;
64 else
66
67 parseTransition(input, res, symbols);
69 }
70
72 throw exception::CommonException("Extra data after the automaton.");
73
74 return res;
75}
76
77template<class SymbolType, class StateType >
79 bool initial = false;
80 bool final = false;
81
83
85 res.addState(from);
86 if(initial) res.addInitialState(from);
87 if(final) res.addFinalState(from);
88
89 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
90 typename ext::vector < common::symbol_or_epsilon < SymbolType > >::const_iterator iter = symbols.begin();
91
93 if(iter == symbols.end())
94 throw exception::CommonException("Invalid line format");
95
98 do {
100 res.addState(to);
101 res.addTransition(from, *iter, to);
102
105 } while(true);
106 } else {
108 }
109 ++iter;
110 }
112
113 if(iter != symbols.end()) throw exception::CommonException("Invalid line format");
114}
115
116template<class SymbolType, class StateType >
119}
120
121template<class SymbolType, class StateType >
123 output << "MISENFA";
124 for(const auto& symbol : automaton.getInputAlphabet()) {
125 output << " ";
127 }
128
129 output << " #E";
130 output << std::endl;
131
132 for(const auto& state : automaton.getStates()) {
133 if(automaton.getInitialStates().find(state) != automaton.getInitialStates().end()) {
134 output << ">";
135 }
136 if(automaton.getFinalStates().find(state) != automaton.getFinalStates().end()) {
137 output << "<";
138 }
140
141 composeTransitionsFromState(output, automaton, state);
142
143 output << std::endl;
144 }
145}
146
147template < class SymbolType, class StateType >
149 for(const SymbolType& inputSymbol : automaton.getInputAlphabet()) {
150 const auto toStates = automaton.getTransitions ( ).equal_range(ext::tie(from, inputSymbol));
151 if ( toStates.empty ( ) ) {
152 output << " -";
153 } else {
154 bool sign = false;
155 for(const auto & transition : toStates ) {
156 output << (sign ? "|" : " ");
157 core::stringApi<StateType>::compose(output, transition.second);
158 sign = true;
159 }
160 }
161 }
162
163 ext::multimap<StateType, StateType > epsilonTransitionsFromState = automaton.getEpsilonTransitionsFromState(from);
164 if ( epsilonTransitionsFromState.empty ( ) ) {
165 output << " -";
166 } else {
167 bool sign = false;
168 for(const auto & transition : epsilonTransitionsFromState ) {
169 output << (sign ? "|" : " ");
170 core::stringApi<StateType>::compose(output, transition.second);
171 sign = true;
172 }
173 }
174}
175
176} /* namespace core */
177
static Token next(ext::istream &input)
Definition: AutomatonFromStringLexer.cpp:12
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: MultiInitialStateEpsilonNFA.h:75
Definition: symbol_or_epsilon.hpp:24
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
Definition: ostream.h:14
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
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
constexpr tuple< Elements &... > tie(Elements &... args) noexcept
Helper of extended tuple of references construction. The tuple is constructed to reffer to values in ...
Definition: tuple.hpp:218
static void initialFinalState(ext::istream &input, bool &rightArrow, bool &leftArrow)
Definition: AutomatonFromStringParserCommon.cpp:10
Definition: stringApi.hpp:26