Teuchos - Trilinos Tools Package Version of the Day
Loading...
Searching...
No Matches
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
18namespace Teuchos {
19
20#ifdef HAVE_TEUCHOSCORE_CXX11
21extern 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 */
33struct 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== */
43inline bool operator==(FiniteAutomaton const&, FiniteAutomaton const&) {
44 return false;
45}
46
47inline void swap(FiniteAutomaton& a, FiniteAutomaton& b) { a.swap(b); }
48
49int get_nstates(FiniteAutomaton const& fa);
50int get_nsymbols(FiniteAutomaton const& fa);
51bool get_determinism(FiniteAutomaton const& fa);
52int get_epsilon0(FiniteAutomaton const& fa);
53int get_epsilon1(FiniteAutomaton const& fa);
54int add_state(FiniteAutomaton& fa);
55void add_transition(FiniteAutomaton& fa, int from_state, int at_symbol, int to_state);
56void add_accept(FiniteAutomaton& fa, int state, int token);
57void remove_accept(FiniteAutomaton& fa, int state);
58int step(FiniteAutomaton const& fa, int state, int symbol);
59int accepts(FiniteAutomaton const& fa, int state);
60int get_nsymbols_eps(FiniteAutomaton const& fa);
61void append_states(FiniteAutomaton& fa, FiniteAutomaton const& other);
62
63void make_single_nfa(FiniteAutomaton& result, int nsymbols, int symbol, int token = 0);
64void make_set_nfa(FiniteAutomaton& result, int nsymbols, std::set<int> const& accepted, int token = 0);
65void make_range_nfa(FiniteAutomaton& result, int nsymbols, int range_start, int range_end, int token = 0);
66void unite(FiniteAutomaton& result, FiniteAutomaton const& a, FiniteAutomaton const& b);
67void concat(FiniteAutomaton& result, FiniteAutomaton const& a, FiniteAutomaton const& b, int token = 0);
68void plus(FiniteAutomaton& result, FiniteAutomaton const& a, int token = 0);
69void maybe(FiniteAutomaton& result, FiniteAutomaton const& a, int token = 0);
70void star(FiniteAutomaton& result, FiniteAutomaton const& a, int token = 0);
71void make_deterministic(FiniteAutomaton& result, FiniteAutomaton& nfa);
72void simplify_once(FiniteAutomaton& result, FiniteAutomaton const& fa);
73void simplify(FiniteAutomaton& result, FiniteAutomaton const& fa);
74
75void add_char_transition(FiniteAutomaton& fa, int from_state, char at_char, int to_state);
76bool is_symbol(char c);
77int get_symbol(char c);
78char get_char(int symbol);
79void make_char_set_nfa(FiniteAutomaton& result, std::set<char> const& accepted, int token = 0);
80void make_char_range_nfa(FiniteAutomaton& result, char range_start, char range_end, int token = 0);
81void make_char_single_nfa(FiniteAutomaton& result, char symbol_char, int token = 0);
82void negate_set(std::set<char>& result, std::set<char> const& s);
83
84std::ostream& operator<<(std::ostream& os, FiniteAutomaton const& fa);
85
86}
87
88#endif
void swap(RCP< T > &r_ptr)
Swap the contents with some other RCP object.
The Teuchos namespace contains all of the classes, structs and enums used by Teuchos,...