reglibcpp  2.1.0
A C++ implementation of models for regular languages
dfa.h
Go to the documentation of this file.
1 // Copyright 2017, 2018 Tom Kranz
2 //
3 // This file is part of reglibcpp.
4 //
5 // reglibcpp is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // reglibcpp is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with reglibcpp. If not, see <https://www.gnu.org/licenses/>.
17 
18 #ifndef REG_DFA_H
19 #define REG_DFA_H
20 
22 #include <memory>
23 
24 #include <vector>
25 
26 #include <string>
27 
28 #include <valarray>
29 
31 namespace reg {
32 class fabuilder;
33 class nfa;
36 
43 class dfa {
44  public:
45  dfa();
46  dfa(std::vector<char32_t>&& alphabet,
47  std::vector<std::vector<size_t>>&& transitions,
48  std::vector<std::string>&& labels, std::valarray<bool>&& acceptingStates);
49  dfa(fabuilder& b, bool force = false);
50  dfa(dfa const& d);
51  dfa(dfa&& d);
52  dfa& operator=(dfa const& d);
53  dfa& operator=(dfa&& d);
54  virtual ~dfa();
55 
57  operator nfa const&() const;
58  bool operator==(dfa const& d) const;
59  bool operator!=(dfa const& d) const;
60  bool operator==(nfa const& d) const;
61  bool operator!=(nfa const& d) const;
62  size_t delta(size_t qi, size_t si) const;
63  size_t delta(size_t qi, std::string const& symbol) const;
64  std::string const& delta(std::string const& q,
65  std::string const& symbol) const;
66  size_t delta_(size_t qi, char32_t u32symbol) const;
67  std::string const& delta_(std::string const& q, char32_t u32symbol) const;
68  size_t deltaHat(size_t qi, std::string const& word) const;
69  std::string const& deltaHat(std::string const& q,
70  std::string const& word) const;
71  size_t deltaHat_(size_t qi, std::u32string const& u32word) const;
72  std::string const& deltaHat_(std::string const& q,
73  std::u32string const& u32word) const;
74  std::string const& getInitialState() const;
75  std::vector<std::string> const& getStates() const;
77  std::vector<char32_t> const& getAlphabet_() const;
78  bool isAccepting(size_t qi) const;
79  bool isAccepting(std::string const& q) const;
80  static fabuilder unite(dfa const& d1, dfa const& d2);
81  static fabuilder intersect(dfa const& d1, dfa const& d2);
82  static fabuilder subtract(dfa const& d1, dfa const& d2);
83  static fabuilder complement(dfa const& d);
84 
86  std::vector<std::vector<size_t>> const& transitions,
87  std::valarray<bool> const& accepting);
88 
89  friend std::string findShortestWord(dfa const& d);
90  friend std::u32string findShortestWord_(dfa const& d);
91 
92  private:
93  struct impl;
95 };
98 } // namespace reg
99 
100 #endif
u32string findShortestWord_(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:447
std::string const & getInitialState() const
Names this DFA's initial state.
Definition: dfa.cpp:391
string findShortestWord(dfa const &d)
Searches the shortest UTF-8-encoded word accepted by a given DFA.
Definition: dfa.cpp:484
bool isAccepting(size_t qi) const
Tests whether a state is an accept state within this DFA.
Definition: dfa.cpp:420
dfa()
Constructs a DFA accepting the empty language ∅.
Definition: dfa.cpp:89
Represents nondeterministic finite automata with ε-moves.
Definition: nfa.h:38
bool operator==(dfa const &d) const
Tests whether this DFA accepts exactly the same language as another one.
Definition: dfa.cpp:146
size_t deltaHat_(size_t qi, std::u32string const &u32word) const
Computes this DFA's transition function recursively for a UTF-32-encoded string of symbols...
Definition: dfa.cpp:320
Represents deterministic finite automata.
Definition: dfa.h:43
size_t deltaHat(size_t qi, std::string const &word) const
Computes this DFA's transition function recursively for a UTF-8-encoded string of symbols...
Definition: dfa.cpp:343
size_t delta(size_t qi, size_t si) const
Computes this DFA's transition function for a state index and a symbol index.
Definition: dfa.cpp:215
bool operator!=(dfa const &d) const
Tests whether this DFA doesn't accept the same language as another one.
Definition: dfa.cpp:194
static fabuilder complement(dfa const &d)
Creates a builder for a DFA accepting the complement of the language of a DFA.
Definition: dfa.cpp:547
std::vector< std::string > const & getStates() const
Fetches this DFA's set of states.
Definition: dfa.cpp:398
std::vector< std::string > const & getAlphabet() const
Fetches this DFA's set of processable symbols as UTF-8-encoded strings.
Definition: dfa.cpp:412
friend std::string findShortestWord(dfa const &d)
Searches the shortest UTF-8-encoded word accepted by a given DFA.
Definition: dfa.cpp:484
friend std::u32string findShortestWord_(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:447
Constructs finite automata step by step.
Definition: fabuilder.h:31
static std::vector< std::valarray< bool > > indistinguishableStates(std::vector< std::vector< size_t >> const &transitions, std::valarray< bool > const &accepting)
Builds the table of indistinguishable states w.r.t. a transition function.
Definition: dfa.cpp:558
Where this library lives.
Definition: dfa.cpp:48
static fabuilder unite(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the union of the languages of two DFAs.
Definition: dfa.cpp:499
static fabuilder subtract(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the set difference of the languages of two DFAs.
Definition: dfa.cpp:529
Private implementation details of DFAs.
Definition: dfa.cpp:51
static fabuilder intersect(dfa const &d1, dfa const &d2)
Creates a builder for a DFA accepting the intersection of the languages of two DFAs.
Definition: dfa.cpp:514
std::vector< char32_t > const & getAlphabet_() const
Fetches this DFA's set of processable symbols.
Definition: dfa.cpp:405
size_t delta_(size_t qi, char32_t u32symbol) const
Computes this DFA's transition function for a state index and a UTF-32-encoded symbol.
Definition: dfa.cpp:239
dfa & operator=(dfa const &d)
Copy-assigns this DFA by copying another one's private implementation object.
Definition: dfa.cpp:112