Skip to content

Commit

Permalink
(#301) рефакторинг томпсона и небольшие правки в антимирове
Browse files Browse the repository at this point in the history
  • Loading branch information
dak151449 committed Dec 28, 2023
1 parent ab36078 commit afd1941
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 224 deletions.
5 changes: 5 additions & 0 deletions libs/Objects/include/Objects/FiniteAutomaton.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ class FiniteAutomaton : public AbstractMachine {
// функция проверки на семантическую детерминированность
bool semdet_entry(bool annoted = false, iLogTemplate* log = nullptr) const;

// добавляет начальное состояние в начало вектора состояний
// и строит eps переход из него в начальное состояние автомата
// используется в томпсоне
void set_initial_state_zero();

public:
FiniteAutomaton();
FiniteAutomaton(int initial_state, std::vector<State> states,
Expand Down
12 changes: 5 additions & 7 deletions libs/Objects/include/Objects/Regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <memory>
#include <optional>
#include <set>
#include <unordered_set>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -52,14 +53,11 @@ class Regex : public AlgExpression {
// Производная по префиксу
bool derivative_with_respect_to_str(std::string str, const Regex* reg_e,
Regex& result) const; // NOLINT(runtime/references)
// производная для случая с отрицанием
bool brzozowski_derivative(Regex* respected_sym, const Regex* reg_e,
std::vector<Regex>& result) const;
// применение ACI правил
Regex* to_aci(std::vector<Regex>& res) const;
Regex* to_aci(std::vector<Regex>& res) const; // NOLINT(runtime/explicit)
Regex* add_alt(std::vector<Regex> res, Regex* root) const;

std::pair<std::vector<FAState>, int> get_thompson(
int nax_index, const std::set<Symbol>& root_alphabet_symbol) const;
std::vector<FAState> get_thompson(const std::set<Symbol>& root_alphabet_symbol) const;

void normalize_this_regex(
const std::vector<std::pair<Regex, Regex>>&); // переписывание regex по
Expand All @@ -72,7 +70,7 @@ class Regex : public AlgExpression {
Regex(Type type, AlgExpression* = nullptr,
AlgExpression* = nullptr); // NOLINT(runtime/explicit)

Regex* make_copy() const override;
Regex* make_copy() const override;
Regex(const Regex&) = default;

// dynamic_cast к типу Regex*
Expand Down
19 changes: 19 additions & 0 deletions libs/Objects/src/FiniteAutomaton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2535,3 +2535,22 @@ Regex FiniteAutomaton::to_regex(iLogTemplate* log) const {
}
return Regex{};
}

void FiniteAutomaton::set_initial_state_zero()
{
for (int i = 0; i < states.size(); i++) {
states[i].index++;
for (auto elem: states[i].transitions) {
auto& s = states[i].transitions.at(elem.first);
std::set<int> new_trans;
for(const auto& index: s) {
new_trans.insert(index + 1);
}
states[i].transitions[elem.first] = new_trans;
}
}

states.emplace(states.begin(), 0, "q", false);
states[0].set_transition(get_initial() + 1, Symbol::epsilon());
initial_state = 0;
}
Loading

0 comments on commit afd1941

Please sign in to comment.