reglibcpp  2.0.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 
29 namespace reg {
30 class fabuilder;
31 class nfa;
34 
41 class dfa {
42  public:
43  dfa();
44  dfa(std::vector<char32_t>&& alphabet,
45  std::vector<std::vector<size_t>>&& transitions,
46  std::vector<std::string>&& labels, std::valarray<bool>&& acceptingStates);
47  dfa(fabuilder& b, bool force = false);
48  dfa(dfa const& d);
49  dfa(dfa&& d);
50  dfa& operator=(dfa const& d);
51  dfa& operator=(dfa&& d);
52  virtual ~dfa();
53 
55  operator nfa const&() const;
56  bool operator==(dfa const& d) const;
57  bool operator!=(dfa const& d) const;
58  bool operator==(nfa const& d) const;
59  bool operator!=(nfa const& d) const;
60  size_t delta(size_t qi, size_t si) const;
61  size_t delta(size_t qi, std::string const& symbol) const;
62  std::string const& delta(std::string const& q,
63  std::string const& symbol) const;
64  size_t delta_(size_t qi, char32_t u32symbol) const;
65  std::string const& delta_(std::string const& q, char32_t u32symbol) const;
66  size_t deltaHat(size_t qi, std::string const& word) const;
67  std::string const& deltaHat(std::string const& q,
68  std::string const& word) const;
69  size_t deltaHat_(size_t qi, std::u32string const& u32word) const;
70  std::string const& deltaHat_(std::string const& q,
71  std::u32string const& u32word) const;
72  std::string const& getInitialState() const;
73  std::vector<std::string> const& getStates() const;
75  std::vector<char32_t> const& getAlphabet_() const;
76  bool isAccepting(size_t qi) const;
77  bool isAccepting(std::string const& q) const;
78  static fabuilder unite(dfa const& d1, dfa const& d2);
79  static fabuilder intersect(dfa const& d1, dfa const& d2);
80  static fabuilder subtract(dfa const& d1, dfa const& d2);
81  static fabuilder complement(dfa const& d);
82 
84  std::vector<std::vector<size_t>> const& transitions,
85  std::valarray<bool> const& accepting);
86 
87  friend std::string findShortestWord(dfa const& d);
88  friend std::u32string findShortestWord_(dfa const& d);
89 
90  private:
91  struct impl;
93 };
96 } // namespace reg
97 
98 #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:483
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:41
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:546
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:483
friend std::u32string findShortestWord_(dfa const &d)
Searches the shortest UTF-32-encoded word accepted by a given DFA.
Definition: dfa.cpp:447
Constructs NFAs 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:557
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:498
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:528
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:513
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