Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
DFA.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <automaton/FSM/DFA.h>
9#include <core/stringApi.hpp>
10
12
14
15namespace core {
16
17template<class SymbolType, class StateType >
18struct stringApi < automaton::DFA < SymbolType, StateType > > {
20 static bool first ( ext::istream & input );
21 static void compose ( ext::ostream & output, const automaton::DFA < SymbolType, StateType > & automaton );
22private:
23 static void parseTransition(ext::istream& input, ext::set<StateType>& states, const ext::vector<SymbolType>& symbols, StateType*& initialState, ext::set<StateType>& finalStates, ext::set<ext::tuple<StateType, SymbolType, StateType>>& transitionFunction);
24 static void composeTransitionsFromState(ext::ostream& output, const automaton::DFA < 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 DFA token.");
37
39
44 symbols.push_back(symbol);
45
47 }
48
49 StateType* initialState = nullptr;
50 ext::set<StateType> finalStates;
53
57 break;
59 continue;
60 else
62
63 parseTransition(input, states, symbols, initialState, finalStates, transitionFunction);
65 }
66
68 throw exception::CommonException("Extra data after the automaton.");
69
70 if(initialState == nullptr)
71 throw exception::CommonException("No initial state recognised.");
72
73 automaton::DFA < > res(*initialState);
74 delete initialState;
75
76 res.setInputAlphabet(ext::set<SymbolType>(symbols.begin(), symbols.end()));
77 res.setStates(states);
78 res.setFinalStates(finalStates);
79
80 for(const ext::tuple<StateType, SymbolType, StateType> & transition : transitionFunction)
81 res.addTransition(std::get<0>(transition), std::get<1>(transition), std::get<2>(transition));
82
83 return res;
84}
85
86template<class SymbolType, class StateType >
88 bool initial = false;
89 bool final = false;
90
92
94 states.insert(from);
95 if ( initial ) {
96 if(initialState != nullptr)
97 throw exception::CommonException("Multiple initial states are not available for DFA type");
98 initialState = new StateType(from);
99 }
100 if ( final )
101 finalStates.insert(from);
102
103 automaton::AutomatonFromStringLexer::Token token = automaton::AutomatonFromStringLexer::next(input);
104 typename ext::vector<SymbolType>::const_iterator iter = symbols.begin();
105
107 if(iter == symbols.end())
108 throw exception::CommonException("Invalid line format");
109
113 states.insert(to);
114 transitionFunction.insert(ext::make_tuple(from, *iter, to));
115 }
117
118 ++iter;
119 }
121
122 if(iter != symbols.end())
123 throw exception::CommonException("Invalid line format");
124}
125
126template<class SymbolType, class StateType >
129}
130
131template<class SymbolType, class StateType >
133 output << "DFA";
134 for(const auto& symbol : automaton.getInputAlphabet()) {
135 output << " ";
137 }
138
139 output << std::endl;
140
141 for(const auto& state : automaton.getStates()) {
142 if(automaton.getInitialState() == state)
143 output << ">";
144
145 if(automaton.getFinalStates().find(state) != automaton.getFinalStates().end())
146 output << "<";
147
149
150 composeTransitionsFromState(output, automaton, state);
151
152 output << std::endl;
153 }
154}
155
156template < class SymbolType, class StateType >
158 ext::iterator_range < typename ext::map < ext::pair < StateType, SymbolType >, StateType >::const_iterator > symbolTransitionsFromState = automaton.getTransitionsFromState(from);
159
160 for(const SymbolType& inputSymbol : automaton.getInputAlphabet()) {
161 if ( ! symbolTransitionsFromState.empty ( ) && symbolTransitionsFromState.front ( ).first.second == inputSymbol ) {
162 output << " ";
163 core::stringApi<SymbolType>::compose(output, symbolTransitionsFromState.front ( ).second);
164 symbolTransitionsFromState.pop_front ( );
165 } else {
166 output << " -";
167 }
168 }
169}
170
171} /* namespace core */
172
static Token next(ext::istream &input)
Definition: AutomatonFromStringLexer.cpp:12
Deterministic finite automaton. Accepts regular languages.
Definition: DFA.h:71
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
Implementation of iterator_range, i.e. pair of iterators. The class provides most notably begin and e...
Definition: range.hpp:24
void pop_front()
Advances the begin iterator.
Definition: range.hpp:145
constexpr bool empty() const
Test whether the iterator_range is empty.
Definition: range.hpp:127
constexpr std::iterator_traits< Iterator >::reference front() const
Getter of the first value in the iterator_range.
Definition: range.hpp:97
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 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_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