Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
MultiInitialStateNFA.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::MultiInitialStateNFA < SymbolType, StateType > > {
20 static bool first ( ext::istream & input );
22private:
24 static void composeTransitionsFromState(ext::ostream& output, const automaton::MultiInitialStateNFA < 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 MISNFA token.");
39 }
41
46 res.addInputSymbol(symbol);
47 symbols.push_back(symbol);
48
50 }
51
55 break;
57 continue;
58 else
60
61 parseTransition(input, res, symbols);
63 }
64
66 throw exception::CommonException("Extra data after the automaton.");
67
68 return res;
69}
70
71template<class SymbolType, class StateType >
73 bool initial = false;
74 bool final = false;
75
77
79 res.addState(from);
80 if(initial) res.addInitialState(from);
81 if(final) res.addFinalState(from);
82
83 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
84 typename ext::vector<SymbolType>::const_iterator iter = symbols.begin();
85
87 if(iter == symbols.end())
88 throw exception::CommonException("Invalid line format");
89
92 do {
94 res.addState(to);
95 res.addTransition(from, *iter, to);
96
99 } while(true);
100 } else {
102 }
103 ++iter;
104 }
106
107 if(iter != symbols.end()) throw exception::CommonException("Invalid line format");
108}
109
110template<class SymbolType, class StateType >
113}
114
115template<class SymbolType, class StateType >
117 output << "MISNFA";
118 for(const auto& symbol : automaton.getInputAlphabet()) {
119 output << " ";
121 }
122
123 output << std::endl;
124
125 for(const auto& state : automaton.getStates()) {
126 if(automaton.getInitialStates().find(state) != automaton.getInitialStates().end()) {
127 output << ">";
128 }
129 if(automaton.getFinalStates().find(state) != automaton.getFinalStates().end()) {
130 output << "<";
131 }
133
134 composeTransitionsFromState(output, automaton, state);
135
136 output << std::endl;
137 }
138}
139
140template < class SymbolType, class StateType >
142 for(const SymbolType& inputSymbol : automaton.getInputAlphabet()) {
143 const auto toStates = automaton.getTransitions ( ).equal_range(ext::make_pair(from, inputSymbol));
144 if ( toStates.empty ( ) ) {
145 output << " -";
146 } else {
147 bool sign = false;
148 for(const auto & transition : toStates ) {
149 output << (sign ? "|" : " ");
150 core::stringApi<StateType>::compose(output, transition.second);
151 sign = true;
152 }
153 }
154 }
155}
156
157} /* namespace core */
158
static Token next(ext::istream &input)
Definition: AutomatonFromStringLexer.cpp:12
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
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
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 std::vector< T, Alloc >::const_iterator const_iterator
The type of constant values iterator.
Definition: vector.hpp:67
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
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
static void initialFinalState(ext::istream &input, bool &rightArrow, bool &leftArrow)
Definition: AutomatonFromStringParserCommon.cpp:10
Definition: stringApi.hpp:26