Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
EpsilonNFA.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::EpsilonNFA < SymbolType, StateType > > {
20 static bool first ( ext::istream & input );
21 static void compose ( ext::ostream & output, const automaton::EpsilonNFA < SymbolType, StateType > & automaton );
22private:
23 static void parseTransition(ext::istream& input, ext::set<StateType>& states, const ext::vector < common::symbol_or_epsilon < SymbolType > > & symbols, StateType*& initialState, ext::set<StateType>& finalStates, ext::set<ext::tuple<StateType, common::symbol_or_epsilon < SymbolType >, StateType>>& transitionFunction);
24 static void composeTransitionsFromState(ext::ostream& output, const automaton::EpsilonNFA < SymbolType, StateType > & automaton, const StateType & from);
25};
26
27template<class SymbolType, class StateType >
29 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
30
33 }
34
36 throw exception::CommonException("Unrecognised ENFA token.");
37 }
39
43 symbols.push_back(common::symbol_or_epsilon < SymbolType > ( ));
44 } else {
47
48 common::symbol_or_epsilon < SymbolType > symbolVariant(symbol);
49 symbols.push_back(symbolVariant);
50 }
51
53 }
54
55 StateType* initialState = nullptr;
56 ext::set<StateType> finalStates;
59
63 break;
65 continue;
66 else
68
69 parseTransition(input, states, symbols, initialState, finalStates, transitionFunction);
71 }
72
74 throw exception::CommonException("Extra data after the automaton.");
75
76 if(initialState == nullptr) throw exception::CommonException("No initial state recognised.");
77
78 automaton::EpsilonNFA < > res(*initialState);
79 delete initialState;
80
81 for ( const common::symbol_or_epsilon < SymbolType > & inputSymbol : symbols) {
82 if(!inputSymbol.is_epsilon())
83 res.addInputSymbol(inputSymbol.getSymbol());
84 }
85 res.setStates(states);
86 res.setFinalStates(finalStates);
87 for ( const ext::tuple<StateType, common::symbol_or_epsilon < SymbolType >, StateType> & transition : transitionFunction) {
88 res.addTransition(std::get<0>(transition), std::get<1>(transition), std::get<2>(transition));
89 }
90
91 return res;
92}
93
94template<class SymbolType, class StateType >
96 bool initial = false;
97 bool final = false;
98
100
102 states.insert(from);
103 if(initial) {
104 if(initialState != nullptr)
105 throw exception::CommonException("Multiple initial states are not available for EpsilonNFA type");
106 initialState = new StateType(from);
107 }
108 if(final) finalStates.insert(from);
109
110 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
111 typename ext::vector<common::symbol_or_epsilon < SymbolType >>::const_iterator iter = symbols.begin();
112
114 if(iter == symbols.end())
115 throw exception::CommonException("Invalid line format");
116
119 do {
121 states.insert(to);
122 transitionFunction.insert(ext::make_tuple(from, *iter, to));
123
126 } while(true);
127 } else {
129 }
130 ++iter;
131 }
133
134 if(iter != symbols.end())
135 throw exception::CommonException("Invalid line format");
136}
137
138template<class SymbolType, class StateType >
141}
142
143template<class SymbolType, class StateType >
145 output << "ENFA";
146 for(const auto& symbol : automaton.getInputAlphabet()) {
147 output << " ";
149 }
150
151 output << " #E";
152 output << std::endl;
153
154 for(const auto& state : automaton.getStates()) {
155 if(automaton.getInitialState() == state) {
156 output << ">";
157 }
158 if(automaton.getFinalStates().find(state) != automaton.getFinalStates().end()) {
159 output << "<";
160 }
162
163 composeTransitionsFromState(output, automaton, state);
164
165 output << std::endl;
166 }
167}
168
169template < class SymbolType, class StateType >
171 for(const SymbolType& inputSymbol : automaton.getInputAlphabet()) {
172 const auto toStates = automaton.getTransitions ( ).equal_range ( ext::tie ( from, inputSymbol ) );
173 if ( toStates.empty ( ) ) {
174 output << " -";
175 } else {
176 bool sign = false;
177 for(const auto & transition : toStates ) {
178 output << (sign ? "|" : " ");
179 core::stringApi<StateType>::compose(output, transition.second);
180 sign = true;
181 }
182 }
183 }
184
185 ext::multimap<StateType, StateType > epsilonTransitionsFromState = automaton.getEpsilonTransitionsFromState(from);
186 if ( epsilonTransitionsFromState.empty ( ) ) {
187 output << " -";
188 } else {
189 bool sign = false;
190 for(const auto & transition : epsilonTransitionsFromState ) {
191 output << (sign ? "|" : " ");
192 core::stringApi<StateType>::compose(output, transition.second);
193 sign = true;
194 }
195 }
196
197}
198
199} /* namespace core */
200
static Token next(ext::istream &input)
Definition: AutomatonFromStringLexer.cpp:12
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: EpsilonNFA.h:74
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
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
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
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
Definition: stringApi.hpp:26