Teuchos - Trilinos Tools Package  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Teuchos_FiniteAutomaton.hpp
1 // @HEADER
2 // *****************************************************************************
3 // Teuchos: Common Tools Package
4 //
5 // Copyright 2004 NTESS and the Teuchos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef TEUCHOS_FINITE_AUTOMATON_HPP
11 #define TEUCHOS_FINITE_AUTOMATON_HPP
12 
13 #include <Teuchos_TableDecl.hpp>
14 #include <iosfwd>
15 #include <set>
16 #include <stdexcept>
17 
18 namespace Teuchos {
19 
20 #ifdef HAVE_TEUCHOSCORE_CXX11
21 extern template struct Table<int>;
22 #endif
23 
24 /* This is basically a weird mix between a DFA and
25  an NFA-epsilon. It is really a DFA that can have two extra
26  epsilon symbols that it accepts transitions with.
27  We can simulate epsilon-transitions to multiple new
28  states by making trees of nodes connected by
29  epsilon-transitions.
30 
31  by convention, the start state is state 0
32  */
33 struct FiniteAutomaton {
34  Table<int> table;
35  std::vector<int> accepted_tokens;
36  bool is_deterministic;
37  FiniteAutomaton() {}
38  FiniteAutomaton(int nsymbols_init, bool is_deterministic_init, int nstates_reserve);
39  void swap(FiniteAutomaton& other);
40 };
41 
42 /* NOTE: this is only needed by Teuchos::any to support its non-standard operator== */
43 inline bool operator==(FiniteAutomaton const&, FiniteAutomaton const&) {
44  return false;
45 }
46 
47 inline void swap(FiniteAutomaton& a, FiniteAutomaton& b) { a.swap(b); }
48 
49 int get_nstates(FiniteAutomaton const& fa);
50 int get_nsymbols(FiniteAutomaton const& fa);
51 bool get_determinism(FiniteAutomaton const& fa);
52 int get_epsilon0(FiniteAutomaton const& fa);
53 int get_epsilon1(FiniteAutomaton const& fa);
54 int add_state(FiniteAutomaton& fa);
55 void add_transition(FiniteAutomaton& fa, int from_state, int at_symbol, int to_state);
56 void add_accept(FiniteAutomaton& fa, int state, int token);
57 void remove_accept(FiniteAutomaton& fa, int state);
58 int step(FiniteAutomaton const& fa, int state, int symbol);
59 int accepts(FiniteAutomaton const& fa, int state);
60 int get_nsymbols_eps(FiniteAutomaton const& fa);
61 void append_states(FiniteAutomaton& fa, FiniteAutomaton const& other);
62 
63 void make_single_nfa(FiniteAutomaton& result, int nsymbols, int symbol, int token = 0);
64 void make_set_nfa(FiniteAutomaton& result, int nsymbols, std::set<int> const& accepted, int token = 0);
65 void make_range_nfa(FiniteAutomaton& result, int nsymbols, int range_start, int range_end, int token = 0);
66 void unite(FiniteAutomaton& result, FiniteAutomaton const& a, FiniteAutomaton const& b);
67 void concat(FiniteAutomaton& result, FiniteAutomaton const& a, FiniteAutomaton const& b, int token = 0);
68 void plus(FiniteAutomaton& result, FiniteAutomaton const& a, int token = 0);
69 void maybe(FiniteAutomaton& result, FiniteAutomaton const& a, int token = 0);
70 void star(FiniteAutomaton& result, FiniteAutomaton const& a, int token = 0);
71 void make_deterministic(FiniteAutomaton& result, FiniteAutomaton& nfa);
72 void simplify_once(FiniteAutomaton& result, FiniteAutomaton const& fa);
73 void simplify(FiniteAutomaton& result, FiniteAutomaton const& fa);
74 
75 void add_char_transition(FiniteAutomaton& fa, int from_state, char at_char, int to_state);
76 bool is_symbol(char c);
77 int get_symbol(char c);
78 char get_char(int symbol);
79 void make_char_set_nfa(FiniteAutomaton& result, std::set<char> const& accepted, int token = 0);
80 void make_char_range_nfa(FiniteAutomaton& result, char range_start, char range_end, int token = 0);
81 void make_char_single_nfa(FiniteAutomaton& result, char symbol_char, int token = 0);
82 void negate_set(std::set<char>& result, std::set<char> const& s);
83 
84 std::ostream& operator<<(std::ostream& os, FiniteAutomaton const& fa);
85 
86 }
87 
88 #endif